简体   繁体   English

Java SHA1WithRSA使用openssl命令行

[英]Java SHA1WithRSA using openssl command line

I have the following piece of Java code that I would like to convert to a shell script using openssl command line tool: 我有以下一段Java代码,我想使用openssl命令行工具将其转换为shell脚本:

java.security.Signature sig = java.security.Signature.getInstance("SHA1WithRSA");
sig.initSign(privateKey);
sig.update(data);
byte[] signatureBytes = sig.sign();

So far I have tried the following: 到目前为止,我尝试了以下内容:

openssl dgst -sha1 -binary < data.der > data.hash
openssl rsautl -sign -inkey private.key -keyform pem -in data.hash -out data.rsa

However it does not produce the same output. 但是它不会产生相同的输出。 I guess it might have something to do with formats or padding etc. What do I need to do to correct the openssl script? 我想它可能与格式或填充等有关。我需要做些什么来纠正openssl脚本?

Both codes above produce a repeatable result but the result is different between java and the openssl shell script. 上面的两个代码都产生了可重复的结果,但java和openssl shell脚本之间的结果是不同的。

All suggestions are appreciated. 所有建议表示赞赏。

Kind regards Jens 亲切的问候Jens

I actually found the answer myself at last. 我终于找到了自己的答案。

The following openssl command will perform SHA1WithRSA and generates the same result as the Java code: 以下openssl命令将执行SHA1WithRSA并生成与Java代码相同的结果:

openssl sha1 -sign private.key -out data.rsa data.der

As simple as that, but it was quite hard to find on the web 就这么简单,但在网上很难找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM