【发布时间】:2019-04-29 22:03:29
【问题描述】:
我正在尝试在 python2.7 中找到 RSASSA-PSS-2048-SHA256 数字签名算法。
目前我的代码是这样的:
def calc_rsassa_pss_2048_sha256(self, data):
private_key = RSA.importKey(self.private_key)
cipher = PKCS1_v1_5.new(private_key)
h = SHA.new(data)
signature = cipher.sign(h)
return base64.b64encode(signature)
但在我们尝试验证生成的签名时出现签名不匹配错误。
Java 中的代码如下:
public static PrivateKey decodePrivateKey(String privateKeyStr) throws NoSuchAlgorithmException, InvalidKeySpecException {
String privateKeyRaw = trimPrivateKey(privateKeyStr);
byte[] buffer = decodeBase64(privateKeyRaw);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
}
public static String sha256withRSAPSS(String privateKeyStr, String content) throws InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
PrivateKey privateKey = decodePrivateKey(privateKeyStr);
Signature signature = Signature.getInstance("SHA256withRSA/PSS", new BouncyCastleProvider());
signature.initSign(privateKey);
signature.update(content.getBytes(CHARSET));
return encodeBase64(signature.sign());
}
不知道上面的python签名代码有什么问题。或者如何在python2.7中使用RSASSA-PSS-2048-SHA256算法?
非常感谢。
【问题讨论】:
标签: python rsa digital-signature sha