Week 07: Something Awesome Krypton Game Level 5 -> 6
Okay, this is a long one as well
So we’re given a password, encoded with a vignere cipher. We’re also given two more “discovered”, encoded messages (which are a lot longer)
We’re not given the length of the key
Find the length of the key using the index of coincidence
Given the key length, use frequency analysis to find the key
I wrote some python which finds the index of coincidence for key lengths between 2 and 10
Take a key length of two for example
found1 = "SXULWGNXIOWRZJGOFLCMRHEFZALGS.....
We form two new strings, one that looks like
SUWNI... (All these letters were shifted by the key “A”)
XLWGX... (All these letters were shifted by the key “B”)
Now for these two strings, we count the number of times each letter appears (inside of a python dictionary)
Now we iterate through this dictionary, with this formula (Index of Coincidence formula)
total = total + (val*(val - 1)) / (sum1*(sum1-1))
here total ends up being our index of coincidence
val is the variable that represents the number of times (A, B, C etc..) appears in the string
sum1 represents the total number of letters counted
And we do this for each string we have (and add them all up), in this case 2, and we divide it by 2 to get the average Index of Coincidence which happens to be 0.0419...
Now we think, what if the key length was 3?
found1 = "SXULWGNXIOWRZJGOFLCMRHEFZALGS.....
So we split the string into 3 new strings, one that looks like
And we do the same thing as before, all the way up to an arbitary number
Because this is a CTF game, the keylength can’t be ridiculously long, so I’ve chosen to check up to length 10
Eventually we get these results:
Now the index of coincidence for plain english text is approximately 0.0667
Source: https://www.dcode.fr/index-coincidence
So, this closest number here is key length 9
Therefore, we can make a safe assumption that our key length is 9
Knowing this, we do frequency analysis again
We collect all letters in position 1, 10 etc... because they were all shifted by the same letter (The first letter of our vignere cipher key)
We collect all letters in position 2, 11 etc...
In the end we have 9 strings
We perform frequency analysis, by counting the number of appearances of each letter in the string, and divide it by the total count
From there, you would shift the letters until they matched the frequency that which you would find in plaintext english
Now I found a website that does the same thing as what I’m doing, but they have a nice bar graph to help us when we are trying to align the two frequency charts, as well as a smooth animation (you just click on shift left/shift right), instead of having a text editor open, and spamming space bar and having to move whatever part of the alphabet has spilled over the edge back onto the front)
Source: http://www.brianveitch.com/maze-runner/frequency-analysis-vigenere/index.html
So the key was KEYLENGTH, and the password was RANDOM!
Now I decided not to do the next level, because I decided that the amount of time it would take to solve that level (the last level in the krypton game), I could probably do quite a bit of another game!
This’ll probably be one of the levels I’ll demo in my presentation in week 08