The previous article shows that the incorrect key is returned, and the correct key is used.
In this way, the correct result is obtained.
Of course, this program only demonstrates the idea of the Caesar password and does not encrypt other symbols. This has a very serious consequence, that is, the key is easily cracked.
Example: (the key is * don't tell you,)
Yes! A! Encrypted! C!
Yes! B! Encrypted! D!
See it? What is the key?
I also mentioned a brute-force cracking attack. Here is a simple idea:
Frequency dictionary
Make a dictionary of commonly used words, such as this, he, she, me, hello, etc.
Then, the keys are enumerated, ranging from 0 to 26, and the words in the dictionary are checked for each case. If yes, the attack results will be output. Manually filter all output results.
Three results are output, and the second one is correct.
The source code is attached:
Using system; <br/> using system. collections. generic; <br/> using system. componentmodel; <br/> using system. data; <br/> using system. drawing; <br/> using system. LINQ; <br/> using system. text; <br/> using system. windows. forms; <br/> using system. io; </P> <p> namespace caesarcode <br/> {<br/> Public partial class winform: Form <br/>{< br/> Public winform () <br/>{< br/> initializecomponent (); <br/>}</P> <p> P Rivate void Caesar (INT choose) <br/>{< br/> bool flag; <br/> int key; <br/> int Len; <br/> int I; <br/> int member; <br/> string info = txtinput. text; <br/> flag = int. tryparse (txtkey. text, out key); </P> <p> If (flag = true & info. length> 0) <br/>{< br/> If (choose! = 1) <br/> key =-key; </P> <p> txtoutput. clear (); <br/> Len = info. length; <br/> for (I = 0; I <Len; I ++) <br/>{< br/> member = info [I]; <br/> If (member> = 'A' & member <= 'Z') <br/> {<br/> member = (member + key % 26 ); <br/> If (member <'A') member = (member + 26); <br/> If (member> 'Z') member = (member-26 ); <br/> txtoutput. text + = (char) member; <br/>}< br/> else if (member> = 'A' & mem Ber <= 'Z') <br/> {<br/> member = (member + key % 26); <br/> If (member <'A ') member = (member + 26); <br/> If (member> 'Z') member = (member-26); <br/> txtoutput. text + = (char) member; <br/>}< br/> else if (member> = '0' & member <= '9 ') <br/>{< br/> member = (member + key % 10); <br/> If (member <'0') member = (member + 10 ); <br/> If (member> '9') member = (member-10); <br/> txtoutput. Text + = (char) member; <br/>}< br/> else if (member >=0 & member <= 47) <br/>{< br/> member = (member + key % 48); <br/> If (member <0) member = (member + 48 ); <br/> If (member> 47) member = (member-48); <br/> txtoutput. text + = (char) member; <br/>}< br/> else if (member >=58 & member <= 64) <br/>{< br/> member = (member + key % 7); <br/> If (member <58) member = (member + 7 ); <br/> If (Member> 64) member = (member-7); <br/> txtoutput. text + = (char) member; <br/>}< br/> else if (member >=91 & member <= 96) <br/>{< br/> member = (member + key % 6); <br/> If (member <91) member = (member + 6 ); <br/> If (member> 96) member = (member-6); <br/> txtoutput. text + = (char) member; <br/>}< br/> else <br/>{< br/> txtoutput. text + = (char) (member + key); <br/>}< B R/> else <br/>{< br/> MessageBox. Show ("Incorrect key input! /Text error! "); <Br/> txtkey. clear (); <br/> txtkey. focus (); <br/>}< br/> private void btninfotocode_click (Object sender, eventargs E) <br/>{< br/> Caesar (1); <br/>}</P> <p> private void btncodetoinfo_click (Object sender, eventargs E) <br/>{< br/> Caesar (2); <br/>}</P> <p> private void btnclear_click (Object sender, eventargs E) <br/>{< br/> txtoutput. clear (); <br/> txtinput. clear (); <br/> txtkey. CLE AR (); <br/>}</P> <p> private void btnforce_click (Object sender, eventargs e) <br/>{< br/> int Len; <br/> int I; <br/> Int J; <br/> char member; <br/> bool trydec = false; <br/> string info = txtinput. text; <br/> string [] dictionary = {"the", "ello", "you", "he", "she "}; <br/> string [] result = new string [26]; <br/> txtoutput. clear (); </P> <p> for (j = 0; j <26; j ++) <br/> {<br/> Len = in Fo. length; <br/> result [J] = ""; <br/> for (I = 0; I <Len; I ++) <br/>{< br/> member = info [I]; </P> <p> If (member> = 'A' & member <= 'Z ') <br/>{< br/> member = (char) (member-J % 26); <br/> If (member <'A') member = (char) (member + 26); <br/> If (member> 'Z') member = (char) (member-26 ); <br/> result [J] + = member; <br/>}< br/> else if (member> = 'A' & member <= 'Z ') <br/> {<br/> member = (Char) (member-J % 26); <br/> If (member <'A') member = (char) (member + 26 ); <br/> If (member> 'Z') member = (char) (member-26); <br/> result [J] + = member; <br/>}< br/> else if (member> = '0' & member <= '9 ') <br/>{< br/> member = (char) (member-J % 10); <br/> If (member <'0') member = (char) (member + 10); <br/> If (member> '9') member = (char) (member-10 ); <br/> result [J] + = member; <B R/>}< br/> else <br/> {<br/> result [J] + = Member + J; <br/>}</P> <p> for (I = 0; I <5; I ++) <br/> {<br/> If (result [J]. contains (Dictionary [I]) <br/>{< br/> trydec = true; <br/> break; <br/>}< br/> If (trydec = true) <br/>{< br/> txtoutput. text + = (result [J] + "/t"); <br/> trydec = false; <br/>}< br/> If (txtoutput. text. length = 0) <br/> MessageBox. show ("cracking failed! "); <Br/>}</P> <p> private void btnimport_click (Object sender, eventargs e) <br/>{< br/> string filepath = ""; <br/> openfiledialog dialogopenfile = new openfiledialog (); <br/> dialogopenfile. addextension = true; <br/> dialogopenfile. filter = "text files (*. TXT) | *. TXT "; <br/> dialogopenfile. checkpathexists = true; <br/> dialogopenfile. title = "Import text"; <br/> dialogresult result = dialogopenfile. showdialog (); <br/> If (result = dialogresult. OK) <br/>{< br/> filepath = dialogopenfile. filename. tostring (); <br/> using (streamreader myreader = new streamreader (filepath) <br/>{< br/> txtinput. TEXT = myreader. readtoend (); <br/>}</P> <p> private void btnexport_click (Object sender, eventargs E) <br/>{< br/> string filepath = ""; <br/> savefiledialog dialogsavefile = new savefiledialog (); <br/> dialogsavefile. addextension = true; <br/> dialogsavefile. filter = "text files (*. TXT) | *. TXT "; <br/> dialogsavefile. checkpathexists = true; <br/> dialogsavefile. title = "Export text"; <br/> dialogresult result = dialogsavefile. showdialog (); <br/> If (result = dialogresult. OK) <br/>{< br/> filepath = dialogsavefile. filename. tostring (); <br/> using (streamwriter mywriter = new streamwriter (filepath) <br/>{< br/> mywriter. write (txtoutput. text); <br/>}</P> <p >}< br/>}