/// <summary>
/// ikmb@163.com
/// </summary>
public class MyRAS
{
/// <summary>
/// RAS加密
/// </summary>
/// <param name="xmlPublicKey">公钥</param>
/// <param name="EncryptString">明文</param>
/// <returns>密文</returns> public static string RSAEncrypt(string xmlPublicKey, string EncryptString)
{
byte[] PlainTextBArray;
byte[] CypherTextBArray;
string Result=String.Empty;
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
int t = (int)(Math.Ceiling((double)EncryptString.Length / (double)));
//分割明文
for (int i = ; i <= t-; i++)
{ PlainTextBArray = (new UnicodeEncoding()).GetBytes(EncryptString.Substring(i * , EncryptString.Length - (i * ) > ? : EncryptString.Length - (i * )));
CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
Result += Convert.ToBase64String(CypherTextBArray) + "ThisIsSplit";
}
return Result;
}
/// <summary>
/// RAS解密
/// </summary>
/// <param name="xmlPrivateKey">私钥</param>
/// <param name="DecryptString">密文</param>
/// <returns>明文</returns>
public static string RSADecrypt(string xmlPrivateKey, string DecryptString)
{
byte[] PlainTextBArray;
byte[] DypherTextBArray;
string Result=String.Empty;
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
string[] Split = new string[];
Split[] = "ThisIsSplit";
//分割密文
string[] mis = DecryptString.Split(Split, StringSplitOptions.RemoveEmptyEntries);
for (int i = ; i < mis.Length; i++)
{
PlainTextBArray = Convert.FromBase64String(mis[i]);
DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
Result += (new UnicodeEncoding()).GetString(DypherTextBArray);
}
return Result;
} /// <summary>
/// 产生公钥和私钥对
/// </summary>
/// <returns>string[] 0:私钥;1:公钥</returns>
public static string[] RSAKey()
{
string[] keys = new string[];
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
keys[] = rsa.ToXmlString(true);
keys[] = rsa.ToXmlString(false);
return keys;
}
}