2 import Crypto
.Cipher
.AES
10 def new(key
,mode
=blockcipher
.MODE_ECB
,IV
=None):
11 return AES(key
,mode
,IV
)
13 class AES(blockcipher
.BlockCipher
):
14 """AES using pycrypto for algo en pycryptoplus for ciphermode
19 >>> cipher = AES.new('0123456789012345')
20 >>> cipher.encrypt('0123456789012345')
21 '_}\\xf0\\xbf\\x10:\\x8cJ\\xe6\\xfa\\xad\\x99\\x06\\xac;*'
25 CBC EXAMPLE (plaintext = 3 blocksizes):
26 -----------------------------------------
27 >>> from binascii import hexlify,unhexlify
29 >>> key = unhexlify('2b7e151628aed2a6abf7158809cf4f3c')
30 >>> IV = unhexlify('000102030405060708090a0b0c0d0e0f')
31 >>> plaintext1 = unhexlify('6bc1bee22e409f96e93d7e117393172a')
32 >>> plaintext2 = unhexlify('ae2d8a571e03ac9c9eb76fac45af8e51')
33 >>> plaintext3 = unhexlify('30c81c46a35ce411e5fbc1191a0a52ef')
34 >>> cipher = AES.new(key,AES.MODE_CBC,IV)
35 >>> ciphertext = cipher.encrypt(plaintext1 + plaintext2 + plaintext3)
36 >>> hexlify(ciphertext)
37 '7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e22229516'
38 >>> decipher = AES.new(key,AES.MODE_CBC,IV)
39 >>> plaintext = decipher.decrypt(ciphertext)
40 >>> hexlify(plaintext)
41 '6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52ef'
43 OR: supply plaintext as seperate pieces:
44 ------------------------------------------
45 >>> cipher = AES.new(key,AES.MODE_CBC,IV)
46 >>> hexlify( cipher.encrypt(plaintext1 + plaintext2[:-2]) )
47 '7649abac8119b246cee98e9b12e9197d'
48 >>> hexlify( cipher.encrypt(plaintext2[-2:] + plaintext3) )
49 '5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e22229516'
50 >>> decipher = AES.new(key,AES.MODE_CBC,IV)
51 >>> hexlify(decipher.decrypt(ciphertext[:22]))
52 '6bc1bee22e409f96e93d7e117393172a'
53 >>> hexlify(decipher.decrypt(ciphertext[22:]))
54 'ae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52ef'
56 def __init__(self
,key
,mode
,IV
):
57 self
.cipher
= Crypto
.Cipher
.AES
.new(key
)
58 self
.blocksize
= Crypto
.Cipher
.AES
.block_size
59 blockcipher
.BlockCipher
.__init
__(self
,key
,mode
,IV
)
65 if __name__
== "__main__":