Python RSA加密解密实现:文本加密示例
以下是使用RSA算法对文本进行加密的Python实现:
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
# 生成RSA密钥对
key = RSA.generate(2048)
# 获取公钥
public_key = key.publickey()
# 加密文本
message = 'Hello, World!'
cipher = PKCS1_OAEP.new(public_key)
ciphertext = cipher.encrypt(message.encode('utf-8'))
# 解密文本
cipher = PKCS1_OAEP.new(key)
plaintext = cipher.decrypt(ciphertext).decode('utf-8')
print('加密前的文本:', message)
print('加密后的文本:', ciphertext)
print('解密后的文本:', plaintext)
输出:
加密前的文本: Hello, World!
加密后的文本: b'\x85k\x9d\x18\x8f\xc5\x8b\xf0P\x89\xad\x1f\x9c\xb4\x87\xa9\xde\x12\x1a\xe5B\x1a\x88\xc0\x9c\x8a\xeb\x1f\x04\x8f\x95\xe3\x8d\xb3\x89\x87\x85\x91\xf5\x0f\xf8\xe4\xbe\x04\x1f\xec\xed\xdd\x8c\x9d\x0f\x8b\x9d\x84\x03\x9d\x98\x0c\x9c\xdb\x9d\xf9\xe3\x7f\x83\xde\xde\x8e\xf2\x1c\xb8\x8f\xb2\x92\xafU\x0b\xf8\t\x1c\x0c\xca\xe4\x8e\x97\xbb\x88\x84\x1a\x8e\x86\x94\x8f\x85\xe4\xd2\x8a\x83\xf9O\x8e\x1e\x9e\x1b\xf9\x11\x1f\x0fI\x0f\x9c\x01\x8d\x1c\xe0U`R\x9f\x8d\x90\x8a\x98\x92\x87\xc9\x8f\x84\xb2\xb6e\xca\xaf\xfe\x0e\x9a\x9c\xe1\x1f\xd4\x8b\xea\xd8\x11c2\xf1\x1a\x91\xfc\x96U\x17S\x8f\xf1\x9f\xce\xcc\xae\x01\x86\x1d\xd9\x1c\x8c\x9c\x8c\xeb\x1a\x8d\xdd\x0c\x1c\x8c\x9d\x1b\xdc\x9d\xd9\x9d\x0c\x8e\x1f\xdd\xdf\xba\x0f\x8c\x18'
解密后的文本: Hello, World!
原文地址: https://www.cveoy.top/t/topic/osKa 著作权归作者所有。请勿转载和采集!