python中RSA算法中使用公钥解密

发布于 2025-01-10 08:59:20 字数 897 浏览 1 评论 0原文

我正在进行许可证检查,我将使用私钥加密字符串文本并使用公钥对其进行解密,我知道这不是标准的解密方式,但我正在尝试以相反的方式为我的项目构建,标准 cryptodome 库在解密时检查私钥,它返回一个错误并说“不是私钥”,所以有办法修复它吗?

public_key = b64decode(pubkey)
public_key = RSA.importKey(public_key )
cipher = PKCS1_v1_5.new(public_key )
plaintext = cipher.decrypt(b64decode(encrpted_data_base64), "Error while decrypting")
print(plaintext)

File "c:\Users\Desktop\license.py", line 17, in <module>
    plaintext = cipher.decrypt(b64decode(encrpted_data_base64), "Error while decrypting")
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\Cryptodome\Cipher\PKCS1_v1_5.py", line 180, in decrypt
    m_int = self._key._decrypt(ct_int)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\Cryptodome\PublicKey\RSA.py", line 156, in _decrypt
    raise TypeError("This is not a private key")
TypeError: This is not a private key

I am making a license check where I will be encrypting the string text with private key and decrypting it with the public key, I know that it is not the standard way of decryption but I am trying to build in reverse manner for my project, the standard cryptodome library checks for private key while decryption and it returns an error and saying "not a private key", so any idea to fix it up?

public_key = b64decode(pubkey)
public_key = RSA.importKey(public_key )
cipher = PKCS1_v1_5.new(public_key )
plaintext = cipher.decrypt(b64decode(encrpted_data_base64), "Error while decrypting")
print(plaintext)

File "c:\Users\Desktop\license.py", line 17, in <module>
    plaintext = cipher.decrypt(b64decode(encrpted_data_base64), "Error while decrypting")
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\Cryptodome\Cipher\PKCS1_v1_5.py", line 180, in decrypt
    m_int = self._key._decrypt(ct_int)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\Cryptodome\PublicKey\RSA.py", line 156, in _decrypt
    raise TypeError("This is not a private key")
TypeError: This is not a private key

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠北森屋 2025-01-17 08:59:20

反向操作没有任何用处,因为您的公钥是公开的。作为加密过程的一部分,它会通过网络广播,以响应请求密钥的请求。任何人都可以提出相同的请求来索取密钥。这意味着如果使用公钥进行解密,任何人都可以使用公钥解密消息,这就是为什么他们不能解密。它们仅用于加密。

也许您想签署该消息?签名的工作方式是在私钥持有者将签名附加到消息后,任何人都可以通过使用公钥和消息,使用 RSA 证明签名的功能来证明附加的签名签名确实是由私钥持有者添加的。

There isn't any use in doing things in reverse because your public key is public. It gets broadcasted over the network as a part of the encryption process in response to a request made asking for the key. Anyone can make that same request to ask for the key. That means anyone would be able to decrypt a message with the public key if public keys were used for decryption which is why they are not. They are only used to encrypt.

Maybe you are looking to sign the message? The way signing works is after a signature has been attached to a message, by the holder of the private key, anyone, through the use of the public key and message, can prove, using a feature of RSA for proving signatures, that the attached signature was indeed added by the holder of the private key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文