Project Euler — Problem 59 Solution


Each char­ac­ter on a com­put­er is assigned a unique code and the pre­ferred stan­dard is ASCII (Amer­i­can Stan­dard Code for Infor­ma­tion Inter­change). For exam­ple, upper­case A = 65, aster­isk (*) = 42, and low­er­case k = 107.

A mod­ern encryp­tion method is to take a text file, con­vert the bytes to ASCII, then XOR each byte with a giv­en val­ue, tak­en from a secret key. The advan­tage with the XOR func­tion is that using the same encryp­tion key on the cipher text, restores the plain text; for exam­ple, 65 XOR 42 = 107, then 107 XOR 42 = 65.

For unbreak­able encryp­tion, the key is the same length as the plain text mes­sage, and the key is made up of ran­dom bytes. The user would keep the encrypt­ed mes­sage and the encryp­tion key in dif­fer­ent loca­tions, and with­out both “halves”, it is impos­si­ble to decrypt the mes­sage.

Unfor­tu­nate­ly, this method is imprac­ti­cal for most users, so the mod­i­fied method is to use a pass­word as a key. If the pass­word is short­er than the mes­sage, which is like­ly, the key is repeat­ed cycli­cal­ly through­out the mes­sage. The bal­ance for this method is using a suf­fi­cient­ly long pass­word key for secu­ri­ty, but short enough to be mem­o­rable.

Your task has been made easy, as the encryp­tion key con­sists of three low­er case char­ac­ters. Using cipher1.txt (right click and ‘Save Link/Target As…’), a file con­tain­ing the encrypt­ed ASCII codes, and the knowl­edge that the plain text must con­tain com­mon Eng­lish words, decrypt the mes­sage and find the sum of the ASCII val­ues in the orig­i­nal text.