I imagine that for now, the only way to decypher a phrase would be to check each word to verify that it is valid using either a dictionary or an API. Which may be a pain.
A thing that is not going to work now, but maybe it could work in the future (big "maybe" here, but bear with me for a second):
Browsers contain their own dictionary for spell checking.
CSS has pseudo-elements for grammar and spelling errors that allows to target content within an element that the browser has identified as having grammar mistakes (::grammar-error) or being misspelled (::spelling-error).
We could create a hidden input, add the content of the decyphered text and check if there is a pseudo-element that fulfills one of the error pseudo-elements.
It would be super hacky, but it would allow us to use the browser dictionary (if it's not open already, in which case, we should use it) instead of having our own dictionary or having to call an API.
Unfortunately, ::grammar-error and ::spelling-error are not currently supported by any browser, so I cannot test this crazy idea at the moment.
Without any proper dictionary, I guess I'll stick to the "bruteforce" method! Here's how it works:
First, build an Array containing numbers from 26 to 1 ([...Array(26)].map((_,i)=>26-i))
Print the current index (26-n) and the cipher (s)
Build the associated original text: For each character, if its charCode is between 97 and 122 (hence, if it is a valid lowercase character), add the current index to it, else just return it.
My solution for cracking the message tries all keys, performs frequency analysis on each result, and sorts the candidates by closeness to the frequencies of letters in the English language.
-module(caesar).-compile(export_all).-export([caesar/2]).-include_lib("eunit/include/eunit.hrl").shift_char(Char,Key)whenChar>=$a,Char=<$z->$a+(Char-$a+Key)rem26;shift_char(Char,Key)whenChar>=$A,Char=<$Z->$A+(Char-$A+Key)rem26;shift_char(Char,_)->Char.caesar(Message,Key)whenis_integer(Key),Key>0,Key<26->lists:map(fun(C)->shift_char(C,Key)end,Message).shift_char_test_()->[?_assertEqual($b,shift_char($a,1)),?_assertEqual($f,shift_char($a,5)),?_assertEqual($a,shift_char($z,1)),?_assertEqual($z,shift_char($r,8)),?_assertEqual($b,shift_char($r,10)),?_assertEqual($M,shift_char($K,2)),?_assertEqual($D,shift_char($W,7))].casear_test_()->[?_assertEqual("b",caesar("a",1)),?_assertEqual("e",caesar("a",4)),?_assertEqual("a",caesar("z",1)),?_assertEqual("efgfoe uif fbtu xbmm pg uif dbtumf",caesar("defend the east wall of the castle",1)),?_assertEqual("dwwdfn iurp wkh zrrgv dw gdzq",caesar("attack from the woods at dawn",3)),?_assertEqual("Dro aesmu, lbygx pyh TEWZON yfob dro vkji nyq!",caesar("The quick, brown fox JUMPED over the lazy dog!",10))].% calculate the frequencies of the letters in the message
freqs(Message)->freqs(string:lowercase(Message),#{},0).freqs([C|Rest],Freqs,Count)whenC>=$a,C=<$z->NewFreqs=maps:put(C,maps:get(C,Freqs,0)+1,Freqs),freqs(Rest,NewFreqs,Count+1);freqs([_|Rest],Freqs,Count)->freqs(Rest,Freqs,Count);freqs([],Freqs,Count)->maps:map(fun(_C,Freq)->100*Freq/Countend,Freqs).% calculate the distance between the frequency of a letter in the message
% and the frequency of the same letter in the English language
freq_distance(C,Freq)->EnglishFreq=maps:get(C,english_frequencies()),math:pow(EnglishFreq-Freq,2).% calculate the distance between frequencies in the message and frequencies in the English language
distance(Freqs)->lists:foldr(fun({_,Dist},Acc)->Acc+Distend,0,maps:to_list(maps:map(funfreq_distance/2,Freqs))).% try all keys and sort them starting with the decrypted message that has the least distance
% with the frequencies of the English language
crack(Message)->Keys=lists:seq(1,25),Candidates=lists:map(fun(Key)->{26-Key,caesar(Message,Key)}end,Keys),Freqs=lists:map(fun({Key,Msg})->{Key,Msg,freqs(Msg)}end,Candidates),Distances=lists:map(fun({Key,Msg,Frq})->{Key,Msg,distance(Frq)}end,Freqs),_BestKeys=lists:sort(fun({_,_,Dist1},{_,_,Dist2})->(Dist1-Dist2)=<0end,Distances).% frequencies of the letters in the English language, from
% http://pi.math.cornell.edu/~mec/2003-2004/cryptography/subs/frequencies.html
english_frequencies()->_Frequencies=#{$e=>12.02,$t=>9.10,$a=>8.12,$o=>7.68,$i=>7.31,$n=>6.95,$s=>6.28,$r=>6.02,$h=>5.92,$d=>4.32,$l=>3.98,$u=>2.88,$c=>2.71,$m=>2.61,$f=>2.30,$y=>2.11,$w=>2.09,$g=>2.03,$p=>1.82,$b=>1.49,$v=>1.11,$k=>0.69,$x=>0.17,$q=>0.11,$j=>0.10,$z=>0.07}.
To try:
% erl
1> c(caesar).
{ok,caesar}
2> caesar:test().
All 13 tests passed.
ok
3> caesar:caesar( "attack from the woods at dawn", 3 ).
"dwwdfn iurp wkh zrrgv dw gdzq"
4> caesar:crack("efgfoe uif fbtu xbmm pg uif dbtumf" ).
[{1,"defend the east wall of the castle",232.3872551020408},
{12,"stutcs iwt tphi lpaa du iwt rphiat",339.90096938775514},
{13,"rstsbr hvs sogh kozz ct hvs qoghzs",514.9585122448979},
...
5> caesar:crack("dwwdfn iurp wkh zrrgv dw gdzq").
[{3,"attack from the woods at dawn",309.13214444444446},
{18,"leelnv qczx esp hzzod le olhy",409.1672777777778},
{25,"exxego jvsq xli asshw ex hear",415.19877777777776},
6> caesar:crack("Dro aesmu, lbygx pyh TEWZON yfob dro vkji nyq!").
[{10,"The quick, brown fox JUMPED over the lazy dog!",
142.00382345679012},
{20,"Jxu gkysa, rhemd ven ZKCFUT eluh jxu bqpo tew!",
281.83945679012345},
{21,"Iwt fjxrz, qgdlc udm YJBETS dktg iwt apon sdv!",
307.0069345679012},
Of course, instead of decrypting the message 25 times, I should perform frequency analysis, shift the keys of the map 25 times, take the three shortest distances and then decrypt the message with the corresponding keys. I’ll update my solution later.
usingSystem;classMainClass{publicstaticvoidMain(string[]args){varcaesarCipher=newCaesarCipher();caesarCipher.Encrypt(3,"attack from the woods at dawn");}}publicclassCaesarCipher{publicvoidEncrypt(intkey,stringtext){if(key<1||key>26){thrownewException("Key must be between 1 and 26 inclusive.");}Console.WriteLine("Plain:");Console.WriteLine(text);vardecText=newchar[text.Length];for(inti=0;i<text.Length;i++){intdec=(int)text[i];if(dec>=65&&dec<=90||dec>=97&&dec<=122){decText[i]=(char)(dec+key);}else{decText[i]=(char)dec;}}Console.WriteLine("Encrypted:");Console.WriteLine(decText);}}
#include <iostream>
intmain(){unsignedkey=1;std::stringtoEncrypt="defend the east wall of the castle";if(key<1||key>26){std::cout<<"Key must be in range of 1-26"<<std::endl;return0;}for(char&c:toEncrypt){if(c<'A'||c>'z'){continue;}elseif(c+key>'z'){c='a'+(c+key)%122-1;continue;}elseif(c+key>'Z'&&c<'a'){c='A'+(c+key)%90-1;continue;}c+=key;}std::cout<<toEncrypt;return0;}
//-----------------------------------------------------------------------//-----------------------------Caesar Cipher-----------------------------//-----------------------------------------------------------------------//a == 97 z == 122//A == 65 Z == 90constcypher=function(msg,key){letinRange=(number,x,y)=>{if(number>=x&&number<=y){returntrue;}returnfalse;}letnewMsg="";for(leti=0;i<msg.length;i++){letcharUnicode=msg.codePointAt(i);if(inRange(charUnicode,97,122)||inRange(charUnicode,65,90)){newMsg+=String.fromCodePoint(charUnicode+key);}else{newMsg+=msg[i];}}returnnewMsg;}letmessage="This took hella lotta time !";console.log(cypher(message,4));
classStringdefcesar_encrypt(key)raiseArgumentError,'Key must be between 1 and 26 inclusive.'ifkey<1||key>26letters_min=('a'..'z').to_aletters_maj=('A'..'Z').to_aresult=tr!(letters_min.join,letters_min.rotate(key).join)result.tr(letters_maj.join,letters_maj.rotate(key).join)enddefcesar_decrypt(key)raiseArgumentError,'Key must be between 1 and 26 inclusive.'ifkey<1||key>26letters_min=('a'..'z').to_aletters_maj=('A'..'Z').to_aresult=tr!(letters_min.join,letters_min.rotate(-key).join)result.tr(letters_maj.join,letters_maj.rotate(-key).join)endendpp'efgfoe uif fbtu xbmm pg uif dbtumfabAB'.cesar_decrypt(1)pp'defend the east wall of the castlezaZA'.cesar_encrypt(1)
Using English frequency analysis (but you actually need to know the language source), here is a javascript version:
// English letter frequency a-z// source: https://en.wikipedia.org/wiki/Letter_frequencyconstENGLISH_FREQUENCIES=newFloat32Array([0.0816,0.0149,0.0278,0.0425,0.1270,0.0222,0.0201,0.0609,0.0696,0.0015,0.0077,0.0402,0.0240,0.0674,0.0750,0.0192,0.0009,0.0598,0.0632,0.0905,0.0275,0.0097,0.0236,0.0015,0.0197,0.0007,].map(v=>Math.log(v)/Math.LN2));// Here we are !constdecipher=str=>Array.from({length:26},(_,key)=>{// Rotate textconsttext=str.toLowerCase().replace(/[a-z]/g,$0=>String.fromCharCode(97+(($0.charCodeAt(0)-97+key)%26)));// Get entropy information based on Shannon// https://en.wikipedia.org/wiki/Entropy_(information_theory)constentropy=text.match(/[a-z]/g).map(c=>ENGLISH_FREQUENCIES[c.charCodeAt(0)-97]).reduce((sum,frequency,_,arr)=>sum-frequency/arr.length,0);return{key,text,entropy};}).sort((a,b)=>a.entropy-b.entropy);// Sort results
You'll get results sort by entropy score:
console.log(decipher("dwwdfn iurp wkh zrrgv dw gdzq").map(({key,text,entropy})=>`${entropy.toFixed(3)} - ${text} (${key})`).join("\n"));// 4.227 - attack from the woods at dawn (23)// 4.936 - piiprz ugdb iwt lddsh pi splc (12)// 4.974 - ohhoqy tfca hvs kccrg oh rokb (11)// 5.098 - tmmtvd ykhf max phhwl tm wtpg (16)
I now perform frequency analysis on the encrypted message, calculate the correlation with the frequencies of letters in the English language, and use the best N correlation values to decrypt the message.
-module(caesar).-export([caesar/2,crack/1,crack/2]).-include_lib("eunit/include/eunit.hrl").wrap(Char,Length)whenis_integer(Length),Length>0->(CharremLength+Length)remLength.shift_char(Char,Key)whenChar>=$a,Char=<$z->$a+wrap(Char-$a+Key,26);shift_char(Char,Key)whenChar>=$A,Char=<$Z->$A+wrap(Char-$A+Key,26);shift_char(Char,_)->Char.caesar(Message,Key)whenis_integer(Key)->lists:map(fun(C)->shift_char(C,Key)end,Message).% calculate the frequencies of the letters in the message
freqs(Message)->freqs(string:lowercase(Message),#{},0).freqs([C|Rest],Freqs,Count)whenC>=$a,C=<$z->NewFreqs=maps:put(C,maps:get(C,Freqs,0)+1,Freqs),freqs(Rest,NewFreqs,Count+1);freqs([_|Rest],Freqs,Count)->freqs(Rest,Freqs,Count);freqs([],Freqs,Count)->maps:map(fun(_C,Freq)->100*Freq/Countend,Freqs).freq_array(FreqMap)->lists:map(fun(X)->maps:get($a+X-1,FreqMap,0)end,lists:seq(1,26)).shift_fun(F,Tau,Period)->{F1,F2}=lists:split(wrap(Tau,Period),F),F2++F1.correlation(F,G)whenlength(F)==length(G)->Period=length(F),lists:map(fun(Tau)->Z=lists:zip(F,shift_fun(G,Tau,Period)),lists:foldl(fun({X,Y},Acc)->X*Y/Period+Accend,0,Z)end,lists:seq(0,Period-1)).% determine the most likely keys by correlation with the frequencies in the English language
crack(Message)->crack(Message,1).crack(Message,Attempts)->Freqs=freqs(Message),Correlation=correlation(freq_array(english_frequencies()),freq_array(Freqs)),Points=lists:zip(lists:seq(0,25),Correlation),BestKeys=lists:reverse(lists:ukeysort(2,Points)),lists:sublist(lists:map(fun({Key,C})->{Key,C,caesar(Message,-Key)}end,BestKeys),Attempts).% frequencies of the letters in the English language, from
% http://pi.math.cornell.edu/~mec/2003-2004/cryptography/subs/frequencies.html
english_frequencies()->_Frequencies=#{$e=>12.02,$t=>9.10,$a=>8.12,$o=>7.68,$i=>7.31,$n=>6.95,$s=>6.28,$r=>6.02,$h=>5.92,$d=>4.32,$l=>3.98,$u=>2.88,$c=>2.71,$m=>2.61,$f=>2.30,$y=>2.11,$w=>2.09,$g=>2.03,$p=>1.82,$b=>1.49,$v=>1.11,$k=>0.69,$x=>0.17,$q=>0.11,$j=>0.10,$z=>0.07}.wrap_test_()->[?_assertEqual(0,wrap(0,26)),?_assertEqual(3,wrap(3,26)),?_assertEqual(0,wrap(26,26)),?_assertEqual(25,wrap(-1,26)),?_assertEqual(10,wrap(-42,26)),?_assertEqual(25,wrap(77,26)),?_assertEqual(5,wrap(15,10)),?_assertEqual(0,wrap(7,1))].shift_char_test_()->[?_assertEqual($b,shift_char($a,1)),?_assertEqual($f,shift_char($a,5)),?_assertEqual($a,shift_char($z,1)),?_assertEqual($z,shift_char($r,8)),?_assertEqual($b,shift_char($r,10)),?_assertEqual($M,shift_char($K,2)),?_assertEqual($D,shift_char($W,7)),?_assertEqual($a,shift_char($a,0)),?_assertEqual($a,shift_char($b,-1)),?_assertEqual($c,shift_char($a,28)),?_assertEqual($r,shift_char($c,-11))].casear_test_()->[?_assertEqual("b",caesar("a",1)),?_assertEqual("e",caesar("a",4)),?_assertEqual("a",caesar("z",1)),?_assertEqual("efgfoe uif fbtu xbmm pg uif dbtumf",caesar("defend the east wall of the castle",1)),?_assertEqual("dwwdfn iurp wkh zrrgv dw gdzq",caesar("attack from the woods at dawn",3)),?_assertEqual("Dro aesmu, lbygx pyh TEWZON yfob dro vkji nyq!",caesar("The quick, brown fox JUMPED over the lazy dog!",10)),?_assertEqual(caesar("defend the east wall of the castle",15),caesar("defend the east wall of the castle",-11))].freqs_test_()->[?_assertEqual(#{$a=>100.0},freqs("a")),?_assertEqual(#{$b=>50.0,$f=>50.0},freqs("bf fb")),?_assertEqual(#{$a=>50.0,$b=>30.0,$c=>20.0},freqs("baac baba bacca cabba aa"))].freq_array_test_()->[?_assertEqual(100.0,lists:nth(1,freq_array(#{$a=>100.0}))),?_assertEqual(50.0,lists:nth(2,freq_array(#{$b=>50.0,$f=>50.0}))),?_assertEqual(50.0,lists:nth(6,freq_array(#{$b=>50.0,$f=>50.0}))),?_assertEqual(50.0,lists:nth(1,freq_array(#{$a=>50.0,$b=>30.0,$c=>20.0}))),?_assertEqual(30.0,lists:nth(2,freq_array(#{$a=>50.0,$b=>30.0,$c=>20.0}))),?_assertEqual(20.0,lists:nth(3,freq_array(#{$a=>50.0,$b=>30.0,$c=>20.0})))].correlation_test_()->F=[0,0,2,0,0],G=[0,1,2,3,0],H=[1,2,1,4,6],Appr=fun(L)->lists:map(fun(X)->round(X*100)end,L)end,[?_assertEqual(Appr([4/5,0.0,0.0,0.0,0.0]),Appr(correlation(F,F))),?_assertEqual(Appr([4/5,6/5,0.0,0.0,2/5]),Appr(correlation(F,G))),?_assertEqual(Appr([2/5,8/5,12/5,2/5,4/5]),Appr(correlation(F,H))),?_assertEqual(Appr([4/5,2/5,0.0,0.0,6/5]),Appr(correlation(G,F))),?_assertEqual(Appr([14/5,8/5,3/5,3/5,8/5]),Appr(correlation(G,G))),?_assertEqual(Appr([16/5,27/5,19/5,14/5,8/5]),Appr(correlation(G,H))),?_assertEqual(Appr([2/5,4/5,2/5,12/5,8/5]),Appr(correlation(H,F))),?_assertEqual(Appr([16/5,8/5,14/5,19/5,27/5]),Appr(correlation(H,G))),?_assertEqual(Appr([58/5,38/5,31/5,31/5,38/5]),Appr(correlation(H,H)))].
To try:
% erl
1> c(caesar).
{ok,caesar}
2> caesar:crack("efgfoe uif fbtu xbmm pg uif dbtumf"). [{1,27.732142857142858, "defend the east wall of the castle"}]
3> caesar:crack("dwwdfn iurp wkh zrrgv dw gdzq").
[{3,24.07692307692308,"attack from the woods at dawn"}]
4> caesar:crack("Dro aesmu, lbygx pyh TEWZON yfob dro vkji nyq!", 3).
[{10,19.342948717948715, "The quick, brown fox JUMPED over the lazy dog!"},
{0,16.821581196581192, "Dro aesmu, lbygx pyh TEWZON yfob dro vkji nyq!"},
{20,16.384615384615383, "Jxu gkysa, rhemd ven ZKCFUT eluh jxu bqpo tew!"}]
JavaScript
I imagine that for now, the only way to decypher a phrase would be to check each word to verify that it is valid using either a dictionary or an API. Which may be a pain.
A thing that is not going to work now, but maybe it could work in the future (big "maybe" here, but bear with me for a second):
It would be super hacky, but it would allow us to use the browser dictionary (if it's not open already, in which case, we should use it) instead of having our own dictionary or having to call an API.
Unfortunately,
::grammar-errorand::spelling-errorare not currently supported by any browser, so I cannot test this crazy idea at the moment.Here we go:
Without any proper dictionary, I guess I'll stick to the "bruteforce" method! Here's how it works:
[...Array(26)].map((_,i)=>26-i))26-n) and the cipher (s)I’m learning Erlang and I love it.
My solution for cracking the message tries all keys, performs frequency analysis on each result, and sorts the candidates by closeness to the frequencies of letters in the English language.
To try:
Of course, instead of decrypting the message 25 times, I should perform frequency analysis, shift the keys of the map 25 times, take the three shortest distances and then decrypt the message with the corresponding keys. I’ll update my solution later.
C#
Rept.it
C++
JavaScript
Ruby
Using English frequency analysis (but you actually need to know the language source), here is a javascript version:
You'll get results sort by entropy score:
ruby <3
My updated Erlang solution.
I now perform frequency analysis on the encrypted message, calculate the correlation with the frequencies of letters in the English language, and use the best N correlation values to decrypt the message.
To try:
My solution in js
Python :
One line :
This is an interesting one. It can be easily solved if you implement first the modulo function in JavaScript.
I have an article about this:
dev.to/codeguppy/javascript-modulo...