Convert java.security “NONEwithRSA” signature into

2019-07-18 22:23发布

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

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

This is my verification:

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

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

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

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.

1条回答
我想做一个坏孩纸
2楼-- · 2019-07-18 22:56

Try this:

RSABlindedEngine engine = new RSABlindedEngine();
PKCS1Encoding paddedEngine = new PKCS1Encoding(engine);
paddedEngine.init(true, privateKey);
return paddedEngine.processBlock(message, 0, message.length);
查看更多
登录 后发表回答