Search code examples
c#.net-2.0

OAEP with SHA1 and MGF1 with BouncyCastle?


I am trying to do RSA/ECB/OAEPWithSHA1AndMGF1Padding with c# using BouncyCastle on .NET Framework 2.0.

I've came down with this:

IAsymmetricBlockCipher engine = new OaepEncoding(new RsaEngine(), new Sha1Digest(), new Sha1Digest(), null);
using (var stream = new StreamReader(publicKey))
{
    var pemReader = new PemReader(stream);
    var pemObj = pemReader.ReadObject();
    var keyPair = (RsaKeyParameters)pemObj;
    engine.Init(true, keyPair);
}
var message = "test";
var data = Encoding.UTF8.GetBytes(message);
var encrypted = engine.ProcessBlock(data, 0, data.Length);

My question is, is that the equivalent to RSA/ECB/OAEPWithSHA1AndMGF1Padding using BouncyCastle and c# or what is the correct way?

I am also in doubt with the parameters here:

IAsymmetricBlockCipher engine = new OaepEncoding(new RsaEngine(), new Sha1Digest(), new Sha1Digest(), null);

I couldn't find a way to define the 2nd Sha1Digest into MGF1 or something.


Solution

  • Have a look at Peter Dettman's GIT

    It solves this with a IBufferedCipher in a generic class called CipherUtilities in his unit test (line 302) he does this like so (line 302):

    c = CipherUtilities.GetCipher("RSA/NONE/OAEPWithSHA1AndMGF1Padding");
    c.Init(false, privKey);
    outBytes = c.DoFinal(outBytes);
    if (!AreEqual(outBytes, input))
    {
        Fail("OAEP test failed on decrypt expected " + Hex.ToHexString(input) + " got " + Hex.ToHexString(outBytes));
    }
    

    note that c is a IBufferedCipher