【问题标题】:Interaction between Crypt::RSA (Perl) and java.security.Signature (Java)Crypt::RSA (Perl) 和 java.security.Signature (Java) 之间的交互
【发布时间】: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() 总是报告错误。这些东西我已经检查过了:

  • datadataSignature 被正确读取
  • PUBLICKEY_MODPUBLICKEY_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


【解决方案1】:

你的第一个线索是你从来没有告诉 Perl 使用什么哈希函数,但是你告诉 Java 使用 SHA256。在 Perl 方面你有很多工作要做。此外,Crypt::RSA 的默认填充方案似乎是 PSS,而对于 Java,它是 PKCSv1.5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-05
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    相关资源
    最近更新 更多