Search code examples
rsadigital-signaturesha1bouncycastle

How to sign with Bouncy Castle using RSA with SHA1 and ISO9796-2 scheme 2?


I need to sign the SHA1 hash of a text with RSA and ISO9796-2 scheme 2 padding

Initially I was doing it only with SHA1 with RSA like this:

public static byte[] signer(byte[] data, PrivateKey key) throws Exception {
    Signature signer = Signature.getInstance("SHA1WithRSA", "BC");
    signer.initSign(key);
    signer.update(data);
    return signer.sign();
}

How should I modify the function? It would be easy to just replace "SHA1WithRSA" with another scheme that does what I need but I don't know if it's possible.


Solution

  • I solved this with this code:

    public static byte[] signer(byte[] data, PrivateKey key) throws Exception {
        Signature signer = Signature.getInstance("SHA1withRSA/ISO9796-2", "BC");
        signer.initSign(key);
        signer.update(data);
        return signer.sign();
    }
    

    SHA1withRSA/ISO9796-2 does the trick.

    I'm thankful to David from bouncy castle mailing list for this answer.