简体   繁体   中英

Encrypt text with RSA/ECB/OAEPWithMD5AndMGF1Padding in C#

We have been asked to encrypt text with following encryption method RSA/ECB/OAEPWithMD5AndMGF1Padding.

While defining the OAEP padding we are only allowed to define one hash function(MD5) and I could not find any way to define the other hash function(MGF1).

var rsa = new RSACng();
rsa.ImportParameters(parameters);
byte[] bytesToBeEncoded = Encoding.UTF8.GetBytes("Testing message");
var encryptedText = rsa.Encrypt(bytesToBeEncoded, RSAEncryptionPadding.CreateOaep(HashAlgorithmName.MD5));

Is there any way we can define the other hash function for Oaep padding (MGF1) or is it not possible within C#?

Thanks

So as per the suggestion of Topaco I am able to encrypt the code using the Bouncy Castle library.

// Read in public key from file
var pemReader = new PemReader(File.OpenText(@"D:\test.pub.pem"));
var rsaPub =(Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters)pemReader.ReadObject();
// create encrypter
var plainTextMessage = Encoding.UTF8.GetBytes("Testing message");
var encrypter = new OaepEncoding(new RsaEngine(), new MD5Digest(), new Sha1Digest(), null);
encrypter.Init(true, rsaPub);
var cipher = encrypter.ProcessBlock(plainTextMessage, 0, plainTextMessage.Length);
var encryptedtext = Convert.ToBase64String(cipher);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM