在发布我的代码之前,我认为最好首先安排一些事情 .
Goal:
对几个小数字执行非常基本的RSA加密 . 对于那些熟悉RSA加密的人,我已经发布了下面算法使用的值 .
Current RSA numbers/values:
P = 29
Q = 31
N = P * Q
披=((P-1)*(Q-1))
E = 11
My issue:
当我试图解密我的代码时出现问题 . 加密按设计工作 .
Code:
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)**
What I have tried to fix the problem:
说实话,我没有尝试任何事情,因为我知道答案不会计算到无限,但BigInteger认为它确实如此 . 无论如何我可以存储130 ^ 611这样的数字吗?如果是这样,怎么样?
Big Question:
如何存储执行解密所需的值?
提前感谢任何试图帮助我的人!
1 回答
您的问题正在发生,因为您正在使用原始数据类型进行计算,然后将这些原语存储在BigInteger中 . 这违背了使用BigInteger的目的 . 让我们来看看有问题的一行:
当Java评估此行时,它将首先使用此表达式
并评估它 . 然后它会将此评估的结果传递给BigInteger的构造函数 . 但是,此时您已经超出了正在使用的数据类型的范围 . 相反,你应该用BigIntegers进行数学计算,如下所示:
现在,您只使用BigInteger进行计算,并且仅存储原始数据类型中已存储在基本数据类型中的值 .