【问题标题】:Convert java.security "NONEwithRSA" signature into BouncyCastle lightweight API将 java.security "NONEwithRSA" 签名转换为 BouncyCastle 轻量级 API
【发布时间】:2014-02-19 09:28:29
【问题描述】:

我需要将 Java 应用程序转换为 C#,因此需要从 java.security API 迁移到 BouncyCastle 轻量级 API。

我的工作代码 (java.security) 如下所示:

private byte[] computeSignature(byte[] message, PrivateKey key) {
    Signature signature = Signature.getInstance("NONEwithRSA");
    signature.initSign(privateKey);
    signature.update(message);
    return signature.sign();
}

这是我的验证:

private void verifySignature(byte[] signature, byte[] message, PublicKey publicKey) {
    Signature signature = Signature.getInstance("NONEwithRSA");
    signature.initVerify(publicKey);
    signature.update(message);
    System.out.println(signer.verify(result) ? "OK" : "FAIL");
}

现在我正在尝试像这样将其迁移到 BC:

  1. NONEwithRSA 算法不存在的问题(不知道如何添加)

    private byte[] computeSignature(byte[] message, AsymmetricKeyParameter key) {
        AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("NONEwithRSA");
        AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
        ContentSigner signer = new BcRSAContentSignerBuilder(sigAlgId, digAlgId).build(key);
        signer.getOutputStream().write(Arrays.copyOf(message, message.length), 0, message.length);
        byte[] signature = signer.getSignature();
    }
    
  2. 没有提供好的签名

    private byte[] computeSignature(byte[] message, AsymmetricKeyParameter privateKey) {
        Signer signer = new GenericSigner(new RSAEngine(), new NullDigest());
        signer.init(true, privateKey);
        signer.update(message, 0, message.length);
        return signer.generateSignature();
    }
    

你有什么建议吗?或者甚至可以将NONEwithRSA 算法迁移到 BC LW API 中?我假设我需要编写自己的签名者,但作为 BC 的新手和 BC 文档,我无法自己处理。

【问题讨论】:

  • 嗨leizeQ,请使用常用标签(有很多关注者)。至少尝试包含编程语言。 lightweight 确实尊重它的名字:它有 2 个追随者,而且它不仅仅针对充气城堡。

标签: java cryptography rsa bouncycastle


【解决方案1】:

试试这个:

RSABlindedEngine engine = new RSABlindedEngine();
PKCS1Encoding paddedEngine = new PKCS1Encoding(engine);
paddedEngine.init(true, privateKey);
return paddedEngine.processBlock(message, 0, message.length);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    相关资源
    最近更新 更多