本文介绍了将java.security“ NONEwithRSA”转换为签名到BouncyCastle轻量级API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to convert Java app into C# and therefore need to migrate from java.security API into BouncyCastle lightweight API.

我的工作代码(java.security)看起来像这样:

My working code (java.security) looks like this:

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:

Now I am trying to migrate it to BC like this:


  1. 不存在 NONEwithRSA 算法的问题(不确定如何添加它) )

  1. problem with NONEwithRSA algorithm which doesn't exist (not sure how to add it)

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();
}


  • 没有提供良好的签名

  • doesn't provide good signature

    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中?我假设我需要编写自己的Signer,但是作为BC的新手,以及BC文档,我无法独自处理。

    Do you have any suggestions? Or is it even possible to migrate the NONEwithRSA algorithm into BC LW API? I assume that I need to write my own Signer, but as a newb to BC and with the BC documentation I can't handle this on my own.

    推荐答案

    尝试一下:

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

    这篇关于将java.security“ NONEwithRSA”转换为签名到BouncyCastle轻量级API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    10-14 02:23