2012-09-24 72 views
10

Tôi đã cố gắng triển khai giải mã AES CBC bằng Python. Vì văn bản được mã hóa không phải là bội số của 16byte, nên cần phải có phần đệm. Nếu không có đệm, lỗi này nổi lênĐệm giải mã AES với PKCS5 Python

"Lỗi Loại: chuỗi Odd-length"

Nhưng tôi không thể tìm thấy một tài liệu tham khảo thích hợp để thực hiện PKCS5 trong PyCrypto Python. Có bất kỳ lệnh nào để thực hiện việc này không? Cảm ơn

Sau khi xem xét đề xuất của Marcus, tôi đã làm điều này.

Mục tiêu của tôi thực sự là giải mã thông điệp hex (128byte) bằng mã này. Tuy nhiên, đầu ra là "?:" Rất nhỏ và lệnh unpad sẽ xóa các byte đó. Đây là mã.

from Crypto.Cipher import AES 
BS = 16 
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) 
unpad = lambda s : s[0:-ord(s[-1])] 

class AESCipher: 
    def __init__(self, key): 
    self.key = key 

    def encrypt(self, raw): 
     raw = pad(raw) 
     iv = raw[:16] 
     raw=raw[16:] 
     #iv = Random.new().read(AES.block_size) 
     cipher = AES.new(self.key, AES.MODE_CBC, iv) 
     return (iv + cipher.encrypt(raw)).encode("hex") 

    def decrypt(self, enc): 
     iv = enc[:16] 
     enc= enc[16:] 
     cipher = AES.new(self.key, AES.MODE_CBC, iv) 
     return unpad(cipher.decrypt(enc)) 

mode = AES.MODE_CBC 
key = "140b41b22a29beb4061bda66b6747e14" 
ciphertext = "4ca00ff4c898d61e1edbf1800618fb2828a226d160dad07883d04e008a7897ee2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81"; 
key=key[:32] 
decryptor = AESCipher(key) 
decryptor.__init__(key) 
plaintext = decryptor.decrypt(ciphertext) 
print plaintext 
+1

http://stackoverflow.com/questions/12524994/encrypt-decrypt-using-pycrypto-aes-256/12525165#12525165, các chức năng đệm trong câu trả lời có thể giúp :) – Marcus

Trả lời

18

Bạn cần giải mã giá trị được mã hóa hex của mình trước khi giải mã. Nếu bạn muốn làm việc với các khóa được mã hóa hex, hãy giải mã khóa đó ..

Ở đây, thao tác này sẽ hoạt động.

from Crypto.Cipher import AES 
from Crypto import Random 

BS = 16 
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) 
unpad = lambda s : s[0:-ord(s[-1])] 

class AESCipher: 
    def __init__(self, key): 
     """ 
     Requires hex encoded param as a key 
     """ 
     self.key = key.decode("hex") 

    def encrypt(self, raw): 
     """ 
     Returns hex encoded encrypted value! 
     """ 
     raw = pad(raw) 
     iv = Random.new().read(AES.block_size); 
     cipher = AES.new(self.key, AES.MODE_CBC, iv) 
     return (iv + cipher.encrypt(raw)).encode("hex") 

    def decrypt(self, enc): 
     """ 
     Requires hex encoded param to decrypt 
     """ 
     enc = enc.decode("hex") 
     iv = enc[:16] 
     enc= enc[16:] 
     cipher = AES.new(self.key, AES.MODE_CBC, iv) 
     return unpad(cipher.decrypt(enc)) 

if __name__== "__main__": 
    key = "140b41b22a29beb4061bda66b6747e14" 
    ciphertext = "4ca00ff4c898d61e1edbf1800618fb2828a226d160dad07883d04e008a7897ee2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81" 
    key=key[:32] 
    decryptor = AESCipher(key) 
    plaintext = decryptor.decrypt(ciphertext) 
    print "%s" % plaintext 
+0

này **; ** ở cuối 'ciphertext = ...' không nên ở đó ... – kravietz

Các vấn đề liên quan