【发布时间】:2016-07-22 22:18:37
【问题描述】:
在我发布我的代码之前,我认为最好先布局几件事。
目标:
对几个小数字执行非常基本的 RSA 加密。对于那些熟悉 RSA 加密的人,我已经发布了用于下面算法的值。
当前 RSA 数字/值:
P=29
Q=31
N=P*Q
Phi=((P-1)*(Q-1))
E=11
我的问题:
当我试图解密我的代码时,问题就出现了。加密按设计工作。
代码:
long[] mesg = new long[]{8, 7, 26, 28};
long[] encrypted_mesg = new long[mesg.length];
for(int i=0; i<mesg.length; i++){
encrypted_mesg[i]=(long)((Math.pow(mesg[i],E))%N);
System.out.print(encrypted_mesg[i] + " ");
}
System.out.println();
//Decrpyt (not functioning-long to small, Big Integer not working)
for(int j=0; j<encryp_mesg.length; j++){
BigInteger decrypt = new BigInteger(Math.pow(encryp_mesg[j],D) + "");
System.out.print(decrypt.toString() + " ");
}
最初的问题是,当 D(私有指数)作为指数应用时,它会长期变大。我做了一个快速的谷歌搜索,并决定尝试实现 BigInteger。当我运行程序时,它会抛出这个错误:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Infinity"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.math.BigInteger.<init>(BigInteger.java:461)
at java.math.BigInteger.<init>(BigInteger.java:597)
at RSA_Riddles.main(RSA_Riddles.java:23)**
我尝试解决的问题:
说实话,我没有尝试过任何事情,因为我知道答案不会计算到无穷大,但 BigInteger 认为它确实如此。无论如何我可以存储一个数字,如 130^611?如果是这样,如何?
大问题:
如何存储执行解密所需的值?
提前感谢任何尝试帮助我的人!
【问题讨论】:
-
我不知道是设计还是运气,当您的加密计算对 29 以上的大多数值产生错误结果时,您只测试高达 28 的值,解密也应该同样是 mod n:m = c^d mod n。同样对您的玩具尺寸进行取幂和取模几乎不实用,但是为安全尺寸进行操作将比您的生命周期(或您的计算机)花费更长的时间,因此实际实现将它们交错,如 Wikipedia 等中所述 - 然后是玩具大小你根本不需要 bignums。
标签: java encryption cryptography rsa storing-data