PyCrypto 无法正确打印

PyCrypto Not Printing Properly

嗨,所以在我得到解密字符串前面的 IV(我相信是 IV 的解密值)之前。现在我没有得到字符串...我如何使它工作,现在已经尝试了几个小时...

我的代码:

    from Crypto.Cipher import AES
    import hashlib
    import base64
    import os
    import string

    iv = os.urandom(16)
    key = hashlib.sha256(b'mypassword123').digest()
    plaintext = (b'the password is totally not secure')
    cipher = AES.new(key, AES.MODE_CFB, iv)
    ciphertext = iv + cipher.encrypt(plaintext)
    print (ciphertext)
    print ("IV = ",iv)
    ciphertext = ciphertext.split(iv)
    ciphertext = str(ciphertext)[1].strip()
    plaintext = cipher.decrypt(ciphertext)
    print (plaintext)

encrypt会变cipher,你得重新做一个; str 会将 byte 更改为 repr(byte),如下所示:

a=b'xxx'
str(a) # "b'xxx'"

from Crypto.Cipher import AES
import hashlib
import base64
import os
import string

iv = os.urandom(16)
key = hashlib.sha256(b'mypassword123').digest()
plaintext = (b'the password is totally not secure')
cipher = AES.new(key, AES.MODE_CFB, iv)
ciphertext = iv + cipher.encrypt(plaintext)
print (ciphertext)
print ("IV = ",iv)
ciphertext = ciphertext.split(iv)
ciphertext = ciphertext[1]
cipher2 = AES.new(key, AES.MODE_CFB, iv)
plaintext = cipher2.decrypt(ciphertext)
print (plaintext)

详情见pycrypto