Pycrypto實現AES加解密

JS實現AES過程。python

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庫實現dom

python 2.7沒有pycrypto標準庫,須要另外安裝。spa

#pkcs7 BS = AES.block_size pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) unpad = lambda s : s[0:-ord(s[-1])] key = os.urandom(16) #random text = 'password' cipher = AES.new(key) encrypted = cipher.encrypt(pad(text)).encode('hex') print encrypted' decrypted = unpad(cipher.decrypt(encrypted.decode('hex'))) print decrypted 

修改代碼以下:code

BS = AES.block_size
pad = 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_CBC iv = '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 
相關文章
相關標籤/搜索