中國最大的web開發資源網站及技術社區,
實際沒做什么事情,想起來也無恥,不過可能有些朋友比較懶的話,也會有一點用的。在此先向 duduwolf 表示歉意再說。
嘟嘟狼的原文如下:http://dev.csdn.net/develop/article/30/30182.shtm
我的無恥成果如下(有些地方作了一些小調整):
#region using directives
using system;
using system.collections.generic;
using system.text;
using system.diagnostics;
#endregion
namespace rsatest
{
/*
??? rsa算法
????? 1978年就出現了這種算法,它是第一個既能用于數據加密也能用于數字簽名的算法。
??? 它易于理解和操作,也很流行。算法的名字以發明者的名字命名:ron rivest,
??? adishamir 和leonard adleman。但rsa的安全性一直未能得到理論上的證明。
????? rsa的安全性依賴于大數難于分解這一特點。公鑰和私鑰都是兩個大素數(大于100個
??? 十進制位)的函數。據猜測,從一個密鑰和密文推斷出明文的難度等同于分解兩個大
??? 素數的積。
????? 密鑰對的產生。選擇兩個大素數,p 和q 。計算:n = p * q 然后隨機選擇加密密鑰e,
??? 要求 e 和 ( p - 1 ) * ( q - 1 )互質。最后,利用euclid 算法計算解密密鑰d, 滿足
??? e * d = 1 ( mod ( p - 1 ) * ( q - 1 ) )其中n和d也要互質。數e和n是公鑰,d是私鑰。
??? 兩個素數p和q不再需要,應該丟棄,不要讓任何人知道。加密信息 m(二進制表示)時,
??? 首先把m分成等長數據塊 m1 ,m2,..., mi ,塊長s,其中 2^s <= n, s 盡可能的大。對應
??? 的密文是:
ci = mi^e ( mod n ) .................( a )
解密時作如下計算:
mi = ci^d ( mod n ) .................( b )
????? rsa 可用于數字簽名,方案是用 ( a ) 式簽名, ( b )式驗證。具體操作時考慮到安全性
??? 和 m信息量較大等因素,一般是先作hash 運算。rsa 的安全性。rsa的安全性依賴于大數
??? 分解,但是否等同于大數分解一直未能得到理論上的證明,因為沒有證明破解rsa就一定
??? 需要作大數分解。假設存在一種無須分解大數的算法,那它肯定可以修改成為大數分解算法。
??? 目前,rsa的一些變種算法已被證明等價于大數分解。不管怎樣,分解n是最顯然的攻擊方法。
??? 現在,人們已能分解140多個十進制位的大素數。因此,模數n必須選大一些,因具體適用情況而定。
???
??? 由于進行的都是大數計算,使得rsa最快的情況也比des慢上100倍,無論是軟件還是硬件實現。
??? 速度一直是rsa的缺陷。一般來說只用于少量數據加密。
*/
??? public struct rsa_param
??? {
??????? public uint64 p, q;?? //兩個素數,不參與加密解密運算
??????? public uint64 f;????? //f=(p-1)*(q-1),不參與加密解密運算
??????? public uint64 n, e;?? //公匙,n=p*q,gcd(e,f)=1
??????? public uint64 d;????? //私匙,e*d=1 (mod f),gcd(n,d)=1
??????? public uint64 s;????? //塊長,滿足2^s<=n的最大的s,即log2(n)
??? };
??? class program
??? {
??????? //小素數表
??????? #region prime table
??????? readonly static long[] g_primetable =
??????? {
??????????? 3,
??????????? 5,
??????????? 7,
??????????? 11,
??????????? 13,
??????????? 17,
??????????? 19,
??????????? 23,
??????????? 29,
??????????? 31,
??????????? 37,
??????????? 41,
??????????? 43,
??????????? 47,
??????????? 53,
??????????? 59,
??????????? 61,
??????????? 67,
??????????? 71,
??????????? 73,
??????????? 79,
??????????? 83,
??????????? 89,
??????????? 97
??????? };
??????? #endregion
??????? readonly long g_primecount = g_primetable.length;
??????? const uint64 multiplier = 12747293821;
??????? const uint64 adder = 1343545677842234541;
??????? //隨機數類
??????? public class randnumber
??????? {
??????????? /* */
??????????? private uint64 randseed;/* */
??????????? public randnumber():this(0) { }
??????????? public randnumber(uint64 s)
??????????? {
??????????????? if (0 == s)//(!s)
??????????????? {
??????????????????? randseed = (uint64)new random().next();//time(null);
??????????????? }
??????????????? else
??????????????? {
??????????????????? randseed = s;
??????????????? }
??????????? }
???????????
??????????? /* */
??????????? public uint64 random(uint64 n)
??????????? {
??????????????? randseed = multiplier * randseed + adder;
??????????????? return randseed % n;
??????????? }
??????? }
??????? static randnumber g_rnd = new randnumber();
??????? /* 模乘運算,返回值 x=a*b mod n */
??????? uint64 mulmod(uint64 a, uint64 b, uint64 n)
??????? {
??????????? return a * b % n;
??????? }
??????? /* 模冪運算,返回值 x=base^pow mod n */
??????? uint64 powmod(uint64 bas, uint64 pow, uint64 n)
??????? {
??????????? uint64 a = bas, b = pow, c = 1;
??????????? while (b != 0)? // (b)
??????????? {
??????????????? while (1 != (b & 1))??? // !(b&1)
??????????????? {
??????????????????? b >>= 1;??????????? //a=a * a % n;??? //函數看起來可以處理64位的整數,但由于這里a*a在a>=2^32時已經造成了溢出,因此實際處理范圍沒有64位
??????????????????? a = mulmod(a, a, n);
??????????????? } b--;??????? //c=a * c % n;??????? //這里也會溢出,若把64位整數拆為兩個32位整數不知是否可以解決這個問題。
??????????????? c = mulmod(a, c, n);
??????????? } return c;
??????? }
??????? /*
??????? rabin-miller素數測試,通過測試返回1,否則返回0。
??????? n是待測素數。
??????? 注意:通過測試并不一定就是素數,非素數通過測試的概率是1/4
??????? */
??????? long rabinmillerknl(uint64 n)
??????? {
??????????? uint64 b, m, j, v, i;
??????????? m = n - 1;
??????????? j = 0;??? //0、先計算出m、j,使得n-1=m*2^j,其中m是正奇數,j是非負整數
??????????? while (1 != (m&1))??? // (!(m & 1))
??????????? {
??????????????? ++j;
??????????????? m >>= 1;
??????????? }??? //1、隨機取一個b,2<=b??????????? b = 2 + g_rnd.random(n - 3);??? //2、計算v=b^m mod n
??????????? v = powmod( b,? m,? n);??? //3、如果v==1,通過測試
??????????? if (v == 1)
??????????? {
??????????????? return 1;
??????????? }??? //4、令i=1
??????????? i = 1;??? //5、如果v=n-1,通過測試
??????????? while (v != n - 1)
??????????? {
??????????????? //6、如果i==l,非素數,結束
??????????????? if (i == j)
??????????????? {
??????????????????? return 0;
??????????????? }??????? //7、v=v^2 mod n,i=i+1
??????????????? v = powmod( v, 2,? n);
??????????????? ++i;??????? //8、循環到5
??????????? } return 1;
??????? }
??????? /*
??????? rabin-miller素數測試,循環調用核心loop次
??????? 全部通過返回1,否則返回0
??????? */
??????? long rabinmiller(uint64 n, long loop)
??????? {
??????????? //先用小素數篩選一次,提高效率
??????????? for (long i = 0; i < g_primecount; i++)
??????????? {
??????????????? if ((n % unchecked((ulong)g_primetable[i])) == 0)
??????????????? {
??????????????????? return 0;
??????????????? }
??????????? }
??????????? //循環調用rabin-miller測試loop次,使得非素數通過測試的概率降為(1/4)^loop
??????????? for (long i = 0; i < loop; i++)
??????????? {
??????????????? if (0 == rabinmillerknl(n)) //(! ...)
??????????????? {
??????????????????? return 0;
??????????????? }
??????????? } return 1;
??????? }
??????? /* 隨機生成一個bits位(二進制位)的素數,最多32位 */
??????? uint64 randomprime(char bits)
??????? {
??????????? uint64 bas;
??????????? do
??????????? {
??????????????? bas = (uint32)1 << (bits - 1);?? //保證最高位是1
??????????????? bas += g_rnd.random(bas);?????????????? //再加上一個隨機數
??????????????? bas |= 1;??? //保證最低位是1,即保證是奇數
??????????? } while (0 == rabinmiller(bas, 30)); // (!rabinmiller(bas, 30))??? //進行拉賓-米勒測試30次
??????????? return bas;??? //全部通過認為是素數
??????? }
??????? /* 歐幾里得法求最大公約數 */
??????? uint64 euclidgcd(uint64 p, uint64 q)
??????? {
??????????? uint64 a = p > q ? p : q;
??????????? uint64 b = p < q ? p : q;
??????????? uint64 t;
??????????? if (p == q)
??????????? {
??????????????? return p;?? //兩數相等,最大公約數就是本身
??????????? }
??????????? else
??????????? {
??????????????? while (0 != b) //(b)??? //輾轉相除法,gcd(a,b)=gcd(b,a-qb)
??????????????? {
??????????????????? a = a % b;
??????????????????? t = a;
??????????????????? a = b;
??????????????????? b = t;
??????????????? } return a;
??????????? }
??????? }
??????? /* stein法求最大公約數 */
??????? uint64 steingcd( uint64 p,? uint64 q)
??????? {
??????????? uint64 a = p > q ? p : q;
??????????? uint64 b = p < q ? p : q;
??????????? uint64 t, r = 1;
??????????? if (p == q)
??????????? {
??????????????? return p;?????????? //兩數相等,最大公約數就是本身
??????????? }
??????????? else
??????????? {
??????????????? //while ((!(a & 1)) && (!(b & 1)))
??????????????? while ((0 ==(a & 1)) && (0 ==(b & 1)))
??????????????? {
??????????????????? r <<= 1;????????? //a、b均為偶數時,gcd(a,b)=2*gcd(a/2,b/2)
??????????????????? a >>= 1;
??????????????????? b >>= 1;
??????????????? }
??????????????? if (0 == (a&1))//(!(a & 1))
??????????????? {
??????????????????? t = a;??????????? //如果a為偶數,交換a,b
??????????????????? a = b;
??????????????????? b = t;
??????????????? } do
??????????????? {
??????????????????? while (0 == (b & 1))//(!(b & 1))
??????????????????? {
??????????????????????? b >>= 1;????? //b為偶數,a為奇數時,gcd(b,a)=gcd(b/2,a)
??????????????????? } if (b < a)
??????????????????? {
??????????????????????? t = a;??????? //如果b小于a,交換a,b
??????????????????????? a = b;
??????????????????????? b = t;
??????????????????? } b = (b - a) >> 1; //b、a都是奇數,gcd(b,a)=gcd((b-a)/2,a)
??????????????? } while (b != 0); //(b);
??????????????? return r * a;
??????????? }
??????? }
??????? /*
??????? 已知a、b,求x,滿足a*x =1 (mod b)
??????? 相當于求解a*x-b*y=1的最小整數解
??????? */
??????? uint64 euclid(uint64 a, uint64 b)
??????? {
??????????? uint64 m, e, i, j, x, y;
??????????? long xx, yy;
??????????? m = b;
??????????? e = a;
??????????? x = 0;
??????????? y = 1;
??????????? xx = 1;
??????????? yy = 1;
??????????? while (0 != e)//(e)
??????????? {
??????????????? i = m / e;
??????????????? j = m % e;
??????????????? m = e;
??????????????? e = j;
??????????????? j = y;
??????????????? y *= i;
??????????????? if (xx == yy)
??????????????? {
??????????????????? if (x > y)
??????????????????? {
??????????????????????? y = x - y;
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? y -= x;
??????????????????????? yy = 0;
??????????????????? }
??????????????? }
??????????????? else
??????????????? {
??????????????????? y += x;
??????????????????? xx = 1 - xx;
??????????????????? yy = 1 - yy;
??????????????? } x = j;
??????????? }
??????????? if (xx == 0)
??????????? {
??????????????? x = b - x;
??????????? } return x;
??????? }
??????? /* 隨機產生一個rsa加密參數 */
??????? rsa_param rsagetparam()
??????? {
??????????? rsa_param rsa =new rsa_param();
??????????? uint64 t;
??????????? rsa.p = randomprime((char)16);????????? //隨機生成兩個素數
??????????? rsa.q = randomprime((char)16);
??????????? rsa.n = rsa.p * rsa.q;
??????????? rsa.f = (rsa.p - 1) * (rsa.q - 1);
??????????? do
??????????? {
??????????????? rsa.e = g_rnd.random(65536);? //小于2^16,65536=2^16
??????????????? rsa.e |= 1;?????????????????? //保證最低位是1,即保證是奇數,因f一定是偶數,要互素,只能是奇數
??????????? } while (steingcd(rsa.e, rsa.f) != 1); rsa.d = euclid(rsa.e, rsa.f);
??????????? rsa.s = 0;
??????????? t = rsa.n >> 1;
??????????? while (0 != t)//(t)
??????????? {
??????????????? rsa.s++;??????????????????? //s=log2(n)
??????????????? t >>= 1;
??????????? } return rsa;
??????? }
??????? /* 拉賓-米勒測試 */
??????? void testrm()
??????? {
??????????? uint32 k = 0;
??????????? console.write(" - rabin-miller prime check./n/n");
??????????? for (uint64 i = 4197900001; i < 4198000000; i += 2)
??????????? {
??????????????? if (0 != rabinmiller(i, 30))
??????????????? {
??????????????????? k++;
??????????????????? console.writeline(i);
??????????????? }
??????????? }
??????????? console.writeline("total: " + k);
??????? }
??????? /* rsa加密解密 */
??????? void testrsa()
??????? {
??????????? rsa_param r;
??????????? string psrc = "abcdefghijklmnopqrstuvwxyz";
??????????? uint32 n = (uint)psrc.length;
??????????? //unsigned char?????? *q, pdec[n];
??????????? byte[] pdec = new byte[n];
??????????? uint64[] penc = new uint64[n];
??????????? r = rsagetparam();
??????????? console.writeline("p=" + r.p);
??????????? console.writeline("q=" + r.q);
??????????? console.writeline("f=(p-1)*(q-1)=" + r.f);
??????????? console.writeline("n=p*q=" + r.n);
??????????? console.writeline("e=" + r.e);
??????????? console.writeline("d=" + r.d);
??????????? console.writeline("s=" + r.s);
??????????? console.writeline("source:" + psrc);
??????????? //q= (unsigned char *)psrc;
??????????? console.write("encode:");
??????????? for (int i = 0; i < (int)n; i++)
??????????? {
??????????????? //penc[i]=powmod(q[i], r.e, r.n);
??????????????? penc[i] = powmod((ulong)psrc[i], r.e, r.n);
??????????????? console.write(penc[i].tostring() + " ");
??????????? } console.writeline("");
??????????? console.write("decode:");
??????????? for (int i = 0; i < (int)n; i++)
??????????? {
??????????????? pdec[i] = (byte)powmod((ulong)penc[i], r.d, r.n);
??????????????? console.write((uint32)pdec[i] + " ");
??????????? } console.writeline("");
??????????? console.writeline(encoding.default.getstring(pdec));
??????? }/* */
??????? static void main(string[] args)
??????? {
??????????? new program().testrsa();
??????? }
??? }
}