Question

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.

Was it helpful?

Solution

Try this:

RSABlindedEngine engine = new RSABlindedEngine();
PKCS1Encoding paddedEngine = new PKCS1Encoding(engine);
paddedEngine.init(true, privateKey);
return paddedEngine.processBlock(message, 0, message.length);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top