在Java中存储大量用于RSA加密的数据 [英] Storing large numbers for RSA encryption in java

查看:129
本文介绍了在Java中存储大量用于RSA加密的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发布代码之前,我认为最好先布局几件事.

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

目标:

在几个较小的数字上执行非常基本的RSA加密.对于那些熟悉RSA加密的人,我在下面发布了用于该算法的值.

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.

当前的RSA数字/值:

P = 29

Q = 31

N = P * Q

N=P*Q

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

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

E = 11

我的问题:

当我尝试解密我的代码时出现问题.加密按设计工作.

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

代码:

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.当我运行程序时,它将引发此错误:

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)** 

我已尝试解决的问题:

说实话,我没有做任何尝试,因为我知道答案不会计算为无穷大,但是BigInteger认为可以.无论如何,我可以存储130 ^ 611这样的数字吗?如果是这样,如何?

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?

主要问题:

如何存储执行解密所需的值?

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

在此先感谢所有尝试帮助我的人!

推荐答案

出现问题是因为您要使用原始数据类型进行计算,然后将这些原始数据存储在BigInteger中.这违反了使用BigInteger的目的.让我们看一下令人讨厌的行:

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

Java评估此行时,它将首先采用此表达式

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

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

并对其进行评估.然后它将评估结果传递给BigInteger的构造函数.但是,这时您已经超出了要使用的数据类型的范围.相反,您应该使用BigIntegers进行数学运算,如下所示:

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);

现在,您仅使用BigInteger进行计算,并且仅在原始数据类型中存储已经存储在原始数据类型中的值.

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.

这篇关于在Java中存储大量用于RSA加密的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆