刚学python,正好项目也有加密这个需求。写了一小段。
解密有点小问题。前面的的16个字节要去掉,也就是IV那个部分。可能,参数没有写对,以后再改了。
'''
Created on Feb 22, 2010
@author: annadaisy
'''
#get AES key
f = open('C://Python26//cipher.txt','r')
keystart = 19
keylength = 16
draftkey = f.read(keystart-1+keylength)
key = draftkey[keystart-1:keystart+keylength]
print 'the key is: ' + key
#get IV
f = open('C://Python26//cipher.txt','r')
IVstart = 49
keylength = 16
draftIV = f.read(IVstart-1+keylength)
IV = draftIV[IVstart-1:IVstart+keylength]
print 'the IV is: ' + IV
#encrypt
from Crypto.Cipher import AES
obj = AES.new(key,AES.MODE_CBC)
plain = 'abcdefghabcdefghooooooooooooooooabcdefghabcdefghoooooooooooooooo'
print 'the len of plain is : '
print len(plain)
print
ciph = obj.encrypt(IV + plain)
print ciph.encode('hex')
print obj.decrypt(ciph)
#这个是后面加的。
decryptplain = obj.decrypt(ciph)
print 'after decrypt: '
print decryptplain[16:]