Crypto001_Week1
这是Coursera上“密码学I”课程的第1周编程作业的解决方案。
其实这个解决方案很简单,尤其是给出提示要将加密的MSGS
异或在一起。只需每次取一个MSGS
,与TARGET
消息(在代码中恰好是MSGS[10]
)异或,即:
Copy code MSGS[0] XOR TARGET
MSGS[1] XOR TARGET
...
MSGS[9] XOR TARGET
这样做会得到什么?想想提示的第二部分,如果异或运算符两侧有一个空格字符和一个字母字符在同一位置,运算后字母字符会从小写变成大写,或者从大写变成小写。
假设我们在异或运算的结果中的某个位置得到一个大写字符“A”,我们知道可能在这个位置MSG[i]
和TARGET
中有一个包含空格字符,另一个包含小写字母“a”(明文)。这不是绝对的,但可能性还是比较大的。
所以这个作业中剩下的诀窍就是我们如何输出MSGS[i] XOR TARGET
的结果,以便我们可以清楚地看到结果。这里是一个可能的解决方案:
我使用python是因为作业已经有一个python源代码段,我们可以重用其中的一些部分,例如strxor()
函数。在得到MSGS[i] XOR TARGET
的结果后,我比较结果中的每个字节,看它是否在“a”“z”或“A”“Z”之间,如果是,我直接输出它;如果不是,我简单地输出一个“*”。这样我得到:
Copy codeSTART--- * * E C * * C * * * T * * S * * * E N * * X E * H T * * * * * * G Q * A * * * * A * O * * * * * * * * N * * E * A * S * L * * E F * * * O * O * * E T * * * B * * C T ---END
START--- * * * E * E * * * * D M * * * * * * L * S * N * * * N T * * * N * O * * * * * E * * E * * * * E * I C * * * * R A U * * R * * * * * * * N * O * * * * * * * T * N N E ---END
START--- * * * * * * * * E * H * * * S * * * U * S q E * * * * Q U * * N * O * * * * R * * * P * * * * * * D E * * V * * N U * * I * * E A K * * T M * * E F * * * * * * * * * ---END
START--- * * * * * * * * * * T * * * S * * * D * * * D w * * N A U * * * * * * N * * * * * * O * I * * * * * I * * * E * O * * * * * * E G * * * * * * R * I * * * * T * * * E ---END
START--- * * * * * * * U * T W * * * S * * E B * * * A w * * * * * * I * * R A K * * * E * * O * I * H * * U * * * * E * P * * * A * * * E * E * N M * * * A * * * * * * * * * ---END
START--- * * * R * E * * * T T * * S * * * * S I * * * * * * * T * * * * * H * * * T * * * * * * * * * * R * I * * V * * E * S * E * * * T * E * A * * R * R * * A * O * * C * ---END
START--- * * * R * E * * * T T * * S * * * * S I * * * * * * * O * * * * * Y * * * * * E * * A * I * * * * * S N * * * R g * * * R * * * N * E * O M * * * * * * * * E O * * * ---END
START--- * * E C * * C * * * * * * * S * * * N * S M H * * * N T * * I * * I * * * * R * * * A * * * H * * * A N * * * * G U * * T T * * * * * * T M * * * * * * * * U * * * E ---END
START--- * H M P * * * * * * * * * * Z A G * N * * C P * * * * * * * * * * E A S * * * * * M * C * * * * * E T * * * I R N * * * L * H * * * * * C * * * * E T * * * * * * * * ---END
START--- t * * E S * * * * * S * E * * * * * D * * Y T * * * * R * S A * W * W * S * * * * * N * * P * * * * T * E * * R T * * E A * * E O * E Y W * * * * N * H * N R O * * * ---END
- 现在看结果字符串中的位置0。我们得到9个“*”字符和一个小写“t”。然后可能
MSGS[9]
中的第一个字符是一个空格,TARGET
中是大写的“T”(明文)。 - 类似地,位置1可能是一个小写的“h”。
- 位置2会有点混乱,因为我们得到2个大写的“E”和一个大写的“M”。我会假设正确答案可能是小写的“e”,因为“E”出现了两次。
- 现在位置3。它有7个不同的字符,我会假设这次
TARGET
在这个位置包含一个空格字符。 - …
通过这种方式,我们应该能推断出破解后的目标消息CRACK
类似于:
The secuet message is Whtn usinw a stream cipher never use the key more than once
仍有一些奇怪的词组,不难调整为:
The secret message is When using a stream cipher never use the key more than once
我注意到“When using a stream cipher”周围有额外的空格字符。我会假设第一个额外空格实际上是一个冒号":“第二个是一个逗号”,"但我不能确定。但是很简单,因为我们现在有目标消息TARGET
和它的明文CRACK
,我们可以计算密钥,然后解密其他的MSGS
来验证密钥是否正确。现在如果我们直接使用带有两个额外空格的CRACK
,我们会得到如下破解的MSGS
:
Copy codeSTART---We can factor the numxer 15 with quantum computer. We can also factor the number 1---END
START---Euler would probably njoy that now his theorem bicomes a corner stone of crypto - ---END
START---The nice thing about Qeeyloq is now we cryptograpders can drive a lot of fancy cars---END
START---The ciphertext producd by a weak encryption algo~ithm looks as good as ciphertext ---END
START---You don't want to buy:a set of car keys from a guu who specializes in stealing cars---END
START---There are two types o| cryptography - that which {ill keep secrets safe from your l---END
START---There are two types o| cyptography: one that allo{s the Government to use brute for---END
START---We can see the point mhere the chip is unhappy if,a wrong bit is sent and consumes ---END
START---A (private-key) encrcption scheme states 3 algorethms, namely a procedure for gene---END
START--- The Concise OxfordDiytionary (2006) defines cry|to as the art of writing o r sol---END
显然,所有明文都有一些奇怪的词语(例如“numxer”、“njoy”、“bicomes”等)。现在我再次用假设的冒号和逗号补全CRACK
:
The secret message is: When using a stream cipher, never use the key more than once
我再次验证了MSGS
的明文,这次我们得到:
Copy codeSTART---We can factor the number 15 with quantum computers. We can also factor the number 1---END
START---Euler would probably enjoy that now his theorem becomes a corner stone of crypto - ---END
START---The nice thing about Keeyloq is now we cryptographers can drive a lot of fancy cars---END
START---The ciphertext produced by a weak encryption algorithm looks as good as ciphertext ---END
START---You don't want to buy a set of car keys from a guy who specializes in stealing cars---END
START---There are two types of cryptography - that which will keep secrets safe from your l---END
START---There are two types of cyptography: one that allows the Government to use brute for---END
START---We can see the point where the chip is unhappy if a wrong bit is sent and consumes ---END
START---A (private-key) encryption scheme states 3 algorithms, namely a procedure for gene---END
START--- The Concise OxfordDictionary (2006) defines crypto as the art of writing o r sol---END
现在好多了。所以问题解决了。
使用代码如下:
import string
space = ' '
letter = string.ascii_letters
print(ord(space))
print(letter)
print(''.join([chr(ord(i)^ord(space)) for i in letter]))
# 32
# abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
# ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
# 可以看出,空格和字母异或后,大小写会互换
import sys
ciphertexts=[
"315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc41556bdb36bbca3e8774301fbcaa3b83b220809560987815f65286764703de0f3d524400a19b159610b11ef3e",
"234c02ecbbfbafa3ed18510abd11fa724fcda2018a1a8342cf064bbde548b12b07df44ba7191d9606ef4081ffde5ad46a5069d9f7f543bedb9c861bf29c7e205132eda9382b0bc2c5c4b45f919cf3a9f1cb74151f6d551f4480c82b2cb24cc5b028aa76eb7b4ab24171ab3cdadb8356f",
"32510ba9a7b2bba9b8005d43a304b5714cc0bb0c8a34884dd91304b8ad40b62b07df44ba6e9d8a2368e51d04e0e7b207b70b9b8261112bacb6c866a232dfe257527dc29398f5f3251a0d47e503c66e935de81230b59b7afb5f41afa8d661cb",
"32510ba9aab2a8a4fd06414fb517b5605cc0aa0dc91a8908c2064ba8ad5ea06a029056f47a8ad3306ef5021eafe1ac01a81197847a5c68a1b78769a37bc8f4575432c198ccb4ef63590256e305cd3a9544ee4160ead45aef520489e7da7d835402bca670bda8eb775200b8dabbba246b130f040d8ec6447e2c767f3d30ed81ea2e4c1404e1315a1010e7229be6636aaa",
"3f561ba9adb4b6ebec54424ba317b564418fac0dd35f8c08d31a1fe9e24fe56808c213f17c81d9607cee021dafe1e001b21ade877a5e68bea88d61b93ac5ee0d562e8e9582f5ef375f0a4ae20ed86e935de81230b59b73fb4302cd95d770c65b40aaa065f2a5e33a5a0bb5dcaba43722130f042f8ec85b7c2070",
"32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd2061bbde24eb76a19d84aba34d8de287be84d07e7e9a30ee714979c7e1123a8bd9822a33ecaf512472e8e8f8db3f9635c1949e640c621854eba0d79eccf52ff111284b4cc61d11902aebc66f2b2e436434eacc0aba938220b084800c2ca4e693522643573b2c4ce35050b0cf774201f0fe52ac9f26d71b6cf61a711cc229f77ace7aa88a2f19983122b11be87a59c355d25f8e4",
"32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd90f1fa6ea5ba47b01c909ba7696cf606ef40c04afe1ac0aa8148dd066592ded9f8774b529c7ea125d298e8883f5e9305f4b44f915cb2bd05af51373fd9b4af511039fa2d96f83414aaaf261bda2e97b170fb5cce2a53e675c154c0d9681596934777e2275b381ce2e40582afe67650b13e72287ff2270abcf73bb028932836fbdecfecee0a3b894473c1bbeb6b4913a536ce4f9b13f1efff71ea313c8661dd9a4ce",
"315c4eeaa8b5f8bffd11155ea506b56041c6a00c8a08854dd21a4bbde54ce56801d943ba708b8a3574f40c00fff9e00fa1439fd0654327a3bfc860b92f89ee04132ecb9298f5fd2d5e4b45e40ecc3b9d59e9417df7c95bba410e9aa2ca24c5474da2f276baa3ac325918b2daada43d6712150441c2e04f6565517f317da9d3",
"271946f9bbb2aeadec111841a81abc300ecaa01bd8069d5cc91005e9fe4aad6e04d513e96d99de2569bc5e50eeeca709b50a8a987f4264edb6896fb537d0a716132ddc938fb0f836480e06ed0fcd6e9759f40462f9cf57f4564186a2c1778f1543efa270bda5e933421cbe88a4a52222190f471e9bd15f652b653b7071aec59a2705081ffe72651d08f822c9ed6d76e48b63ab15d0208573a7eef027",
"466d06ece998b7a2fb1d464fed2ced7641ddaa3cc31c9941cf110abbf409ed39598005b3399ccfafb61d0315fca0a314be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537530541ab0f9f3cd04ff50d66f1d559ba520e89a2cb2a83",
"32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904"
]
cArr = [
[int(c[i:i+2], 16) for i in range(0, len(c), 2)] for c in ciphertexts
]
def strxor(a, b):
"""xor two strings of different lengths"""
if len(a) > len(b):
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
else:
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])
def arrxor(a, b):
a = [int(a[i:i+2], 16) for i in range(0, len(a), 2)]
b = [int(b[i:i+2], 16) for i in range(0, len(b), 2)]
if len(a) > len(b):
return "".join([chr(x ^ y) for (x, y) in zip(a[:len(b)], b)])
else:
return "".join([chr(x ^ y) for (x, y) in zip(a, b[:len(a)])])
# 若某个集合内字母不同,则该位置可能为空格,将空格对应的密文都提取出来,便可进一步计算明文
treshhold = 6
spaceIndex = []
for i in range(len(possibleLetter)):
for j in range(len(possibleLetter[i])):
if len(possibleLetter[i][j]) > treshhold:
spaceIndex.append((i, j))
spaceArr = []
for i in range(len(spaceCipher)):
ss = spaceCipher[i]
if len(ss) == 1:
for hh in ss :
spaceArr.append(hh)
else:
spaceArr.append(ord(' '))
# len(spaceCipher), len(spaceArr), spaceArr
def xorDecArr(a, b):
return [chr(a[i]^b[i]) for i in range(min(len(a), len(b)))]
''.join(xorDecArr(cArr[0], spaceArr))
# 若只有一个可能的字母,则大概率为明文
def xorOneHex(idx, a, b):
i = idx * 2
a = int(a[i:i+2], 16)
b = int(b[i:i+2], 16)
return chr(a ^ b)
def xorOneDec(a, b):
return chr(a ^ b)
keySet = [set() for _ in range(max([len(cArr[i]) for i in range(len(possibleLetter))]))]
for i in range(len(possibleLetter)):
for j in range(len(cArr[i])):
possSet = possibleLetter[i][j]
if len(possSet) == 1:
for hh in possSet:
keySet[j].add(xorOneHex(0, hex(ord(hh))[2:], hex(cArr[i][j])[2:]))
keySet
key1cnt = 0
for i in keySet:
if len(i) == 1:
key1cnt += 1
print(key1cnt)
keyArr = []
for i in range(len(keySet)):
ss = keySet[i]
if len(ss) == 1:
for hh in ss :
keyArr.append(ord(hh))
else:
keyArr.append(ord(' '))
''.join(xorDecArr(keyArr, cArr[0]))
# 还是有一些有多余的,,,看看密钥空间现在多大
ll = [len(i) for i in keySet]
ll
cnt = 1
for i in ll:
if i != 0:
cnt *= i
cnt
# 我超,8388608,肯定不行了,还有那么多为0的,mmp
# 不确定密钥的长度,看看密文的长度集合
[len(i) for i in cArr], max([len(i) for i in cArr])
[(idx, len(hh)) for idx, hh in enumerate(cArr)]
target = cArr[-1]
visible = string.ascii_letters
from collections import defaultdict
hh = [defaultdict(int) for _ in range(max([len(hh) for hh in cArr]))]
for i in range(10):
print('0'+str(i), end='|')
for i in range(10, 100):
print(i, end='|')
print()
for index, i in enumerate(cArr):
res = xorDecArr(target, i)
tep = ''
for cidx, c in enumerate(res):
if c in visible:
tep += c
print(c, end = ' |')
else:
tep += '*'
print('*', end = ' |')
for cidx, c in enumerate(tep):
if c == '*':
continue
hh[cidx][c] += 1
print()
print('---Plain---')
thesh = 3
for dic in hh:
if not len(dic):
print('*', end=' |')
elif len(dic) >= thesh:
print(' ', end = ' |')
else:
maxn = max([dic[j] for j in dic])
for j in dic:
if dic[j] == maxn:
print(j.lower(), end = ' |')
break
hh
# 创建一个 defaultdict,当键不存在时,返回默认值 0
d = defaultdict(int)
# 当我们尝试访问一个不存在的键时,它会返回默认值 0
print(d["some_key"]) # 输出:0
# 现在 "some_key" 已经在字典中了,它的值是 0
print(d) # 输出:defaultdict(<class 'int'>, {'some_key': 0})
possbilePlaintext = 'The secret message is: When using a stream cipher, never use the key more than once'
possbilePlaintextArr = [ord(x) for x in possbilePlaintext]
key = [ord(x) for x in xorDecArr(possbilePlaintextArr, cArr[-1])]
for i in cArr:
print(''.join(xorDecArr(key, i)))