java签名:私钥和证书验证失败

omvjsjqw  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(329)

我正在尝试使用rsa-sha256、私钥和证书对数据进行签名和验证:
privatekey是私钥
publickey通过cert.getpublickey()来自cert;

protected String sign(String plainText, PrivateKey privateKey, PublicKey publicKey, java.security.cert.Certificate certificate) throws Exception {
        //Sign
        Signature privateSignature = Signature.getInstance("SHA256withRSA");
        privateSignature.initSign(privateKey);
        privateSignature.update(plainText.getBytes(StandardCharsets.UTF_8));
        byte[] signature = privateSignature.sign();
        String result = Base64.getEncoder().encodeToString(signature);
        //Verify
        privateSignature.initVerify(publicKey);
        privateSignature.update(signature);
        boolean bool = privateSignature.verify(signature);
        System.out.println("VERIFIED: "+bool); //Always false
        return result;
    }

问题是,我总是弄错。
尽管我在这里测试了相同的证书和私钥:
https://www.sslshopper.com/certificate-key-matcher.html
它说我的私钥和证书匹配。
这里有什么问题?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题