简体   繁体   English

在 C# 中使用 RSA/ECB/OAEPWithSHA-1AndMGF1Padding 加密

[英]Encrpyt with RSA/ECB/OAEPWithSHA-1AndMGF1Padding in C#

I need to encrypt some text in C# .NET 4.7.2 and the cipher i need to use is RSA/ECB/OAEPWithSHA-1AndMGF1Padding.我需要在 C# .NET 4.7.2 中加密一些文本,我需要使用的密码是 RSA/ECB/OAEPWithSHA-1AndMGF1Padding。 However the built-in C# class, RSACryptoServiceProvider doesn't seem to have MGF1 padding.然而,内置的 C# 类RSACryptoServiceProvider似乎没有 MGF1 填充。

How do I encrypt with a public key and RSA/ECB/OAEPWithSHA-1AndMGF1Padding?如何使用公钥和 RSA/ECB/OAEPWithSHA-1AndMGF1Padding 进行加密?

The code I currently have:我目前拥有的代码:

var cert = new X509Certificate2('path to certificate');
var rsa = cert.PublicKey.Key as RSACryptoServiceProvider;
var dataToEncrypt = ASCIIEncoding.ASCII.GetBytes(data);
var encryptedByteArray = rsa.Encrypt(dataToEncrypt, true).ToArray();

return Convert.ToBase64String(encryptedByteArray);
RSA rsa = ...;
byte[] encrypted = rsa.Encrypt(data, RSAEncryptionPadding.OaepSha1);

MGF-1 is the only Mask Generation Function defined by RFC thus far, so it is implied. MGF-1 是迄今为止 RFC 定义的唯一掩码生成函数,因此它是隐含的。

Also, try to avoid using RSACryptoServiceProvider directly.另外,尽量避免直接使用 RSACryptoServiceProvider。 If you're on an old version and have to, then passing true to its fOAEP parameter is the same as requesting OaepSha1 from the new overload.如果您使用的是旧版本并且必须这样做,那么将true传递给其fOAEP参数与从新重载请求 OaepSha1 相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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