Ho cercato di implementare la decodifica CBC AES in Python. Poiché il testo cifrato non è un multiplo di 16 byte, era necessario il riempimento. Senza imbottitura, questo errore è emersoImbottitura decodifica AES con Python PKCS5
"TypeError: stringa di Odd-length"
ma non riuscivo a trovare un riferimento adeguato per l'attuazione PKCS5 in pycrypto Python. Esistono comandi per implementare questo? Grazie
Dopo aver esaminato il suggerimento di Marcus, l'ho fatto.
Il mio obiettivo è in realtà decodificare un messaggio esadecimale (128 byte) utilizzando questo codice. Tuttavia, l'output è "?:" Che è molto piccolo e il comando unpad sta cancellando quei byte. Questo è il codice.
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
http://stackoverflow.com/questions/12524994/encrypt-decrypt-using-pycrypto-aes-256/12525165#12525165, le funzioni di imbottitura in risposta può aiutare :) – Marcus