Last active
April 9, 2018 20:38
-
-
Save junwei-wang/34948f0d89bf4511ecc179ad67b82ef6 to your computer and use it in GitHub Desktop.
The Cryptopals Crypto Challenges
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env wolframscript | |
HexToBase64 = IntegerString[#, "Base64"] &@FromDigits[#, 16] & | |
hex = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d" | |
Print @ HexToBase64[hex] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env wolframscript | |
HexXor = IntegerString[#, 16] &@ BitXor[FromDigits[#1, 16], FromDigits[#2, 16]] & | |
Print @ HexXor["1c0111001f010100061a024b53535009181c", "686974207468652062756c6c277320657965"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env wolframscript | |
charFreq = <| | |
"a" -> 0.0651738, "b" -> 0.0124248, "c" -> 0.0217339, "d" -> 0.0349835, "e" -> 0.1041442, "f" -> 0.0197881, "g" -> 0.0158610, | |
"h" -> 0.0492888, "i" -> 0.0558094, "j" -> 0.0009033, "k" -> 0.0050529, "l" -> 0.0331490, "m" -> 0.0202124, "n" -> 0.0564513, | |
"o" -> 0.0596302, "p" -> 0.0137645, "q" -> 0.0008606, "r" -> 0.0497563, "s" -> 0.0515760, "t" -> 0.0729357, "u" -> 0.0225134, | |
"v" -> 0.0082903, "w" -> 0.0171272, "x" -> 0.0013692, "y" -> 0.0145984, "z" -> 0.0007836, " " -> 0.1918182 | |
|>; | |
score[s_, key_] := Module[{l, xor, v}, | |
l = FromDigits[#, 16] & /@ StringPartition[s, 2]; | |
xor = FromCharacterCode @BitXor[l, key] // ToLowerCase; | |
v = Lookup[charFreq, #] & /@ StringPartition[xor, 1]; | |
Sum[i, {i, v // DeleteMissing}] | |
]; | |
s = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736" | |
scores = score[s, #] & /@ Range[1, 255]; | |
Print @ Position[scores, Max[scores]][[1, 1]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env wolframscript | |
charFreq = <| | |
"a" -> 0.0651738, "b" -> 0.0124248, "c" -> 0.0217339, "d" -> 0.0349835, "e" -> 0.1041442, "f" -> 0.0197881, "g" -> 0.0158610, | |
"h" -> 0.0492888, "i" -> 0.0558094, "j" -> 0.0009033, "k" -> 0.0050529, "l" -> 0.0331490, "m" -> 0.0202124, "n" -> 0.0564513, | |
"o" -> 0.0596302, "p" -> 0.0137645, "q" -> 0.0008606, "r" -> 0.0497563, "s" -> 0.0515760, "t" -> 0.0729357, "u" -> 0.0225134, | |
"v" -> 0.0082903, "w" -> 0.0171272, "x" -> 0.0013692, "y" -> 0.0145984, "z" -> 0.0007836, " " -> 0.1918182 | |
|>; | |
StringScore[s_, key_] := Module[{l, xor, v}, | |
l = FromDigits[#, 16] & /@ StringPartition[s, 2]; | |
xor = FromCharacterCode @BitXor[l, key] // ToLowerCase; | |
v = Lookup[charFreq, #] & /@ StringPartition[xor, 1]; | |
Sum[i, {i, v // DeleteMissing}] | |
]; | |
strList = ReadList["https://cryptopals.com/static/challenge-data/4.txt", String]; | |
scores = Outer[StringScore[#1, #2] &, strList, Range[1, 255]]; | |
p = Position[scores, Max[scores]]; | |
l = FromDigits[#, 16] & /@ StringPartition[strList[[p[[1, 1]]]], 2]; | |
xor = FromCharacterCode @ BitXor[l, p[[1, 2]]]; | |
Print @ xor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env wolframscript | |
keys = Table[ToCharacterCode[{"I", "C", "E"}], 100] // Flatten; | |
s = "Burning 'em, if you ain't quick and nimble | |
I go crazy when I hear a cymbal" // ToCharacterCode; | |
xor = BitXor[s, Take[keys, Length[s]]]; | |
IntegerString[#, 16, 2] & /@ xor // StringJoin // Print |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env wolframscript | |
AESDecrypt[key_ByteArray, bytes_ByteArray] := Module[{aesKey}, | |
aesKey = SymmetricKey[<|"Cipher" -> "AES128", "BlockMode" -> "ECB", "Key" -> key , "InitializationVector" -> None|>]; | |
Decrypt[aesKey, bytes] // Normal | |
]; | |
s = ReadList["https://cryptopals.com/static/challenge-data/7.txt", String] // StringJoin // BaseDecode // Normal | |
key = StringToByteArray["YELLOW SUBMARINE"]; | |
bytes = ByteArray[s]; | |
AESDecrypt[key, bytes] // FromCharacterCode // Print |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env wolframscript | |
ECBEncryptQ[s_] := ECBEncryptQ[s] = Module[{tmp, len}, | |
tmp = StringPartition[s, 32]; | |
len = DeleteDuplicates[tmp] // Length; | |
If[len < Length[tmp], True, False] | |
]; | |
s = ReadList["https://cryptopals.com/static/challenge-data/8.txt", String]; | |
(ECBEncryptQ /@ s // Position[#, True] &) // Part[s, #[[1, 1]]] & // Print |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env wolframscript | |
AESDecrypt[key_ByteArray, bytes_ByteArray, mode_, iv_: None] := Module[{aesKey}, | |
aesKey = SymmetricKey[<|"Cipher" -> "AES128", "BlockMode" -> mode, "Key" -> key , "InitializationVector" -> iv|>]; | |
Decrypt[aesKey, bytes] // Normal | |
]; | |
s = ReadList["https://cryptopals.com/static/challenge-data/10.txt", String] // StringJoin // BaseDecode; | |
AESDecrypt[StringToByteArray["YELLOW SUBMARINE"], s, "CBC", | |
ByteArray[Table[0, 16]]] // ByteArray // ByteArrayToString // Print |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env wolframscript | |
PKCS7[in_ByteArray, blocksize_] := PKCS7[in, blocksize] = Module[{mod}, | |
mod = blocksize - Mod[Length[in], blocksize]; | |
Join[in // Normal, Table[mod, mod]] | |
] | |
PKCS7[in_String, blocksize_] := PKCS7[in, blocksize] = Module[{bytes}, | |
bytes = StringToByteArray[in]; | |
PKCS7[bytes, blocksize] // ByteArray // ByteArrayToString | |
] | |
Print @ PKCS7["YELLOW SUBMARINE", 20] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://cryptopals.com/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment