git-svn-id: svn+ssh://devel.yobi.be/home/svn/CryptoPlus/trunk@6 49921240-e1b6-4d8d...
[python-cryptoplus.git] / Cipher / AES.py
blob4314c29853c7b35b0c370c8b06bd398dd7befd29
1 import blockcipher
2 import Crypto.Cipher.AES
4 MODE_ECB = 1
5 MODE_CBC = 2
6 MODE_CFB = 3
7 MODE_OFB = 5
8 MODE_CTR = 6
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
16 EXAMPLE:
17 ----------
18 >>> import AES
19 >>> cipher = AES.new('0123456789012345')
20 >>> cipher.encrypt('0123456789012345')
21 '_}\\xf0\\xbf\\x10:\\x8cJ\\xe6\\xfa\\xad\\x99\\x06\\xac;*'
22 >>> cipher.decrypt(_)
23 '0123456789012345'
25 CBC EXAMPLE (plaintext = 3 blocksizes):
26 -----------------------------------------
27 >>> from binascii import hexlify,unhexlify
28 >>> import AES
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'
55 """
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)
61 def _test():
62 import doctest
63 doctest.testmod()
65 if __name__ == "__main__":
66 _test()