我現在很擔心,我的編程習慣不太好。我從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%關於密碼學方面的問題,如果你問我。 –