Using SHA1 and RSA with java.security.Signature vs. MessageDigest and Cipher

后端 未结 6 575
鱼传尺愫
鱼传尺愫 2020-11-27 10:48

I\'m trying to understand what the Java java.security.Signature class does. If I compute an SHA1 message digest, and then encrypt that digest using RSA, I get a dif

6条回答
  •  不知归路
    2020-11-27 11:32

    To produce the same results:

    MessageDigest sha1 = MessageDigest.getInstance("SHA1", BOUNCY_CASTLE_PROVIDER);
    byte[] digest = sha1.digest(content);
    DERObjectIdentifier sha1oid_ = new DERObjectIdentifier("1.3.14.3.2.26");
    
    AlgorithmIdentifier sha1aid_ = new AlgorithmIdentifier(sha1oid_, null);
    DigestInfo di = new DigestInfo(sha1aid_, digest);
    
    byte[] plainSig = di.getDEREncoded();
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", BOUNCY_CASTLE_PROVIDER);
    cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    byte[] signature = cipher.doFinal(plainSig);
    

提交回复
热议问题