gpt4 book ai didi

python - 我如何知道 RSA 算法中我的公钥和私钥是什么?

转载 作者:太空宇宙 更新时间:2023-11-03 19:53:26 25 4
gpt4 key购买 nike

我编写了一个程序来加密文本文件中的数据,但我不太明白一些事情。

我使用 RSA 加密算法和 pycryptodome 包。

我首先不明白的是:PKCS1_OAEPRSA是非对称加密,那么为什么我们需要另一个基于RSA的算法? - 我不明白的另一件事是我如何识别我的公钥和我的私钥,人们说 - RSA.generate() 同时创建 publicprivate key,但我如何才能看到它们呢?我不明白的第三件事是当我将关键参数放入 new 时,new 意味着什么,new 是做什么的?

import os
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP

desktop = os.path.join(os.environ['USERPROFILE'], 'Desktop')
key = RSA.generate(2048) # 2048 bit key
cipher = PKCS1_OAEP.new(key)

if os.path.exists(os.path.join(desktop, 'newfile.txt')):
with open(os.path.join(desktop, 'newfile.txt'), 'rb') as f:
if not f.readable():
pass
else:
plainText = f.read()
cipherText = cipher.encrypt(plainText)
print(f'Encrypted Text: {cipherText}')
else:
print('File is not exists!')

最佳答案

如果我想用外行的话来说,您需要一个随机生成的 key 来创建RSA公钥/私钥对。因此,key = RSA.generate(2048) 正在生成一个随 secret 钥,以便您可以使用它来创建 PKCS1_OAEP 公钥/私钥一对。

对于您询问有关访问公钥/私钥的问题的另一部分,您可以使用如下代码:

key = RSA.generate(2048)
private_key = key.export_key()
file_out = open("private.pem", "wb")
file_out.write(private_key)

public_key = key.publickey().export_key()
file_out = open("receiver.pem", "wb")
file_out.write(public_key)

Quoted from documentation

关于python - 我如何知道 RSA 算法中我的公钥和私钥是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59688875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com