<s> from prob1 import hexToRaw , rawToHexLUT <EOL> from prob2 import hex_xor <EOL> import string <EOL> letterFrequency = { } <EOL> letterFrequency [ '<STR_LIT:A>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:B>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:C>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:D>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:E>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:F>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:H>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:I>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:L>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:M>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:N>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:O>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:P>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:R>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:S>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:T>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:X>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:Y>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT>' ] = <NUM_LIT> ; <EOL> letterFrequency [ '<STR_LIT:U+0020>' ] = <NUM_LIT> ; <EOL> def calculateMG ( plain ) : <EOL> counts = [ ] ; <EOL> for i in range ( <NUM_LIT> ) : <EOL> counts . append ( <NUM_LIT:0> ) ; <EOL> for i in range ( len ( plain ) ) : <EOL> if ( plain [ i ] < <NUM_LIT> ) : <EOL> counts [ ord ( chr ( plain [ i ] ) . upper ( ) ) ] += <NUM_LIT:1> ; <EOL> result = <NUM_LIT:0.0> ; <EOL> for i in string . ascii_uppercase : <EOL> result += letterFrequency [ i ] * counts [ ord ( i ) ] ; <EOL> result += letterFrequency [ '<STR_LIT:U+0020>' ] * counts [ ord ( '<STR_LIT:U+0020>' ) ] ; <EOL> result /= len ( plain ) ; <EOL> return result ; <EOL> def tryKey ( cipher , key ) : <EOL> fullkey = key * len ( cipher ) ; <EOL> fullkey = fullkey [ : len ( cipher ) ] ; <EOL> potential_plain = hex_xor ( cipher , fullkey ) ; <EOL> return calculateMG ( hexToRaw ( potential_plain ) ) , potential_plain ; <EOL> def findGoodKeys ( cipher ) : <EOL> for i in range ( <NUM_LIT> ) : <EOL> mg , plain = tryKey ( cipher , rawToHexLUT [ i ] ) ; <EOL> if ( mg > <NUM_LIT> ) : <EOL> print ( "<STR_LIT>" + rawToHexLUT [ i ] ) ; <EOL> print ( "<STR_LIT>" + str ( plain ) . lstrip ( "<STR_LIT>" ) . rstrip ( "<STR_LIT:'>" ) ) ; <EOL> print ( "<STR_LIT>" + str ( hexToRaw ( str ( plain ) . lstrip ( "<STR_LIT>" ) . rstrip ( "<STR_LIT:'>" ) ) ) . lstrip ( "<STR_LIT>" ) . rstrip ( "<STR_LIT:'>" ) ) ; <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> cip = b'<STR_LIT>' ; <EOL> findGoodKeys ( cip ) ; </s>
