public static class RsaHelper
{
public static byte[] Encrypte(byte[] source, string publicKeyXml, int keyLength, bool oaep)
{
publicKeyXml = "your key";
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keyLength))
{
try
{
rsa.InitializeFromXmlString(publicKeyXml);
}
catch(Exception ex)
{
//ShowError("密钥格式有误", "加密失败", ex);
}
return Encrypte(source, rsa,oaep);
}
}
public static byte[] Encrypte(byte[] source, RSAParameters parameters, bool oaep)
{
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(parameters.Modulus.Length * 8))
{
rsa.ImportParameters(parameters);
return Encrypte(source, rsa, oaep);
}
}
private static byte[] Encrypte(byte[] source, RSACryptoServiceProvider rsa, bool oaep)
{
List<byte> encrypted = new List<byte>();
MemoryStream msInput = new MemoryStream(source);
int bufferSize = rsa.KeySize / 8 - 11;
byte[] buffer = new byte[bufferSize];
int length = msInput.Read(buffer, 0, bufferSize);
try
{
while (length > 0)
{
if (length == bufferSize)
{
encrypted.AddRange(rsa.Encrypt(buffer, oaep));
}
else
{
byte[] current = new byte[length];
Array.Copy(buffer, current, length);
encrypted.AddRange(rsa.Encrypt(current, oaep));
}
&n

本文档介绍了如何使用RSAHelper类进行公钥和私钥的加密与解密操作,包括Encrypte和Decrypte方法,以及将RSA参数转换为XML字符串的实用函数。示例代码展示了如何使用默认和自定义参数进行加密和解密操作。
最低0.47元/天 解锁文章
1709

被折叠的 条评论
为什么被折叠?



