RSA解密问题C#
RSA 解密问题
我在使用 C# RSA 程序时遇到问题。它无法正确解密。当我分配 d =(e^-1)%phiN ,然后将 d 应用于我的密文时,它会得出可笑的十进制答案。它应该得出一个整数。我认为这是我的数学问题。你有什么建议吗? 如果您需要详细信息或其余代码,请询问。 另外,我可以使用填充方案来使此代码更好吗?目前这段代码很容易受到频率分析。
protected void decryptRSA(object sender, EventArgs ev)
{
double p = (double)Convert.ToInt64(P.Text);//I use 123 for testing
double q = (double)Convert.ToInt64(Q.Text);//127
double e = (double)Convert.ToInt64(E.Text);//133
double phiN = (p-1)*(q-1);
double n = p*q;
double d = Math.Pow(e, -1D);
d = d%phiN;
string cipherStr = outputBuffer.Text;
double[] cipherTextDouble = new double[100];
string[]plainText = new string[cipherTextDouble.Length];
cipherTextDouble = parser(cipherStr, 'D');
for(int slot = 0; slot<cipherTextDouble.Length; slot++)
{
cipherTextDouble[slot] = (double)(Math.Pow((double)cipherTextDouble[slot],(double)d)%n);
}
for(int slot = 0; slot<cipherTextDouble.Length; slot++)
{
inputBuffer.Text += Convert.ToChar(cipherTextDouble[slot]) + ' ';//the spot were it dies
//it doesn't like to convert from a decimal like 1.75 to a char. Of course I should never get a decimal like 1.75, which is the problem
}
}
RSA Decryption Issue
I am having problems with a C# RSA program. It is not decrypting properly. When I assign d =(e^-1)%phiN
, and then apply d to my ciphertext it comes up with ridiculous decimal answers. It should come up with a whole number. I think it is a problem with my math. Do you have any advice?
If you need details or the rest of the code, please ask.
Also, is there a padding scheme I could use to make this code better? Right now this code is vulnerable to frequency analysis.
protected void decryptRSA(object sender, EventArgs ev)
{
double p = (double)Convert.ToInt64(P.Text);//I use 123 for testing
double q = (double)Convert.ToInt64(Q.Text);//127
double e = (double)Convert.ToInt64(E.Text);//133
double phiN = (p-1)*(q-1);
double n = p*q;
double d = Math.Pow(e, -1D);
d = d%phiN;
string cipherStr = outputBuffer.Text;
double[] cipherTextDouble = new double[100];
string[]plainText = new string[cipherTextDouble.Length];
cipherTextDouble = parser(cipherStr, 'D');
for(int slot = 0; slot<cipherTextDouble.Length; slot++)
{
cipherTextDouble[slot] = (double)(Math.Pow((double)cipherTextDouble[slot],(double)d)%n);
}
for(int slot = 0; slot<cipherTextDouble.Length; slot++)
{
inputBuffer.Text += Convert.ToChar(cipherTextDouble[slot]) + ' ';//the spot were it dies
//it doesn't like to convert from a decimal like 1.75 to a char. Of course I should never get a decimal like 1.75, which is the problem
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有正确计算指数。您需要找到一个数字
d
,使得ed = 1 (mod phi)
,即e (mod phi)
的倒数。这与计算实数中 e 的倒数不同,后者是 double d = Math.Pow(e, -1D); 计算的内容,然后进行取模运算。这就是您最终得到十进制数的原因(在本例中 1/133 ~ 0.007 和 1/133 % 15372 仍然 = 0.007,因为%
实际上是 C# 中的“余数”运算符,而不是整数模(否则它无论如何都不能在双精度上工作))。您需要使用欧几里得算法来计算逆模 phi。
编辑:GregS 正确地指出,对于计算机实现,您可能需要使用 扩展欧几里得算法 代替一次求模逆。这通常是通过计算完成的。您可以使用欧几里得算法(通常手动)来完成,但这很浪费时间。
You are not calculating the exponent correctly. You need to find a number
d
such thated = 1 (mod phi)
i.e. the inverse ofe (mod phi)
. This is not the same as calculating the inverse of e in the reals which is whatdouble d = Math.Pow(e, -1D);
calculates, and then doing the mod operation. This is the reason that you end up with a decimal number (in this case 1/133 ~ 0.007 and 1/133 % 15372 still = 0.007 since%
is actually a 'remainder' operator in C# and not an integer modulus (otherwise it wouldn't work on a double anyway)).You need to use the Euclidean Algorithm to calculate the inverse mod phi.
EDIT: GregS correctly points out that for a computer implementation you probably want to use the Extended Euclidean Algorithm instead to find the modular inverse in a single pass. This is what is usually done computationally. You can do it with the Euclidean algorithm (usually by hand) but it's a waste of time.