我正在尝试在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
算法?
非常感谢。
答案 0 :(得分:1)
在Python上,您正在使用PKCS#1 v1.5填充进行签名。在Java上,您正在使用PSS。当您使用不同的方案时,很自然,这两个不会产生相同的输出。通常,建议在v1.5或更高版本上使用PSS方案。
我不是Python专家,但是在互联网上快速浏览之后,也许Hazmat加密库可以在Python(https://cryptography.io/en/latest/hazmat/primitives/asymmetric/rsa/)上为您提供帮助:
new Question("What colour is the sky?",
new string[] { "A. Pink", "B. Blue", "C. Purple", "D.Yellow" },
Question.multipleChoice, B );
编辑:如果危险品不适合您,请在此处查看批准的答案:PKCS1_PSS sign() method