python實(shí)現(xiàn)AES加密與解密
AES加密方式有五種:ECB, CBC, CTR, CFB, OFB
從安全性角度推薦CBC加密方法,本文介紹了CBC,ECB兩種加密方法的python實(shí)現(xiàn)
python 在 Windows下使用AES時(shí)要安裝的是pycryptodome 模塊
pip install pycryptodome
python 在 Linux下使用AES時(shí)要安裝的是pycrypto模塊
pip install pycrypto
CBC加密需要一個(gè)十六位的key(密鑰)和一個(gè)十六位iv(偏移量)
ECB加密不需要iv
AES CBC 加密的python實(shí)現(xiàn)
from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex # 如果text不足16位的倍數(shù)就用空格補(bǔ)足為16位 def add_to_16(text): if len(text.encode('utf-8')) % 16: add = 16 - (len(text.encode('utf-8')) % 16) else: add = 0 text = text + ('\0' * add) return text.encode('utf-8') # 加密函數(shù) def encrypt(text): key = '9999999999999999'.encode('utf-8') mode = AES.MODE_CBC iv = b'qqqqqqqqqqqqqqqq' text = add_to_16(text) cryptos = AES.new(key, mode, iv) cipher_text = cryptos.encrypt(text) # 因?yàn)锳ES加密后的字符串不一定是ascii字符集的,輸出保存可能存在問題,所以這里轉(zhuǎn)為16進(jìn)制字符串 return b2a_hex(cipher_text) # 解密后,去掉補(bǔ)足的空格用strip() 去掉 def decrypt(text): key = '9999999999999999'.encode('utf-8') iv = b'qqqqqqqqqqqqqqqq' mode = AES.MODE_CBC cryptos = AES.new(key, mode, iv) plain_text = cryptos.decrypt(a2b_hex(text)) return bytes.decode(plain_text).rstrip('\0') if __name__ == '__main__': e = encrypt("hello world") # 加密 d = decrypt(e) # 解密 print("加密:", e) print("解密:", d)
AES ECB加密的python實(shí)現(xiàn)
""" ECB沒有偏移量 """ from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex def add_to_16(text): if len(text.encode('utf-8')) % 16: add = 16 - (len(text.encode('utf-8')) % 16) else: add = 0 text = text + ('\0' * add) return text.encode('utf-8') # 加密函數(shù) def encrypt(text): key = '9999999999999999'.encode('utf-8') mode = AES.MODE_ECB text = add_to_16(text) cryptos = AES.new(key, mode) cipher_text = cryptos.encrypt(text) return b2a_hex(cipher_text) # 解密后,去掉補(bǔ)足的空格用strip() 去掉 def decrypt(text): key = '9999999999999999'.encode('utf-8') mode = AES.MODE_ECB cryptor = AES.new(key, mode) plain_text = cryptor.decrypt(a2b_hex(text)) return bytes.decode(plain_text).rstrip('\0') if __name__ == '__main__': e = encrypt("hello world") # 加密 d = decrypt(e) # 解密 print("加密:", e) print("解密:", d)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python3.6 實(shí)現(xiàn)AES加密的示例(pyCryptodome)
- Python3.7 基于 pycryptodome 的AES加密解密、RSA加密解密、加簽驗(yàn)簽
- Python實(shí)現(xiàn)AES加密,解密的兩種方法
- python實(shí)現(xiàn)AES加密和解密
- 使用Python進(jìn)行AES加密和解密的示例代碼
- Python使用PyCrypto實(shí)現(xiàn)AES加密功能示例
- python簡(jiǎn)單實(shí)現(xiàn)AES加密和解密
- python實(shí)現(xiàn)aes加密及pycryptodome庫(kù)使用
相關(guān)文章
跟老齊學(xué)Python之集成開發(fā)環(huán)境(IDE)
IDE的全稱是:Integrated Development Environment,簡(jiǎn)稱IDE,也稱為Integration Design Environment、Integration Debugging Environment,翻譯成中文叫做“集成開發(fā)環(huán)境”,在臺(tái)灣那邊叫做“整合開發(fā)環(huán)境”。2014-09-09python無法引用另一個(gè)文件夾的py文件問題及解決
這篇文章主要介紹了python無法引用另一個(gè)文件夾的py文件問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08淺談pandas用groupby后對(duì)層級(jí)索引levels的處理方法
今天小編就為大家分享一篇淺談pandas用groupby后對(duì)層級(jí)索引levels的處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11Python 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的web服務(wù)器
這篇文章主要介紹了Python 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的web服務(wù)器的方法,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2021-01-01pandas快速處理Excel,替換Nan,轉(zhuǎn)字典的操作
這篇文章主要介紹了pandas快速處理Excel,替換Nan,轉(zhuǎn)字典的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03Python3 常用數(shù)據(jù)標(biāo)準(zhǔn)化方法詳解
這篇文章主要介紹了Python3 常用數(shù)據(jù)標(biāo)準(zhǔn)化方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03