介绍一下python rsa加密算法的代码
时间: 2024-05-13 21:11:57 浏览: 6
Python中使用rsa加密算法可以使用第三方库pycryptodome,以下是一个简单的示例代码:
```python
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
# 生成密钥对
key = RSA.generate(2048)
# 获取公钥和私钥
private_key = key.export_key()
public_key = key.publickey().export_key()
# 加密数据
cipher = PKCS1_OAEP.new(RSA.import_key(public_key))
encrypted_data = cipher.encrypt(b"Hello, World!")
# 解密数据
cipher = PKCS1_OAEP.new(RSA.import_key(private_key))
decrypted_data = cipher.decrypt(encrypted_data)
print(decrypted_data.decode())
```
这段代码先是生成了一个2048位的RSA密钥对,然后使用公钥加密了一段文本,再使用私钥解密得到原始文本。需要注意的是,这里使用的是PKCS1_OAEP填充方式,如果需要使用其他填充方式需要修改相应参数。
相关问题
rsa加密算法python代码
以下是一个简单的 Python 实现 RSA 加密算法的代码:
```python
import random
import math
# 生成大素数
def generate_prime_number(bit_length):
while True:
p = random.getrandbits(bit_length)
if is_prime(p):
return p
# 判断是否为素数
def is_prime(n):
if n <= 3:
return n > 1
elif n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
# 求最大公约数
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
# 扩展欧几里得算法
def extended_euclidean_algorithm(a, b):
if b == 0:
return (a, 1, 0)
else:
d, x, y = extended_euclidean_algorithm(b, a % b)
return (d, y, x - (a // b) * y)
# 生成公钥和私钥
def generate_key_pair(bit_length):
p = generate_prime_number(bit_length // 2)
q = generate_prime_number(bit_length // 2)
n = p * q
phi_n = (p - 1) * (q - 1)
e = random.randint(2, phi_n - 1)
while gcd(e, phi_n) != 1:
e = random.randint(2, phi_n - 1)
d = extended_euclidean_algorithm(e, phi_n)[1] % phi_n
return ((n, e), (n, d))
# RSA 加密
def rsa_encrypt(plaintext, public_key):
n, e = public_key
ciphertext = [pow(ord(char), e, n) for char in plaintext]
return ciphertext
# RSA 解密
def rsa_decrypt(ciphertext, private_key):
n, d = private_key
plaintext = [chr(pow(char, d, n)) for char in ciphertext]
return ''.join(plaintext)
# 示例
plaintext = 'Hello, world!'
public_key, private_key = generate_key_pair(1024)
ciphertext = rsa_encrypt(plaintext, public_key)
decrypted_text = rsa_decrypt(ciphertext, private_key)
print('明文: %s' % plaintext)
print('公钥: %s' % str(public_key))
print('私钥: %s' % str(private_key))
print('密文: %s' % str(ciphertext))
print('解密后的明文: %s' % decrypted_text)
```
在这个实现中,我们使用了 Python 标准库中的 `random` 和 `math` 模块。该代码生成了一个 1024 位的 RSA 密钥对,使用公钥加密和私钥解密了一个字符串,并最终输出了加密前后的内容。注意,这个实现并不是最优化的,可能存在一些性能瓶颈和安全问题,仅供参考。
PythonRSA加密解密算法
PythonRSA是一个用于加密和解密数据的Python库,它基于RSA算法。RSA是一种非对称加密算法,使用公钥加密数据,私钥解密数据。下面是PythonRSA加密解密算法的介绍:
1. 生成密钥对:使用PythonRSA可以生成RSA密钥对,包括公钥和私钥。公钥用于加密数据,私钥用于解密数据。
2. 加密数据:使用公钥对需要加密的数据进行加密。加密后的数据只能使用私钥进行解密。
3. 解密数据:使用私钥对加密后的数据进行解密,还原为原始数据。
PythonRSA提供了简单易用的接口来实现加密和解密操作。你可以使用以下代码示例来实现RSA加密解密:
```python
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
# 生成RSA密钥对
key = RSA.generate(2048)
private_key = key.export_key()
public_key = key.publickey().export_key()
# 加载公钥和私钥
public_key = RSA.import_key(public_key)
private_key = RSA.import_key(private_key)
# 加密数据
cipher = PKCS1_OAEP.new(public_key)
encrypted_data = cipher.encrypt(b"Hello, World!")
# 解密数据
cipher = PKCS1_OAEP.new(private_key)
decrypted_data = cipher.decrypt(encrypted_data)
print(decrypted_data.decode())
```