python base64编码_JS和Python实现AES算法
1. AES原理
AES算法是典型的對稱加密算法,AES原理可以學習這兩篇文檔:
- 漫畫:什么是AES算法:https://www.toutiao.com/i6783550080784794124/
- AES加密算法的詳細介紹與實現:https://blog.csdn.net/qq_28205153/article/details/55798628
- 或者可以看之前我整理的:AES算法原理
2. 摘要
3.JS實現AES算法
3.1 基于node環境
npm install crypto-js
// test.js,Base64編碼,`node test`執行const CryptoJS = require("crypto-js");var key ="0CoJUm6Qyw8W8jud";var iv = "0102030405060708";function encrypt(text){ return CryptoJS.AES.encrypt(text,CryptoJS.enc.Utf8.parse(key),{ iv:CryptoJS.enc.Utf8.parse(iv), mode:CryptoJS.mode.CBC, padding:CryptoJS.pad.Pkcs7 })}function decrypt(text){ var result = CryptoJS.AES.decrypt(text,CryptoJS.enc.Utf8.parse(key),{ iv:CryptoJS.enc.Utf8.parse(iv), mode:CryptoJS.mode.CBC, padding:CryptoJS.pad.Pkcs7 }) return result.toString(CryptoJS.enc.Utf8)}var text="小瞇嘻";var encoded=encrypt(text)console.log(encoded.toString());console.log(decrypt(encoded))3.2 不需要node環境
導入 aes.js 腳本,也能直接使用
www.jb51.net aes.js4.Python實現AES算法
pip install pycryptodome
pycryptodome 庫是對 pycrypto 庫的擴展
4.1 十六進制ASCII處理加密結果
from Crypto.Cipher import AESfrom binascii import b2a_hex, a2b_hex# 如果text不足16位的倍數就用空格補足為16位# 不同于JS,pycryptodome庫中加密方法不做任何padding,因此需要區分明文是否為中文的情況def add_to_16(text): pad = 16 - len(text.encode('utf-8')) % 16 text = text + pad * chr(pad) return text.encode('utf-8')# 加密函數def encrypt(text, key, mode, iv): text = add_to_16(text) cryptos = AES.new(key, mode, iv) cipher_text = cryptos.encrypt(text) return b2a_hex(cipher_text).decode('utf-8')def decrypt(text, key, mode, iv): cryptos = AES.new(key, mode, iv) text = a2b_hex(text) plain_text = cryptos.decrypt(text) # return plain_text.decode('utf-8') return bytes.decode(plain_text)if __name__ == '__main__': key = '0CoJUm6Qyw8W8jud'.encode('utf-8') mode = AES.MODE_CBC iv = '0102030405060708'.encode('utf-8') text = "小瞇嘻的博客123" e = encrypt(text, key, mode, iv) # 加密 print("加密后:", e) d = decrypt(e, key, mode, iv) # 解密 print("解密后:", d)encode() 和 decode() 方法用于字符串與 bytes 的互相轉換。binascii 模塊包含很多用來方法來轉換二進制和各種ASCII編碼的二進制表示法,其中 b2a_hex() 和 a2b_hex() 方法用于 bytes 與 16進制ASCII 的互相轉換。
Python的內置函數 bytes() 可以將字符串str類型轉換成bytes類型,必須明確encoding的參數,不可省略。而如果字符串內容都是 ASCII 字符,則可以通過直接在字符串之前添加 'b' 來構建字節串值,就能直接將一個字符串轉換成 bytes 對象。同樣的,bytes轉為字符串,python3中的內置函數ascii()返回一個字符串對象,如果參數中有非ascii字符,會用 u,U,x 來替代。
encrypt() 和 decrypt() 方法返回的結果是bytes類型。因為輸出的bytes中的字節不一定能與ascii字符集對應的上,因此先將bytes轉換為16進制的ASCII,便于保存輸出結果。
4.2 Base64編碼加密結果
python中的基本使用
# 編碼>>> base64.b64encode(b'/x01') # 想象它是一張圖片,編碼成 base64 之后,就能進行傳輸b'L3gwMQ=='# 解碼>>> base64.b64decode(b'L3gwMQ==') # 傳輸成功之后,在把解碼回來變成一張圖片b'/x01'前面的AES算法中,如果將加密結果轉成Base64位,
import base64from Crypto.Cipher import AESfrom binascii import b2a_hex, a2b_hex# 如果text不足16位的倍數就用空格補足為16位# 不同于JS,pycryptodome庫中加密方法不做任何padding,因此需要區分明文是否為中文的情況def add_to_16_cn(text): pad = 16 - len(text.encode('utf-8')) % 16 text = text + pad * chr(pad) return text.encode('utf-8')# 加密函數def encrypt(text, key, mode, iv): text = add_to_16_cn(text) cryptos = AES.new(key, mode, iv) cipher_text = cryptos.encrypt(text) # return b2a_hex(cipher_text).decode('utf-8') return base64.b64encode(cipher_text).decode('utf-8') #base編碼def decrypt(text, key, mode, iv): cryptos = AES.new(key, mode, iv) # text = a2b_hex(text) text = base64.b64decode(text) #base64解碼 plain_text = cryptos.decrypt(text) return plain_text.decode('utf-8')if __name__ == '__main__': key = '0CoJUm6Qyw8W8jud'.encode('utf-8') mode = AES.MODE_CBC iv = '0102030405060708'.encode('utf-8') text = "小瞇嘻的博客123" e = encrypt(text, key, mode, iv) # 加密 print("加密后:", e) d = decrypt(e, key, mode, iv) # 解密 print("解密后:", d)5.結果驗證
驗證一下,JS與python實現的AES算法,結果一致,且能兼容中文的。
AES_JS.png
AES_python.png
作者:小瞇嘻鏈接:https://www.jianshu.com/p/865f3e570780來源:簡書著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
總結
以上是生活随笔為你收集整理的python base64编码_JS和Python实现AES算法的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 水泡疹怎么治
- 下一篇: AGON AG275QXL 英雄联盟联名
