首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >密码穹顶官方示例

密码穹顶官方示例
EN

Stack Overflow用户
提问于 2022-10-14 21:42:41
回答 1查看 34关注 0票数 0

我正在尝试从https://pycryptodome.readthedocs.io/en/latest/src/examples.html中更改一个正式的示例

并适应我正在做的一个小小的加密项目:

代码语言:javascript
复制
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES, PKCS1_OAEP

file_in = open("encrypted_data.bin", "rb")

private_key = RSA.import_key(open("private.pem").read())

enc_session_key, nonce, tag, ciphertext = \
   [ file_in.read(x) for x in (private_key.size_in_bytes(), 16, 16, -1) ]

在我的示例文件中,encrypted_data.bin (file_in = open("encrypted_data.bin","rb"))将是一个字符串变量,而不是文件。

如何在下面的循环中使用字符串变量来代替file_in

代码语言:javascript
复制
enc_session_key, nonce, tag, ciphertext = \
   [ **file_in.read(x)** for x in (private_key.size_in_bytes(), 16, 16, -1) ]
EN

回答 1

Stack Overflow用户

发布于 2022-10-14 21:48:37

只需将文件读取命令替换为字符串即可。例如:

代码语言:javascript
复制
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES, PKCS1_OAEP

encrypted_data = "my_data_str"

private_key = RSA.import_key("my_private_key_str")

enc_session_key, nonce, tag, ciphertext = \
   [ encrypted_data  for x in (private_key.size_in_bytes(), 16, 16, -1) ]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74075104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档