【发布时间】:2012-03-20 21:17:41
【问题描述】:
我想使用 RSA 密钥对签署文件。为此,我有这个 Perl 脚本:
#!/usr/bin/perl
use Crypt::RSA;
my $data = ... # File contents
my $rsa = new Crypt::RSA;
my $key = new Crypt::RSA::Key::Private(Filename => "stackoverflow.priv", Password => "*****");
my $signature = $rsa->sign(Message => $data, Key => $key, Armour => 0);
# Write signature to file
在客户端,我想使用以下 Java 函数来验证文件:
private static final String PUBLICKEY_MOD = "190343051422614110006523776876348493...";
private static String PUBLICKEY_EXP = "65537";
public boolean check() {
byte[] data = ... // Data
byte[] dataSignature = ... // Signature (as calculated in the Perl script)
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initVerify(getPublicKey());
signature.update(data);
return signature.verify(dataSignature);
}
private PublicKey getPublicKey() {
RSAPublicKeySpec spec = new RSAPublicKeySpec(new BigInteger(PUBLICKEY_MOD), new BigInteger(PUBLICKEY_EXP));
KeyFactory factory = KeyFactory.getInstance("RSA");
return factory.generatePublic(spec);
}
但是,check() 总是报告错误。这些东西我已经检查过了:
-
data和dataSignature被正确读取 -
PUBLICKEY_MOD和PUBLICKEY_EXP是正确的 -
getPublicKey()返回具有正确属性的 PublicKey - 私钥和公钥是同一对的一部分
有人知道如何正确验证文件吗? signature 是否正确实例化?
【问题讨论】:
-
非常感谢!我解决了这两个问题,并且效果很好。如果您想添加您的积分作为答案,我会将其标记为解决方案。
-
您能否指出您为使 SHA-256 为 PERL 工作所做的工作,this other question...
-
你做了什么?我被困在同一个位置。试图做出改变,但没有奏效。在 SCALA 中它看起来像
var signature = Signature.getInstance("SHA256withRSA" )和在 PERL 中$publickey->verify_message($signature, $datatosign, 'SHA256', 'v1.5')
标签: java rsa signature verification