1.生成一对keys:
/// <summary>
/// generate private key and public key arr[0] for private key arr[1] for public key
/// </summary>
/// <returns></returns>
public static string[] GenerateKeys()
{
string[] sKeys = new String[2];
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
sKeys[0] = rsa.ToXmlString(true);
sKeys[1] = rsa.ToXmlString(false);
return sKeys;
}
2.加密:
/// <summary>
/// RSA Encrypt
/// </summary>
/// <param name="sSource" >Source string</param>
/// <param name="sPublicKey" >public key</param>
/// <returns></returns>
public static string EncryptString(string sSource,string sPublicKey)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string plaintext = sSource;
rsa.FromXmlString(sPublicKey);
byte[] cipherbytes;
byte[] byteEn = rsa.Encrypt(Encoding.UTF8.GetBytes("a"), false);
cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(plaintext), false);
StringBuilder sbString = new StringBuilder();
for (int i = 0; i < cipherbytes.Length; i++)
{
sbString.Append(cipherbytes[i] + ",");
}
3. 解密:
/// <summary>
/// RSA Decrypt
/// </summary>
/// <param name="sSource">Source string</param>
/// <param name="sPrivateKey">Private Key</param>
/// <returns></returns>
public static string DecryptString(String sSource, string sPrivateKey)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(sPrivateKey);
byte[] byteEn = rsa.Encrypt(Encoding.UTF8.GetBytes("a"), false);
string[] sBytes = sSource.Split(',');
for (int j = 0; j < sBytes.Length; j++)
{
if (sBytes[j] != "")
{
byteEn[j] = Byte.Parse(sBytes[j]);
}
}
byte[] plaintbytes = rsa.Decrypt(byteEn, false);
return Encoding.UTF8.GetString(plaintbytes);
}
return sbString.ToString();
}