我现在很担心,我的编程习惯不太好。我从thier网站修改了基本pyDes代码:pyDes解密“数据必须是8字节的倍数”
import os, sys, binascii
text = input ("Text to be encrypted...")
key = input ("Key...")
sys.path.append (os.path.abspath ("").split (":") [0] + ":\\Python\\Libraries\\pyDes")
import pyDes
def toKey (string):
b = string
a = 0
if len (b) > 16:
while len (b) != 16:
b = b [:-1]
elif len (b) < 16:
while len (b) != 16:
b += b [a]
a += 1
return b
key = toKey (key)
data = pyDes.triple_des(key, pyDes.CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=pyDes.PAD_PKCS5)
print ("Before: " + text)
encrypted = str (data.encrypt (text)) [2:-1]
print ("Encrypted: " + encrypted)
decrypted = str (data.decrypt (encrypted)) [2:-1]
print ("Decrypted: " + decrypted)
当我运行该程序,加密工作正常,然而,解密引发错误:
Traceback (most recent call last):
File "E:\Python\Examples\Encrypt.py", line 23, in <module>
decrypted = str (data.decrypt (encrypted)) [2:-1]
File "E:\Python\Libraries\pyDes\pyDes.py", line 836, in decrypt
block = self.__key3.crypt(iv, DECRYPT)
File "E:\Python\Libraries\pyDes\pyDes.py", line 572, in crypt
raise ValueError("Invalid data length, data must be a multiple of " + str(self.block_size) + " bytes\n.")
ValueError: Invalid data length, data must be a multiple of 8 bytes
很抱歉,如果这是真的很烦简单:-(
如果不允许编码/解码问题,我们可以删除约30%关于密码学方面的问题,如果你问我。 –