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));
}
length = msInput.Read(buffer, 0, bufferSize);
}
return encrypted.ToArray();
}
catch (Exception ex)
{
//ShowError("加密失败:" + Environment.NewLine + ex.ToString());
return new byte[0];
}
}
public static byte[] Decrypte(byte[] source, string privateKeyXml,int keyLength, bool oaep)
{
privateKeyXml = "your key";
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keyLength))
{
try
{
rsa.InitializeFromXmlString(privateKeyXml);
}
catch (Exception ex)
{
//ShowError("密钥格式有误", "解密失败", ex);
}
return Decrypte(source, rsa, oaep);
}
}
public static byte[] Decrypte(byte[] source, RSAParameters parameters, bool oaep)
{
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(parameters.D.Length * 8))
{
rsa.ImportParameters(parameters);
return Decrypte(source, rsa, oaep);
}
}
public static byte[] Decrypte(byte[] source, RSACryptoServiceProvider rsa,bool oaep)
{
List<byte> decrypted = new List<byte>();
MemoryStream msInput = new MemoryStream(source);
int bufferSize = rsa.KeySize / 8;
byte[] buffer = new byte[bufferSize];
int length = msInput.Read(buffer, 0, bufferSize);
try
{
//while (length > 0)
//{
// decrypted.AddRange(rsa.Decrypt(buffer, oaep));
// length = msInput.Read(buffer, 0, bufferSize);
//}
while (length > 0)
{
if (length == bufferSize)
{
decrypted.AddRange(rsa.Decrypt(buffer, oaep));
}
else
{
byte[] current = new byte[length];
Array.Copy(buffer, current, length);
decrypted.AddRange(rsa.Decrypt(current, oaep));
}
length = msInput.Read(buffer, 0, bufferSize);
}
return decrypted.ToArray();
//return decrypted.ToArray();
//int count = 0;
//length = decrypted.Count;
//while(decrypted[length-count-1]==(char)0)
//{
// count++;
//}
//decrypted.RemoveRange(length - count, count);
//return decrypted.ToArray();
}
catch (Exception ex)
{
//ShowError("解密失败:" + Environment.NewLine + ex.ToString());
return new byte[0];
}
}
public static string ToPriavateXmlString(this RSACryptoServiceProvider rsa)
{
RSAParameters parameters = rsa.ExportParameters(true);
return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>",
Convert.ToBase64String(parameters.Modulus),
Convert.ToBase64String(parameters.Exponent),
Convert.ToBase64String(parameters.P),
Convert.ToBase64String(parameters.Q),
Convert.ToBase64String(parameters.DP),
Convert.ToBase64String(parameters.DQ),
Convert.ToBase64String(parameters.InverseQ),
Convert.ToBase64String(parameters.D));
}
public static string ToPublicXmlString(this RSACryptoServiceProvider rsa)
{
RSAParameters parameters = rsa.ExportParameters(false);
return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",
Convert.ToBase64String(parameters.Modulus),
Convert.ToBase64String(parameters.Exponent));
}
public static void InitializeFromXmlString(this RSACryptoServiceProvider rsa, string xmlString)
{
RSAParameters parameters = new RSAParameters();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
if (xmlDoc.DocumentElement.Name.Equals("RSAKeyValue"))
{
foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
{
switch (node.Name)
{
case "Modulus":
parameters.Modulus = Convert.FromBase64String(node.InnerText);
break;
case "Exponent":
parameters.Exponent = Convert.FromBase64String(node.InnerText);
break;
case "P":
parameters.P = Convert.FromBase64String(node.InnerText);
break;
case "Q":
parameters.Q = Convert.FromBase64String(node.InnerText);
break;
case "DP":
parameters.DP = Convert.FromBase64String(node.InnerText);
break;
case "DQ":
parameters.DQ = Convert.FromBase64String(node.InnerText);
break;
case "InverseQ":
parameters.InverseQ = Convert.FromBase64String(node.InnerText);
break;
case "D":
parameters.D = Convert.FromBase64String(node.InnerText);
break;
}
}
}
else
{
throw new Exception("Invalid XML RSA key.");
}
rsa.ImportParameters(parameters);
}
}
调用:
加密
byte[] enCrypte = RsaHelper.Encrypte(
System.Text.Encoding.Default.GetBytes("内容"),
null,
1,
false);
解密
byte[] decrypt12e = RsaHelper.Decrypte(
enCrypte ,
null,
1,
false);
测试key--私钥
prikey sample:<RSAKeyValue><Modulus>zFRI24utX198Vo7fu7AJOGmkODeyX6r0vu6SgQWRDwodnYsgQWvnk3o2ORoIeQzibj83FzeSR9RCBQ60JAU35pJMxzqtH42c38XKvEs++B284z61h83GaDTMU7pcL36+heTIG4QOPy1FvvMySxeNd+tQ+jHpwAehx1DR08OObPk=</Modulus><Exponent>AQAB</Exponent><P>/tosAlnxsisd8iYVg1xUpiu3HSMmXs7YXg0af+GihVM/dsG7K5ncMO+6FbfSXv7gM2jsIUXX6L8yyX4lDmwLBw==</P><Q>zT/c6mRHirJBy6l4mQs9Oc23TZOpCvcTzVHDD/VaXSvf6H8rFWNcFpOquM5I0yJfmVDE1fjYpf6YSJ8bu3vH/w==</Q><DP>hW6jRiPjiN5MUoEgGGPaKhqPSCEkMNVIL037BuOTHO0mOD5cigqHn9OUAVT6PySBLEmOACm6TrD0nUx/iFqHNw==</DP><DQ>pKbcs/OqKKuDeLwFENEhVqOers8Lxs4axpUWpFCxIxk1UC/kAg5pK3tec06YwTdL9ZQDs5sjQVaSF3/8IVTVRQ==</DQ><InverseQ>vinBF+khGarSmmhN5SI3o5wsssvfzP3ONqdQZ8Csckq7qy8F1VMlL5pp78iPj0jc8FiFQdD3kw9x7oXGbZbD4g==</InverseQ><D>y7SnokFhijjO5BiykxKQTq57axST/26XbBa69mhPUvgZyGvgr3hdOhlYH5uFoPEoeRMK4Fmlu3iwjQw4qcSOEcVK55EYwIL2ODU8xLEsS6D90OZdBwWj+dNITstbWbaOOq6auo3vVroPBgGTrHiEaHqPBxG3Ysh6OotlSTYdU1U=</D></RSAKeyValue>
测试key--公钥
pubkey sample:<RSAKeyValue><Modulus>zFRI24utX198Vo7fu7AJOGmkODeyX7r0vu6SgQWRDwodnYsgQWvnk3o2ORoIeQzibj83FzeSR9RCBQ60JAU35pJMxzqtH42c38XKvEs++B284z61h83GaDTMU7pcL36+heTIG4QOPy1FvvMySxeNd+tQ+jHpwAehx1DR08OObPk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>