encryption - Ceaser Cipher crack using C language -
i writing program decrypt text using ceaser cypher algorithm. till code working fine , gets possible decrypted results have show correct one, how can this? below code decrypted strings. code answer should "3 hello world".
void main(void) { char input[] = "gourz#roohk"; for(int key = 1;x<26;key++) { printf("%i",input[i]-x%26); for(int = strlen(input)-1;i>=0;i--) { printf("%c",input[i]-x%26); } } }
recall caesar cipher has 25 possible shifts. also, text of non-trivial length, it's highly only 1 shift make input make sense. 1 possible approach, then, see if result of shift makes sense; if does, it's correct shift (e.g. compare words against dictionary see if they're "real" words; not sure if you've done web services yet, there free dictionary apis available).
consider following text: 3 uryyb jbeyq. possible shifts of this:
- 3 gdkkn vnqkc (12)
- 3 xubbe mehbt (3)
- 3 hello world (13)
- 3 jgnnq yqtnf (15)
- etc.
as can see, shift of 13 makes text contain "real" words, correct shift 13.
another possible solution (albeit more complicated) through frequency analysis (i.e. see if resulting text has same - or similar - statistical characteristics english). example, in english frequent letter "e," correct shift have "e" frequent letter. way of example, first paragraph of answer contains 48 instances of letter "e", if shift 15 letters, has 8:
gtrpaa iwpi p rpthpg rxewtg wph dcan 25 edhhxqat hwxuih. pahd, udg itmi du cdc-igxkxpa atcviw, xi'h wxvwan axztan iwpi dcan dct hwxui lxaa bpzt iwt xceji bpzt htcht. dct edhhxqat peegdprw, iwtc, xh id htt xu iwt gthjai du iwt hwxui bpzth htcht; xu xi sdth, iwtc xi'h egdqpqan iwt rdggtri hwxui (t.v. rdbepgt ldgsh pvpxchi p sxrixdcpgn id htt xu iwtn'gt "gtpa" ldgsh; cdi hjgt xu ndj'kt sdct ltq htgkxrth nti, qji iwtgt pgt ugtt sxrixdcpgn pexh pkpxapqat).
the key word here "likely" - it's not @ statistically (especially shorter texts) , it's possible write text that's resistant technique degree (e.g. through deliberate misspellings, lipograms, etc.). note have example of exception above - "3 xubbe mehbt" has more instances of letter "e" "3 hello world" though second 1 correct shift - want apply several statistical tests increase confidence (especially shorter texts).
Comments
Post a Comment