使用rsa公钥加密文本消息

2024-03-29 07:50:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我的客户给了我一个公钥,我想给他发一条用他的公钥加密的短信。公钥的扩展名为.pub。在

我正试图在bash中通过openssl命令和python使用pycrypto模块来实现这一点,但运气不佳。我是一个没有密码学经验的新手。在

我该怎么走这个。谢谢提前

public_key


Tags: 模块命令bash客户经验public短信密码学
1条回答
网友
1楼 · 发布于 2024-03-29 07:50:54

假设:

  • 你的客户提供的公钥在“键.pub“文件
  • 在运行时从用户获取输入,以便在名为“msg”的变量中加密字符串或文本。在
  • 已安装加密公钥库使用命令“sudo pip install加密公钥

代码:

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5

with open("key.pub", 'r') as f1:
    pubkey = f1.read()

msg = raw_input("Enter String to be encrypted: ")
print("raw string->", msg)

keyPub = RSA.importKey(pubkey) # import the public key
cipher = Cipher_PKCS1_v1_5.new(keyPub)
cipher_text = cipher.encrypt(msg.encode()) # now we have the cipher
print("cipher text->", cipher_text)

文件中密钥的格式:

文件中密钥的格式应该是这样的,

^{pr2}$

相关问题 更多 >