http://www.voidspace.org.uk/python/modules.shtml#pycrypto 有 windows 下编译好的包,直接安装就可以。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import hashlib from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex >>> def encrypt(key,text): # 密钥key 长度必须为16(AES-128), 24(AES-192),或者32 (AES-256)Bytes 长度 # 所以直接将用户提供的 key md5 一下变成32位的。 key_md5 = hashlib.md5(key).hexdigest() # AES.MODE_CFB 是加密分组模式,详见 http://blog.csdn.net/aaaaatiger/article/details/2525561 # b'0000000000000000' 是初始化向量IV ,16位要求,可以看做另一个密钥。在部分分组模式需要。 # 那个 b 别漏下,否则出错。 cipher = AES.new(key_md5,AES.MODE_CFB,b'0000000000000000') # AES 要求需要加密的内容长度为16的倍数,当密文长度不够的时候用 '\0' 不足。 ntext = text + ('\0' * (16-(len(text)%16))) # b2a_hex 转换一下,默认加密后的字符串有很多特殊字符。 return b2a_hex(cipher.encrypt(ntext)) >>> def decrypt(key,text): key_md5 = hashlib.md5(key).hexdigest() cipher = AES.new(key_md5,AES.MODE_CFB,b'0000000000000000') t=cipher.decrypt(a2b_hex(text)) return t.rstrip('\0') >>> encrypt('111','012340000000000000000000000000000000000000056') 'a37e0dda14a9678fcc82b8e16387c498c7206122195b1b91269a637322d776c48a28342d24b31879a35a0e77480a1dab' >>> decrypt('111','a37e0dda14a9678fcc82b8e16387c498c7206122195b1b91269a637322d776c48a28342d24b31879a35a0e77480a1dab') '012340000000000000000000000000000000000000056' >>> |