Python中DES、AES、RSA加密解密实现,能否详细讲解其原理和步骤?
- 内容介绍
- 文章标签
- 相关推荐
本文共计609个文字,预计阅读时间需要3分钟。
AES加密是一种基本算法,实现AES有多种模式,主要包括ECB、CBC、CFB、OFB和CTR。这里以AES加密中的CBC模式为例,与ECB模式相比,CBC模式需要IV(初始化向量),而ECB模式不需要。以下是AES CBC模式的代码示例:
pythonimport base64from Crypto.Cipher import AES
密钥和明文key=b'1234567890123456'iv=b'1234567890123456'plaintext=b'Hello, AES CBC mode!'
创建AES CBC模式加密对象cipher=AES.new(key, AES.MODE_CBC, iv)
加密ciphertext=cipher.encrypt(plaintext)
将密文转换为base64编码ciphertext_base64=base64.b64encode(ciphertext)
print(ciphertext_base64)
AES加解密
AES 只是个基本算法,实现 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] # 加密函数 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加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题,使用base64编码 return base64.b64encode(b2a_hex(self.ciphertext)).decode('utf-8') # 解密函数 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): """校验RSA加密 使用公钥进行加密""" 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): """校验RSA加密 使用私钥进行解密""" 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长度必须为16/24, ,偏移量ivs self.key = key self.mode = CBC self.iv = iv # 加密函数,如果text不是16的倍数,那就补足为16的倍数 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') # 解密后,去掉补足的空格用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加解密的实现的详细内容,更多关于python des,aes,rsa加解密的资料请关注易盾网络其它相关文章!
本文共计609个文字,预计阅读时间需要3分钟。
AES加密是一种基本算法,实现AES有多种模式,主要包括ECB、CBC、CFB、OFB和CTR。这里以AES加密中的CBC模式为例,与ECB模式相比,CBC模式需要IV(初始化向量),而ECB模式不需要。以下是AES CBC模式的代码示例:
pythonimport base64from Crypto.Cipher import AES
密钥和明文key=b'1234567890123456'iv=b'1234567890123456'plaintext=b'Hello, AES CBC mode!'
创建AES CBC模式加密对象cipher=AES.new(key, AES.MODE_CBC, iv)
加密ciphertext=cipher.encrypt(plaintext)
将密文转换为base64编码ciphertext_base64=base64.b64encode(ciphertext)
print(ciphertext_base64)
AES加解密
AES 只是个基本算法,实现 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] # 加密函数 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加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题,使用base64编码 return base64.b64encode(b2a_hex(self.ciphertext)).decode('utf-8') # 解密函数 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): """校验RSA加密 使用公钥进行加密""" 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): """校验RSA加密 使用私钥进行解密""" 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长度必须为16/24, ,偏移量ivs self.key = key self.mode = CBC self.iv = iv # 加密函数,如果text不是16的倍数,那就补足为16的倍数 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') # 解密后,去掉补足的空格用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加解密的实现的详细内容,更多关于python des,aes,rsa加解密的资料请关注易盾网络其它相关文章!

