在 java 中存储大量用于 RSA 加密的数字
Storing large numbers for RSA encryption in java
在我 post 我的代码之前,我认为最好先布局一些东西。
目标:
对几个小数字执行非常基本的 RSA 加密。对于那些熟悉 RSA 加密的人,我已经 post 编辑了用于以下算法的值。
当前 RSA numbers/values:
P=29
问=31
N=P*Q
披=((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(私有指数)在用作指数时会长期变大。我进行了快速 Google 搜索并决定尝试实施 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 之类的数字吗?如果是这样,如何?
大题:
如何存储执行解密所需的值?
提前感谢所有尝试帮助我的人!
出现问题是因为您使用原始数据类型进行计算,然后将这些原始数据存储在 BigInteger 中。这违背了使用 BigInteger 的目的。让我们看看有问题的行:
BigInteger decrypt = new BigInteger(Math.pow(encryp_mesg[j],D) + "");
当Java计算这一行时,它会首先取这个表达式
Math.pow(encryp_mesg[j],D) + ""
并对其进行评价。然后它将此评估的结果传递给 BigInteger 的构造函数。但是,此时您已经超出了您正在使用的数据类型的范围。相反,您应该使用 BigIntegers 进行数学运算,如下所示:
BigInteger e = new BigInteger(Integer.toString(encryp_mesg[j]));
BigInteger decrypt = e.pow(D);
现在您只使用 BigInteger 进行计算,并且只将您已经存储在原始数据类型中的值存储在原始数据类型中。
在我 post 我的代码之前,我认为最好先布局一些东西。
目标:
对几个小数字执行非常基本的 RSA 加密。对于那些熟悉 RSA 加密的人,我已经 post 编辑了用于以下算法的值。
当前 RSA numbers/values:
P=29
问=31
N=P*Q
披=((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(私有指数)在用作指数时会长期变大。我进行了快速 Google 搜索并决定尝试实施 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 之类的数字吗?如果是这样,如何?
大题:
如何存储执行解密所需的值?
提前感谢所有尝试帮助我的人!
出现问题是因为您使用原始数据类型进行计算,然后将这些原始数据存储在 BigInteger 中。这违背了使用 BigInteger 的目的。让我们看看有问题的行:
BigInteger decrypt = new BigInteger(Math.pow(encryp_mesg[j],D) + "");
当Java计算这一行时,它会首先取这个表达式
Math.pow(encryp_mesg[j],D) + ""
并对其进行评价。然后它将此评估的结果传递给 BigInteger 的构造函数。但是,此时您已经超出了您正在使用的数据类型的范围。相反,您应该使用 BigIntegers 进行数学运算,如下所示:
BigInteger e = new BigInteger(Integer.toString(encryp_mesg[j]));
BigInteger decrypt = e.pow(D);
现在您只使用 BigInteger 进行计算,并且只将您已经存储在原始数据类型中的值存储在原始数据类型中。