java - Crypt::RSA (Perl) 和 java.security.Signature (Java) 之间的交互

标签 java rsa signature verification

我想使用 RSA key 对对文件进行签名。为此,我有这个 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() 总是报告 false。我已经检查过这些事情:

  • datadataSignature 已正确读取
  • PUBLICKEY_MODPUBLICKEY_EXP 正确
  • getPublicKey() 返回具有正确属性的 PublicKey
  • 私钥和公钥是同一对的一部分

有人知道如何正确验证文件吗? signature 实例是否正确?

最佳答案

第一个可能出现问题的线索是,您从未告诉 Perl 使用什么哈希函数,而是告诉 Java 使用 SHA256。在 Perl 方面您有很多工作要做。另外,Crypt::RSA 的默认填充方案似乎是 PSS,而 Java 的默认填充方案是 PKCSv1.5

关于java - Crypt::RSA (Perl) 和 java.security.Signature (Java) 之间的交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9795118/

相关文章:

java - 如何循环设置按钮布局参数?

python - RSA实现解密/加密

c# - 将 RSACryptoServiceProvider RSA XML key 转换为 PKCS8

encryption - RSA可以同时用作加密和签名吗?

javascript - 有什么方法可以复制 div 的呈现 HTML?

linux - 在非 Windows 平台上签署 PowerShell 脚本?

java - 将War文件从64位机器编译为32位

java - 将 char 数组添加到 HashSet

java - 键为字符串、值为包含元素迭代问题的 ArrayList 的 Hashmap

c# - .NET Core 2.0 RSA PlatformNotSupportedException