python AES 加密解密过程

刚学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:]

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值