python des,aes,rsa加解密的實(shí)現(xiàn)
AES加解密
AES 只是個(gè)基本算法,實(shí)現(xiàn) AES 有幾種模式,主要有 ECB、CBC、CFB 和 OFB CTR,直接上代碼,此處為AES加密中的CBC模式,EBC模式與CBC模式相比,不需要iv。
import base64from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
unpad = lambda s: s[:-ord(s[len(s) - 1:])]
class AES3:
def __init__(self, key):
self.key = key
self.mode = AES.MODE_CBC
self.iv = self.key
def _pad(self, text):
key_len = len(self.key)
pad = text + (key_len - len(text) % key_len) * chr(key_len - len(text) % key_len)
return pad
def _unpad(self, text):
pad = ord(text[-1:])
return text[0:-pad]
# 加密函數(shù)
def encrypt(self, text):
length = 16
count = len(text)
if count % length != 0:
add = length - (count % length)
else:
add = 0
text = text + ('\0' * add)
cryptor = AES.new(self.key.encode("utf8"), self.mode, self.iv.encode("utf8"))
self.ciphertext = cryptor.encrypt(bytes(text, encoding="utf8"))
# AES加密時(shí)候得到的字符串不一定是ascii字符集的,輸出到終端或者保存時(shí)候可能存在問(wèn)題,使用base64編碼
return base64.b64encode(b2a_hex(self.ciphertext)).decode('utf-8')
# 解密函數(shù)
def decrypt(self, text):
decode = base64.b64decode(text)
cryptor = AES.new(self.key.encode("utf8"), self.mode, self.iv.encode("utf8"))
plain_text = unpad(cryptor.decrypt(decode))
return a2b_hex(plain_text) .decode('utf8')
RSA公鑰加密,私鑰解密
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
import base64
# 私鑰
private_key = '''-----BEGIN RSA PRIVATE KEY-----
5353dfggd
-----END RSA PRIVATE KEY-----
'''
# 公鑰
public_key = '''-----BEGIN PUBLIC KEY-----
hfgghftetet
-----END PUBLIC KEY-----'''
def rsa_encrypt(message):
"""校驗(yàn)RSA加密 使用公鑰進(jìn)行加密"""
cipher = Cipher_pkcs1_v1_5.new(RSA.importKey(public_key))
cipher_text = base64.b64encode(cipher.encrypt(message.encode())).decode()
return cipher_text
def rsa_decrypt(text):
"""校驗(yàn)RSA加密 使用私鑰進(jìn)行解密"""
cipher = Cipher_pkcs1_v1_5.new(RSA.importKey(private_key))
retval = cipher.decrypt(base64.b64decode(text), 'ERROR').decode('utf-8')
return retval
DES加解密
from pyDes import *
import base64
class Des3(object):
def __init__(self, key, iv):
# 這里密鑰key長(zhǎng)度必須為16/24, ,偏移量ivs
self.key = key
self.mode = CBC
self.iv = iv
# 加密函數(shù),如果text不是16的倍數(shù)【加密文本text必須為16的倍數(shù)!】,那就補(bǔ)足為16的倍數(shù)
def encrypt(self, text):
des3 = triple_des(self.key, self.mode, self.iv, pad=None, padmode=PAD_PKCS5)
data = des3.encrypt(text)
data = base64.b64encode(data)
return data.decode('utf-8')
# 解密后,去掉補(bǔ)足的空格用strip() 去掉
def decrypt(self, data):
des3 = triple_des(self.key, self.mode, self.iv, pad=None, padmode=PAD_PKCS5)
data = base64.b64decode(data)
text = des3.decrypt(data)
return text.decode('hex')
以上就是python des,aes,rsa加解密的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于python des,aes,rsa加解密的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python神經(jīng)網(wǎng)絡(luò)Xception模型復(fù)現(xiàn)詳解
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)Xception模型復(fù)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
解決python執(zhí)行較大excel文件openpyxl慢問(wèn)題
這篇文章主要介紹了解決python執(zhí)行較大excel文件openpyxl慢問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
Python實(shí)現(xiàn)決策樹(shù)C4.5算法的示例
本篇文章主要介紹了Python實(shí)現(xiàn)決策樹(shù)C4.5算法的示例,詳解的介紹了決策樹(shù)C4.5算法的原理和實(shí)現(xiàn)代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-05-05
python實(shí)現(xiàn)內(nèi)存監(jiān)控系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)內(nèi)存監(jiān)控系統(tǒng),通過(guò)系統(tǒng)命令或操作系統(tǒng)文件獲取到內(nèi)存信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Django自定義模板過(guò)濾器和標(biāo)簽的實(shí)現(xiàn)方法
這篇文章主要介紹了Django自定義模板過(guò)濾器和標(biāo)簽的實(shí)現(xiàn)方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
python刪除列表元素的三種方法(remove,pop,del)
這篇文章主要介紹了python刪除列表元素的三種方法(remove,pop,del),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

