#!/usr/bin/env python#-*-coding:utf-8-*-from crypto.cipher import aesfrom binascii import B2a_hex, A2b_hex class Prpc Rypt (): Def __init__ (self,key,iv): Self.key = key Self.iv = IV Self.mode = AES. MODE_CBC self.bs = aes.block_size #补位 Self.pad = lambda s:s + (self.bs-len (s)% self.bs) * CHR (SEL F.bs-len (s)% self.bs) Self.unpad = Lambda S:s[0:-ord (s[-1])] def encrypt (self,text): Text = SE Lf.pad (text) Cryptor = aes.new (SELF.KEY,SELF.MODE,SELF.IV) #目前AES-128 is sufficient for current use ciphertext = Cryptor.enc Rypt (text) #把加密后的字符串转化为16进制字符串 return B2a_hex (ciphertext) #解密后, remove the top-up spaces with strip () to remove the Def decrypt (sel F,text): Cryptor = Aes.new (Self.key,self.mode, iv) Plain_text = Cryptor.decrypt (A2b_hex (text)) retur N Self.unpad (' Plain_text.rstrip ')) if __name__ = = ' __main__ ': IV = "1237635217384736" PC = Prpcrypt (' Jsdfiahdjfie QODK ', iv) #初始化密钥 and IV import SYS E= Pc.encrypt (sys.argv[1]) #加密 d = Pc.decrypt (e) #解密 print "Encrypt:", e print "Decrypt:", D print "Length:", Len (d)
More reference http://stackoverflow.com/questions/12524994/encrypt-decrypt-using-pycrypto-aes-256
The "Go" Python AES algorithm uses IV