Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中存储大量用于RSA加密的数字_Java_Encryption_Cryptography_Rsa_Storing Data - Fatal编程技术网

在java中存储大量用于RSA加密的数字

在java中存储大量用于RSA加密的数字,java,encryption,cryptography,rsa,storing-data,Java,Encryption,Cryptography,Rsa,Storing Data,在我发布我的代码之前,我认为我最好先安排几件事 目标: 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] + " ");

在我发布我的代码之前,我认为我最好先安排几件事

目标:

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() + " ");
}
对几个小数字执行非常基本的RSA加密。对于那些熟悉RSA加密的人,我已经在下面发布了用于算法的值

当前RSA编号/值:

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

Q=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() + " ");
}
当我试图解密我的代码时,问题就出现了。加密按设计工作

代码:

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() + " ");
}
long[]mesg=新长[]{8,7,26,28};
long[]加密的_mesg=新的long[mesg.length];

对于(int i=0;i您的问题之所以出现,是因为您使用基本数据类型进行计算,然后将这些基本数据存储在BigInteger中。这与使用BigInteger的目的背道而驰。让我们看看有问题的行:

BigInteger decrypt = new BigInteger(Math.pow(encryp_mesg[j],D) + "");
当Java计算这一行时,它将首先接受这个表达式

Math.pow(encryp_mesg[j],D) + ""
并对其求值。然后,它将此求值结果传递给BigInteger的构造函数。但是,此时您已经超出了正在使用的数据类型的界限。相反,您应该使用BigInteger进行计算,如下所示:

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

现在,您只使用BigInteger进行计算,并且只将已存储在基本数据类型中的值存储在基本数据类型中。

问题的出现是因为您使用基本数据类型进行计算,然后将这些基本数据存储在BigInteger中。这与使用BigInteger的目的背道而驰。让我们看一下令人不快的一句话:

BigInteger decrypt = new BigInteger(Math.pow(encryp_mesg[j],D) + "");
当Java计算这一行时,它将首先接受这个表达式

Math.pow(encryp_mesg[j],D) + ""
并对其求值。然后,它将此求值结果传递给BigInteger的构造函数。但是,此时您已经超出了正在使用的数据类型的界限。相反,您应该使用BigInteger进行计算,如下所示:

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

现在,您只使用BigInteger进行计算,并且只存储在基本数据类型中您已经存储在基本数据类型中的值。

谢谢,我将立即更改实现。此外,BigInteger有任何限制吗?如果有,限制是什么?很高兴我能帮上忙!如果它对您有效,如果您能够遵循它,那就太好了因此,礼仪和接受答案大整数是“任意精度”,这意味着它们支持的数字与您的计算机所能支持的数字一样大(您不太可能遇到该限制)我有点不知道。这是我的第一个问题。我如何接受答案?答案左边应该是一个灰色的复选标记-单击该复选标记,它会变成绿色-谢谢!@jpgamer31首先使用
biginger.valueOf
执行到
biginger
的转换。您可能还想签出
biginger\modPow
方法最后,
BigInteger
值基本上受实现和内存的限制。基本上:别担心,除非你想做一些愚蠢的事情,比如对大数执行无模幂运算。谢谢,我会立即更改实现。此外,BigInteger有任何限制吗?如果有,它们是什么?很高兴我能你会帮上忙的!如果它对你有用,如果你能遵循这样的礼节并接受答案,那就太好了。大整数是“任意精度”,这意味着它们支持的数字与你的机器所能支持的数字一样大(你不太可能遇到这个限制)我有点不知道。这是我的第一个问题。我如何接受答案?答案左边应该是一个灰色的复选标记-单击该复选标记,它会变成绿色-谢谢!@jpgamer31首先使用
biginger.valueOf
执行到
biginger
的转换。您可能还想签出
biginger\modPow
方法最后,
BigInteger
值基本上受实现和内存的约束。基本上:不要担心,除非你想做一些愚蠢的事情,比如对大数执行无模幂运算。我不知道这是出于设计还是运气,当加密计算产生错误结果时,你只测试28以下的值对于29以上的大多数值,解密也应该类似地为mod n:m=c^d mod n。对于玩具尺寸而言,单独进行指数运算和模运算几乎不实用,但对于安全尺寸而言,解密所需的时间将远远超过您的寿命(或您的计算机),所以实际的实现会像维基百科中解释的那样将它们交织在一起——然后对于玩具大小,你根本不需要bignum。我不知道这是设计还是运气,当你的加密计算对29以上的大多数值产生错误的结果时,你只测试28以下的值。同样,解密也应该是mod n:m=c^d mod另外,分别进行指数运算和模运算对于你的玩具尺寸来说几乎不实用,但对于安全尺寸来说,这要比你的生命周期(或你的计算机)要长得多,所以实际的实现会像维基百科中解释的那样将它们交错在一起——而对于玩具尺寸来说,你根本不需要bignum。