【发布时间】:2017-07-30 04:25:53
【问题描述】:
无辜地,我以为“SHA1withRSA算法”只是简单地用“SHA1”操作plainText,并使用RSA/pkcs1padding来加密“SHA1”的结果。但是直到我写了一些java代码来测试我才发现我错了我的想法。 我使用 RSA publickey 来解密我使用相应的 privatekey 用 "SHA1withRSA algorithm" 签名的签名。但是我发现结果不等于“SHA1(plainText)”,下面是我的java代码:
String plaintext= "123456";
Signature signature=Signature.getInstance("SHA1withRSA",new BouncyCastleProvider());
signature.initSign(pemPrivatekey);
signature.update(plaintext.getBytes());
byte[] sign = signature.sign();
//RSA decode
byte[] bytes = RsaCipher.decryptByRsa(sign, pemPublickey);
String rsaDecodeHex=Hex.toHexString(bytes);
System.out.println(rsaDecodeHex.toLowerCase());
String sha1Hex = Hash.getSha1(plaintext.getBytes());
System.out.println(sha1Hex);
//rsaDecodeHex!=sha1Hex
很容易找到rsaDecodeHex!=sha1Hex,在哪里
rsaDecodeHex=3021300906052b0e03021a050004147c4a8d09ca3762af61e59520943dc26494f8941b
和
sha1Hex=7c4a8d09ca3762af61e59520943dc26494f8941b 。
那么,“SHA1withRSA”中的细节是什么?
【问题讨论】:
-
仅作记录:您明白使用 SHA1 意味着“浪费时间和精力”吗? blog.qualys.com/ssllabs/2014/09/09/… ...当您开始研究密码学时,最好选择推荐在 2017 年使用的算法。SHA1 是...不是。
-
仅作记录:如果您在这里没有得到好的答案;考虑询问security.stackexchange.com
-
好吧,乍一看
rsaDecodeHex似乎是sha1Hex填充了几个前缀字节,但我可能错了。 -
@GhostCat 这显然取决于您使用它的目的,但在此方案中,SHA1 仅用作传统的散列函数,而 RSA 提供保密性。如果你也需要你的哈希是安全的(你可能会这样做),那么 SHA1 绝对不是答案。
-
@biziclop:这是一个签名,RSA 本身不提供保密性。所以SHA1也需要在这种情况下提供一些安全保障。
标签: java encryption rsa digital-signature sha1