RSA实现在C# [英] RSA Implementation in C#
问题描述
我想实现RSA算法在C#。 。试图复制的RSA-100或更大,其中p和q是非常大时,下面的代码工作当p和q是小,但不
例如当:
p = 61,q = 53,N = 3233,披(N)= 3120,E = 17,D = 2753
一旦被解密,我得到了正确的原始一封邮件。我从 RSA维基百科页面得到这些值。代码也适用于p和q的其他小的值。
然而,使用 RSA-100 或更高,我不回来我的原始邮件。我一直在使用的不同值指数(e)尝试并确保它是互质与皮皮(n)的,但我不能得到正确的结果。我失去了一些东西简单/显而易见的?
感谢您提前为您的帮助!
// p和q的RSA-100
//串p =37975227936943673922808872755445627854565536638199;
//串Q =40094690950920881030683735292761468389214899724061;
串P =61;
串Q =53;
//字符串转换为BigInteger的
的BigInteger rsa_p = BigInteger.Parse(P);
的BigInteger rsa_q = BigInteger.Parse(Q);
// N = P * Q
的BigInteger rsa_n = BigInteger.Multiply(rsa_p,rsa_q);
//披(N)=(P-1)*(Q-1)
的BigInteger rsa_fn = BigInteger.Multiply((rsa_p - 1),(rsa_q - 1));
的BigInteger rsa_e = 17;
//计算ð
的BigInteger rsa_d = BigInteger.ModPow(rsa_e,(rsa_fn - 1),rsa_fn);
//加密该消息,在这种情况下,3007
// C =(3007 ^ rsa_e)模rsa_n
的BigInteger C = BigInteger.ModPow(3007,rsa_e,rsa_n) ;
//解密消息,男应该等于3007
// M =(3007 ^ rsa_d)模rsa_n
的BigInteger M = BigInteger.ModPow(C,rsa_d,rsa_n) ;
D = E ^(PHI(N)-1 )MOD岛(N)看起来我错了。您也需要D = E ^(PHI(PHI(N)) - 1)。MOD岛(N),或者你可以使用扩展欧几里德
I am trying to implement the RSA Algorithm in C#. The code below works when p and q are small, but not when trying to replicate RSA-100 or greater where p and q are very large.
For example when:
p = 61, q = 53, n = 3233, phi(n) = 3120, e = 17, d = 2753
Once decrypted, I get the correct original messsage. I got these values from the RSA Wikipedia page. The code also works for other small values of p and q.
However, when using RSA-100 or greater, I do not get back my original message. I have tried using different values for the exponent (e) and made sure it is coprime with phi(n) but I cannot get the correct result. Am I missing something simple/obvious?
Thank you in advance for your help!
//p and q for RSA-100
//string p = "37975227936943673922808872755445627854565536638199";
//string q = "40094690950920881030683735292761468389214899724061";
string p = "61";
string q = "53";
//Convert string to BigInteger
BigInteger rsa_p = BigInteger.Parse(p);
BigInteger rsa_q = BigInteger.Parse(q);
//n = p * q
BigInteger rsa_n = BigInteger.Multiply(rsa_p, rsa_q);
//phi(n) = (p-1)*(q-1)
BigInteger rsa_fn = BigInteger.Multiply((rsa_p - 1), (rsa_q - 1));
BigInteger rsa_e = 17;
//Compute d
BigInteger rsa_d = BigInteger.ModPow(rsa_e, (rsa_fn - 1), rsa_fn);
//Encrypt the message, in this case 3007
//C = (3007^rsa_e) mod rsa_n
BigInteger C = BigInteger.ModPow(3007, rsa_e, rsa_n);
//Decrypt the message, M should equal 3007
//M = (3007^rsa_d) mod rsa_n
BigInteger M = BigInteger.ModPow(C, rsa_d, rsa_n);
d=e^(phi(n)-1) mod phi(n) looks wrong to me. You either need d=e^(phi(phi(n))-1) mod phi(n), or you could use extended euclid.
这篇关于RSA实现在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!