Python写一个RSA加密解密
时间: 2023-09-22 18:06:20 浏览: 118
### 回答1:
好的,以下是一份使用 Python 实现 RSA 加密解密的示例代码:
```
import rsa
# RSA加密
def rsa_encrypt(plaintext, pub_key):
# 加密
ciphertext = rsa.encrypt(plaintext.encode(), pub_key)
# 返回密文
return ciphertext
# RSA解密
def rsa_decrypt(ciphertext, pri_key):
# 解密
plaintext = rsa.decrypt(ciphertext, pri_key).decode()
# 返回明文
return plaintext
# 生成公钥、私钥
(pub_key, pri_key) = rsa.newkeys(512)
# 待加密的明文
plaintext = "RSA encryption test"
# 调用RSA加密函数,进行加密
ciphertext = rsa_encrypt(plaintext, pub_key)
# 调用RSA解密函数,进行解密
decrypted_plaintext = rsa_decrypt(ciphertext, pri_key)
# 输出结果
print("待加密的明文:", plaintext)
print("加密后的密文:", ciphertext)
print("解密后的明文:", decrypted_plaintext)
```
注意:本代码仅供参考,实际使用中可能需要对代码进行一定的修改。
### 回答2:
RSA是一种非对称加密算法,它通过生成一对密钥(公钥和私钥)来实现加密和解密。我们可以使用Python来编写一个简单的RSA加密解密示例。
首先,我们需要安装一个第三方库,PyCryptodome,在命令行中运行以下命令进行安装:
```python
pip install pycryptodome
```
然后,我们可以编写一个Python脚本来实现RSA加密解密。以下是一个简单的示例:
```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()
# 写入密钥对文件中
with open('private.pem', 'wb') as f:
f.write(private_key)
with open('public.pem', 'wb') as f:
f.write(public_key)
# 加载密钥对
private_key = RSA.import_key(open('private.pem').read())
public_key = RSA.import_key(open('public.pem').read())
# 加密
message = 'Hello, World!'
cipher = PKCS1_OAEP.new(public_key)
encrypted_message = cipher.encrypt(message.encode())
# 解密
cipher = PKCS1_OAEP.new(private_key)
decrypted_message = cipher.decrypt(encrypted_message).decode()
print('加密后的消息:', encrypted_message)
print('解密后的消息:', decrypted_message)
```
上述代码首先生成了一个2048位的RSA密钥对,并将私钥和公钥分别写入名为`private.pem`和`public.pem`的文件中。然后加载密钥对,将明文消息加密,再通过私钥解密,最后打印出加密后和解密后的消息。
请注意,这只是一个简单的示例,实际使用中还需要考虑更多的安全性和加密机制。
### 回答3:
RSA是一种常用的非对称加密算法,能够提供安全的数据传输和存储。在Python中,我们可以使用`rsa`库来实现RSA加密解密的功能。
首先,我们需要安装`rsa`库。可以通过在终端或命令提示符中运行`pip install rsa`来安装。
接下来,我们可以使用以下代码示例来进行RSA加密和解密:
```python
import rsa
# 生成RSA的公钥和私钥
(public_key, private_key) = rsa.newkeys(2048)
# 要加密的明文
message = "Hello, World!"
# 使用公钥加密明文
encrypted_message = rsa.encrypt(message.encode(), public_key)
# 使用私钥解密密文
decrypted_message = rsa.decrypt(encrypted_message, private_key).decode()
# 打印加密前和解密后的结果
print("加密前的明文:", message)
print("解密后的明文:", decrypted_message)
```
在上述代码中,我们首先使用`rsa.newkeys()`函数生成了一个2048位的RSA公钥和私钥对。然后,我们将要加密的明文转化为字节类型,并使用`rsa.encrypt()`函数通过公钥对明文进行加密。最后,我们使用私钥对密文进行解密,并将解密后的结果转化为字符串类型。
通过运行以上代码,我们可以看到加密前的明文和解密后的明文是相同的。
这就是使用Python编写一个RSA加密解密的简单示例。使用`rsa`库可以方便快捷地实现RSA加密解密的功能。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![apk](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)