功能:用指定的公鑰(n,e)解密指定字符串 source
*/
private string DecryptString(string source, BigInteger e, BigInteger n)
...{
int len = source.Length;
int len1 = 0;
int blockLen = 0;
if ((len % 256) == 0)
len1 = len / 256;
else
len1 = len / 256 + 1;
string block = "";
string temp = "";
for (int i = 0; i < len1; i++)
...{
if (len >= 256)
blockLen = 256;
else
blockLen = len;
block = source.Substring(i * 256, blockLen);
BigInteger biText = new BigInteger(block, 16);
BigInteger bIEnText = biText.modPow(e, n);
string temp1 = System.Text.Encoding.Default.GetString(bIEnText.getBytes());
temp += temp1;
len -= blockLen;
}
return temp;
}
加密過 程和解密過程代碼如下所示:
/**//*
加 密過程,其中d、n是RSACryptoServiceProvider生成的D、Modulus
*/
private string EncryptProcess(string source, string d, string n)
...{
byte[] N = Convert.FromBase64String(n);
byte[] D = Convert.FromBase64String(d);
BigInteger biN = new BigInteger(N);
BigInteger biD = new BigInteger(D);
return EncryptString(source, biD, biN);
}
/**//*
解密過程,其中e、n是 RSACryptoServiceProvider生成的Exponent、Modulus
*/
private string DecryptProcess(string source, string e, string n)
...{
byte[] N = Convert.FromBase64String(n);
byte[] E = Convert.FromBase64String(e);
BigInteger biN = new BigInteger(N);
BigInteger bIE = new BigInteger(E);
return DecryptString(source, bIE, biN);
}
以 上方法經本人實際使用,效果良好,希望對朋友們有幫助。
PS:文中所用大整數類下載地址:http://www.hugesoft.Net/ContentPage.ASPx?p1=010001&p2=201