Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中使用数字签名算法RSASSA-PSS-2048-SHA256_Python_Rsa_Digital Signature_Sha - Fatal编程技术网

如何在python中使用数字签名算法RSASSA-PSS-2048-SHA256

如何在python中使用数字签名算法RSASSA-PSS-2048-SHA256,python,rsa,digital-signature,sha,Python,Rsa,Digital Signature,Sha,我正试图在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) r

我正试图在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)
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());
}
但是当我们尝试验证生成的签名时,出现了签名不匹配错误

在Java中,代码如下所示:

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)
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文章中,您正在使用PKCS#1 v1.5填充进行签名。在Java部件上,您正在使用PSS。当您使用不同的方案时,这两种方案自然不会产生相同的输出。通常,建议在v1.5以上使用PSS方案

我不是Python专家,但在互联网上快速浏览之后,也许Hazmat crypto库可以在Python()上帮助您:

编辑:如果危险品不适合您,请在此处查看批准的答案: