首页 > 代码库 > Pycrypto实现AES加解密
Pycrypto实现AES加解密
JS实现AES过程。
function getAesString(str,keyObj) { var lengthKeyObj = keyObj||get_rand_key(0); var key = CryptoJS.enc.Hex.parse(lengthKeyObj.rand_key); var iv = CryptoJS.enc.Latin1.parse("1234567890abcdef"); var encrypted = CryptoJS.AES.encrypt(str, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); var cipher_text = encrypted.ciphertext.toString(); return lengthKeyObj.key_index + cipher_text;}
以下通过python pycrypto库实现
python 2.7没有pycrypto标准库,需要另外安装。
#pkcs7BS = AES.block_sizepad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)unpad = lambda s : s[0:-ord(s[-1])] key = os.urandom(16) #randomtext = ‘password‘ cipher = AES.new(key)encrypted = cipher.encrypt(pad(text)).encode(‘hex‘)print encrypted‘decrypted = unpad(cipher.decrypt(encrypted.decode(‘hex‘)))print decrypted
修改代码如下:
BS = AES.block_sizepad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)unpad = lambda s : s[0:-ord(s[-1])]org_pass = ‘1234567890abcdef‘ mode = AES.MODE_CBCiv = ‘1234567890abcdef‘cipher = AES.new( aes_rand_key.decode(‘hex‘),mode,iv)encrypted = cipher.encrypt(pad(org_pass)).encode(‘hex‘)decrypted = unpad(cipher.decrypt(encrypted.decode(‘hex‘)))print decrypted login_pass = rand_key2 + encrypted
Pycrypto实现AES加解密
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。