我正在尝试使用pyDES模块创建加密和解密功能。
问题:
如果我在屏幕上打印密文,然后将其复制(CTRL + C),然后将其作为crypto()函数的输入,则会出现以下异常:
ValueError: Invalid data length, data must be a multiple of 8 bytes
我的代码:
key = "12345678"
charset = "utf-8"
def encrypt():
msg = input("Enter message: ")
data = des(key, CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
cipherText = data.encrypt(msg.encode(charset))
print(cipherText)
def decrypt():
cipherText = input("Enter cipher text: ")
data = des(key, CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
decrypted = data.decrypt(cipherText).decode(charset)
print(decrypted)
虽然直接将cipherText传递给消息以解密消息(而不是通过从屏幕上手动复制消息),却可以完美地工作!像这样:
key = "12345678"
charset = "utf-8"
msg = input("Enter message: ")
data = des(key, CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
cipherText = data.encrypt(msg.encode(charset))
print(cipherText)
decrypted = data.decrypt(cipherText).decode(charset)
print(decrypted)
修复/解释将不胜感激。