无法获得正确的代码输出?
Trouble getting the correct output of code?
原加密如下(我已经做了):
对于密钥中的每个字母和明文中的每个字母(它们的长度相同),您计算 ascii 值,找到 XOR 结果,将其转换为十进制,将该值加 32,然后转换回 char。 char 将是密文。 .
key = this is a cool key
plaintext = Wonderland is cool
ciphertext= C''7e;?a/dc&<lc$*5
def decryption(ciphertext,key):
plaintext = ""
for k in range(len(ciphertext)):
i = ciphertext[k]
j = key[k]
val = xor_calc(chr(ord(i)-32), chr(ord(j)-32))
val = chr(ord(xor)+ 32)
plaintext += val
解密后我得到:
onderland 很酷
显然应该与原始明文相匹配,另外还有其他密码和密钥我缺少字母。不过,我的加密工作正常,因为我的答案与结果相符。如有任何帮助,我们将不胜感激!
忘了说我有一个函数 xor_calc 可以获取密码和密钥字母,转换为 ascii 并计算异或结果,然后 returns 返回到 char
简化 xor
的计算对我有用:
xor = chr((ord(i)-32) ^ (ord(j)-32) + 32)
事实上,如果我运行decryption(plaintext,key)
,我得到你的ciphertext
;如果 运行 decryption(ciphertext,key)
,我得到你的 plaintext
。 (这是有道理的,因为 xor
是可逆的。)
原加密如下(我已经做了): 对于密钥中的每个字母和明文中的每个字母(它们的长度相同),您计算 ascii 值,找到 XOR 结果,将其转换为十进制,将该值加 32,然后转换回 char。 char 将是密文。 .
key = this is a cool key
plaintext = Wonderland is cool
ciphertext= C''7e;?a/dc&<lc$*5
def decryption(ciphertext,key):
plaintext = ""
for k in range(len(ciphertext)):
i = ciphertext[k]
j = key[k]
val = xor_calc(chr(ord(i)-32), chr(ord(j)-32))
val = chr(ord(xor)+ 32)
plaintext += val
解密后我得到: onderland 很酷
显然应该与原始明文相匹配,另外还有其他密码和密钥我缺少字母。不过,我的加密工作正常,因为我的答案与结果相符。如有任何帮助,我们将不胜感激!
忘了说我有一个函数 xor_calc 可以获取密码和密钥字母,转换为 ascii 并计算异或结果,然后 returns 返回到 char
简化 xor
的计算对我有用:
xor = chr((ord(i)-32) ^ (ord(j)-32) + 32)
事实上,如果我运行decryption(plaintext,key)
,我得到你的ciphertext
;如果 运行 decryption(ciphertext,key)
,我得到你的 plaintext
。 (这是有道理的,因为 xor
是可逆的。)