Storing large numbers for RSA encryption in java

我是研究僧i 提交于 2019-12-12 04:24:13

问题


Before I post my code, I think it is best that I layout a couple of things first.

Goal:

Perform very basic RSA encryption on a couple of small numbers. For those of you who are familiar with RSA encryption, I have posted the values being used for the algorithm below.

Current RSA numbers/values:

P=29

Q=31

N=P*Q

Phi=((P-1)*(Q-1))

E=11

My issue:

The problem arises when I am trying to decrypt my code. The encryption works as designed.

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() + " ");
}

The initial problem was the fact that D (private exponent), when applied as an exponent, was way to big for long. I did a quick Google search and decided to try and implement BigInteger. When I run the program, it throws this error:

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:

To be honest, I have not tried really anything because I know the answer does not compute to infinity, but BigInteger thinks that it does. Is there anyway that I can store a number such as 130^611? If so, How?

Big Question:

How can I store the values needed to perform the decryption?

Thank you in advance to any who try and help me!


回答1:


Your problem is occurring because you are doing the calculations with primitive data types, then storing those primitives in a BigInteger. This defeats the purpose of using a BigInteger. Let's look at the offending line:

BigInteger decrypt = new BigInteger(Math.pow(encryp_mesg[j],D) + "");

When Java evaluates this line, it will first take this expression

Math.pow(encryp_mesg[j],D) + ""

And evaluate it. It will then pass the result of this evaluation to the constructor of BigInteger. However, at this point you have already exceeded the bounds of the data types you're working with. Instead, you should be doing the math with BigIntegers, like this:

BigInteger e = new BigInteger(Integer.toString(encryp_mesg[j]));
BigInteger decrypt  = e.pow(D);

Now you're only doing calculations using BigInteger, and only storing in primitive data types values that you already had stored in primitive data types.



来源:https://stackoverflow.com/questions/38536018/storing-large-numbers-for-rsa-encryption-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!