這是上述相同的答案,但我修改所以它可以與python 3一起使用。
import binascii
from Crypto.Cipher import AES
from Crypto import Random
def encrypt(passwrd, message):
msglist = []
key = bytes(passwrd, "utf-8")
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv)
msg = iv + cipher.encrypt(bytes(message, "utf-8"))
msg = binascii.hexlify(msg)
for letter in str(msg):
msglist.append(letter)
msglist.remove("b")
msglist.remove("'")
msglist.remove("'")
for letter in msglist:
print(letter, end="")
print("")
def decrypt(passwrd, message):
msglist = []
key = bytes(passwrd, "utf-8")
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv)
msg = cipher.decrypt(binascii.unhexlify(bytes(message, "utf-8")))[len(iv):]
for letter in str(msg):
msglist.append(letter)
msglist.remove("b")
msglist.remove("'")
msglist.remove("'")
for letter in msglist:
print(letter, end="")
print("")
還有http://bugs.python.org/issue7475 – jfs
SO,'hex'代碼丟失!有什麼方法可以添加該編解碼器或方法,鏈接上有兩個文件,這有什麼作用? – iMagur
要以十六進制格式轉換**數字**,仍然可以使用'hex(n)'。 – JeromeJ