首页 > 解决方案 > 如何在 Python 中使用 RSA 私钥(非正常签名​​)加密数据?

问题描述

我想用私钥(不是普通签名)进行 RSA 加密,但 PyCryptodome 似乎无法做到。

我需要使用私钥执行此操作的原因是,我需要获得与不是我编写的 Java 程序相同的结果,该程序错误地使用 javax.crypto.Cipher 和私钥来签署消息......

import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import javax.crypto.Cipher;

...

String deviceKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASC...";
PKCS8EncodedKeySpec localPKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decode(deviceKey).getBytes("UTF-8"));
PrivateKey localPrivateKey = KeyFactory.getInstance("RSA").generatePrivate(localPKCS8EncodedKeySpec);

byte[] hash = MessageDigest.getInstance("SHA-256").digest("test".getBytes());
Cipher localCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
localCipher.init(Cipher.ENCRYPT_MODE, localPrivateKey);
String sign = new String(Base64.encode(localCipher.doFinal(hash)));
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
from Crypto.Hash import SHA256

...

deviceKey = 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASC...'
privateKey = RSA.importKey(deviceKey)
hash = SHA256.new('test'.encode()).digest()
signer = PKCS1_v1_5.new(privateKey)
sign = b64encode(signer.encrypt(hash))

Java程序的结果:

我的 Python 脚本的结果:

标签: pythonrsapycryptopycryptodome

解决方案


您不使用私钥来加密数据。

  • 私钥可以对数据进行签名,可以通过匹配的公钥进行验证。
  • 公钥可以加密数据,可以通过匹配的私钥解密。

如果你真正想要的是签署你的哈希,而不是使用encrypt函数,你应该使用sign函数。

所以,而不是

from Crypto.Cipher import PKCS1_v1_5
PKCS1_v1_5.new(privateKey).encrypt(hash)

你可能想试试

from Crypto.Signature import pkcs1_15
pkcs1_15.new(privateKey).sign(hash)

我写了一篇关于使用 pycryptodome 进行签名/验证的小文,如果你想看看的话。


推荐阅读