2017-05-23 94 views
0

嗨,所以在我得到解密字符串前面的IV(我相信它是解密值)之前。現在,我沒有得到的字符串...我如何使這項工作,現在一直試圖小時......PyCrypto沒有正確打印

我的代碼:

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) 

回答

0

encrypt將改變cipher,你必須做出一個新的一個; 和str將改變byterepr(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