Vigenere密码解密问题
Vigenere Cipher Decryption Issues
key = "password"
def cipher(text): #my example cipher method
encoded_chars = []
for i in range(len(text)):
key_c = key[i % len(key)]
encoded_c = chr(ord(text[i]) + ord(key_c) % 256)
encoded_chars.append(encoded_c)
encoded_string = ''.join(encoded_chars)
return (encoded_string)
def decipher(text):
dec = []
text = base64.urlsafe_b64encode(b"'{text}'").decode()
print('text')
for i in range(len(text)):
key_c = key[i % len(key)]
dec_c = chr((256 + ord(text[i]) - ord(key_c)) % 256)
dec.append(text)
return str(dec_c)
enter code here
print(decipher("test"))
用 vigenere 密码成功加密后,我得到如下信息:b'\xc2\xbd\xc2\xb8\xc3\x83\xc3\x80'
我想用上面的代码解密这个字符串并将其转换回文本 "password" 但是当我尝试解密我的文本时它给我留下了另一个编码的字符串。有人可以解释我的解密有什么问题吗?
不清楚你想用 base64 库做什么。此外,您在解密方法中混淆了变量:
dec.append(text) # text is the passed in argument.
# Why append it to the result
return str(dec_c) # this is the last element in the loop
# why return this rather than the list you made above?
以上这些都没有多大意义。也许这样的事情会让它朝着正确的方向前进:
key = "password"
def cipher(text): #my example cipher method
encoded_chars = []
for i in range(len(text)):
key_c = key[i % len(key)]
encoded_c = chr(ord(text[i]) + ord(key_c) % 256)
encoded_chars.append(encoded_c)
encoded_string = ''.join(encoded_chars)
return (encoded_string)
def decipher(text):
dec = []
for i in range(len(text)):
key_c = key[i % len(key)]
dec_c = chr((256 + ord(text[i]) - ord(key_c)) % 256)
dec.append(dec_c)
return "".join(dec)
plain = "some text to encrypt"
encrypted = cipher(plain)
print("encrypted: ", encrypted)
decrypted = decipher(encrypted)
print("decrypted: ", decrypted)
打印:
encrypted: ãÐàØã×ÜäçâÔàÇâÚãç
decrypted: some text to encrypt
key = "password"
def cipher(text): #my example cipher method
encoded_chars = []
for i in range(len(text)):
key_c = key[i % len(key)]
encoded_c = chr(ord(text[i]) + ord(key_c) % 256)
encoded_chars.append(encoded_c)
encoded_string = ''.join(encoded_chars)
return (encoded_string)
def decipher(text):
dec = []
text = base64.urlsafe_b64encode(b"'{text}'").decode()
print('text')
for i in range(len(text)):
key_c = key[i % len(key)]
dec_c = chr((256 + ord(text[i]) - ord(key_c)) % 256)
dec.append(text)
return str(dec_c)
enter code here
print(decipher("test"))
用 vigenere 密码成功加密后,我得到如下信息:b'\xc2\xbd\xc2\xb8\xc3\x83\xc3\x80'
我想用上面的代码解密这个字符串并将其转换回文本 "password" 但是当我尝试解密我的文本时它给我留下了另一个编码的字符串。有人可以解释我的解密有什么问题吗?
不清楚你想用 base64 库做什么。此外,您在解密方法中混淆了变量:
dec.append(text) # text is the passed in argument.
# Why append it to the result
return str(dec_c) # this is the last element in the loop
# why return this rather than the list you made above?
以上这些都没有多大意义。也许这样的事情会让它朝着正确的方向前进:
key = "password"
def cipher(text): #my example cipher method
encoded_chars = []
for i in range(len(text)):
key_c = key[i % len(key)]
encoded_c = chr(ord(text[i]) + ord(key_c) % 256)
encoded_chars.append(encoded_c)
encoded_string = ''.join(encoded_chars)
return (encoded_string)
def decipher(text):
dec = []
for i in range(len(text)):
key_c = key[i % len(key)]
dec_c = chr((256 + ord(text[i]) - ord(key_c)) % 256)
dec.append(dec_c)
return "".join(dec)
plain = "some text to encrypt"
encrypted = cipher(plain)
print("encrypted: ", encrypted)
decrypted = decipher(encrypted)
print("decrypted: ", decrypted)
打印:
encrypted: ãÐàØã×ÜäçâÔàÇâÚãç
decrypted: some text to encrypt