下载pycryto,点击这里

#!/usr/bin/python
#encoding:utf-8
"""
示例代码
"""
from Crypto.Cipher import AES

key = '0123456789abcdef'
mode = AES.MODE_CBC
iv=key
encryptor = AES.new(key, mode,iv)
text = 'j' * 64 + 'i' * 128
ciphertext = encryptor.encrypt(text)
print ciphertext 	

"""
上例中的key是16位, 还可以是24 或 32 位长度, 其对应为 AES-128, AES-196 和 AES-256.
解密则可以用以下代码进行:
"""
 
#decryptor = AES.new(key, mode)
#plain = decryptor.decrypt(ciphertext)

iv为空时,上述代码会报错“ValueError: IV must be 16 bytes long”,参考如下链接

In AES with CBC mode, the IV isn't anymore an optional parameter.

posted on 2014-01-20 15:21  土豆饼  阅读(682)  评论(0编辑  收藏  举报