【问题标题】:How to make “MessageDigest SHA-256 and Signature RSASSA-PSS” equivalent to “Signature SHA256withRSA/PSS ”如何使“MessageDigest SHA-256 and Signature RSASSA-PSS”等同于“Signature SHA256withRSA/PSS”
【发布时间】:2020-04-17 03:27:53
【问题描述】:

我想用 SHA256withRSA/PSS 签名分两步,首先我散列一条消息,然后用 RSASSA-PSS 对摘要进行签名

    byte[] document =   {0, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1};
    MessageDigest digestor256 = MessageDigest.getInstance("SHA256", "BC");
    byte[] documentDigest256 = digestor256.digest(document);

    DigestAlgorithmIdentifierFinder hashAlgorithmFinder = new faultDigestAlgorithmIdentifierFinder();
    AlgorithmIdentifier hashingAlgorithmIdentifier256 = hashAlgorithmFinder.find("SHA256");

    DigestInfo digestInfo2 = new DigestInfo(hashingAlgorithmIdentifier256, documentDigest256);
    Signature s2 = Signature.getInstance("NONEwithRSASSA-PSS", "BC");
    MGF1ParameterSpec mgfParam = new MGF1ParameterSpec("SHA256");
    PSSParameterSpec pssParam = new PSSParameterSpec("SHA256", "MGF1", mgfParam, 32, 1);
    s.setParameter(pssParam);
    s.initSign(keyPair.getPrivate());
    s.update(digestInfo2.getEncoded());
    byte[] signature = s.sign();

但我使用 SHA256withRSA/PSS 无法验证

    Signature ver = Signature.getInstance("SHA256withRSA/PSS", "BC");
    ver.setParameter(pssParam);
    ver.initVerify(keyPair.getPublic());
    ver.update(document);
    boolean re = ver.verify(signature);

我需要一些帮助,谢谢你的帮助。

【问题讨论】:

    标签: java rsa signature


    【解决方案1】:

    不要做 DigestInfo。 RSASSA-PKCS1v1_5 签名使用在 ASN.1 DER DigestInfo 中编码散列的步骤,但 RSASSA-PSS 签名不这样做;请参阅 RFC 3447 或 8017。此外,您不需要使用组合算法在版本上指定参数,因为默认值已经正确,尽管冗余地这样做并没有什么坏处。示例修改为使用我的密钥对,并输出到控制台:

        KeyStore ks = KeyStore.getInstance("jks"); ks.load(new FileInputStream(args[0]), args[1].toCharArray());
        PrivateKey prv = (PrivateKey)ks.getKey(args[2], args[1].toCharArray()); 
        PublicKey pub = ks.getCertificate(args[2]).getPublicKey();
    
        byte[] document =   {0, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1};
        MessageDigest digestor256 = MessageDigest.getInstance("SHA256", "BC");
        byte[] documentDigest256 = digestor256.digest(document);
    
        Signature s2 = Signature.getInstance("NONEwithRSASSA-PSS", "BC");
        MGF1ParameterSpec mgfParam = new MGF1ParameterSpec("SHA256");
        PSSParameterSpec pssParam = new PSSParameterSpec("SHA256", "MGF1", mgfParam, 32, 1);
        s2.setParameter(pssParam);
        s2.initSign(prv);
        s2.update(documentDigest256);
        byte[] signature = s2.sign();
    
        Signature ver = Signature.getInstance("SHA256withRSA/PSS", "BC");
        if(false){ ver.setParameter(pssParam); } // can enable if desired
        ver.initVerify(pub);
        ver.update(document);
        System.out.println( ver.verify(signature) );
    

    另外,很简单,您拼错了 DefaultDigestAlgorithmIdentifierFinder 并使用 s2s 作为变量名。

    【讨论】:

    • 非常感谢您的回答,我成功解决了这个问题。
    猜你喜欢
    • 2014-05-15
    • 1970-01-01
    • 2014-08-07
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多