[英]Program freezes during the generation of a variable in a while cicle in JAVA
In my school, teachers told has to write a program that encrypts a number with the RSA encryption. 在我的学校,老师被告知必须编写一个使用RSA加密对数字进行加密的程序。 The whole program works just fine, except for a small part, that sometimes freezes the program when gets executed. 整个程序工作正常,除了一小部分,在执行时有时会冻结程序。 Here's the code of the class: 这是该类的代码:
package newpackage;
import java.lang.Math.*;
import java.util.*;
import java.lang.*;
import java.io.*;
public class CrittografiaRSA {
public static Random rand = new Random();
public static double d;
public static String cod;
public static int e;
public static boolean EuclideAlgorithm(int a, int b) {
int r;
boolean k = false;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
if (a == 1) {
k = true;
}
if (a != 1) {
k = false;
}
return k;
}
public static String RSA(int p, int q, String mex, int e) {
CrittografiaRSA.cod = "";
int pqless, n, i = 0, v;
boolean GCD = false, dVerify = false, PQPN;
PQPN = EuclideAlgorithm(p, q);
if (PQPN == false) {
cod = "P e Q non sono primi fra loro";
return cod;
}
n = p * q;
pqless = (q - 1) * (p - 1);
if (NewJFrame.generateE == true) {
while (GCD == false || e >= pqless || e == 1) {
e = rand.nextInt(pqless);
System.out.println(e);
GCD = EuclideAlgorithm(e, pqless);
}
}
if (NewJFrame.generateE == false) {
GCD = EuclideAlgorithm(e, pqless);
if (GCD == false || e >= pqless || e == 1) {
cod = "La E fornita non è adatta.";
return cod;
}
}
CrittografiaRSA.d = e;
while (e == CrittografiaRSA.d || d == 0) {
while (dVerify == false) {
CrittografiaRSA.d = ((i * pqless) + 1) / (double) e;
v = (int) d;
if (d == v) {
dVerify = true;
}
i = i + 1;
}
if (e == i && NewJFrame.generateE == false) {
cod = "Nessuna chiave D generabile con le chiavi inserite";
return cod;
}
}
System.out.println("La chiave pubblica è: (" + n + "," + e + ")\n" + "La chiave privata è: (" + n + "," + (int) d + ")");
if (NewJFrame.cryptoChar == false) {
int mexx = Integer.parseInt(mex);
if (n <= mexx) {
cod = "Messaggio troppo grande per le chiavi fornite";
return cod;
}
long c;
double pot = Math.pow(mexx, e);
c = (long) (pot % n);
CrittografiaRSA.cod = Long.toString(c);
}
return CrittografiaRSA.cod;
}
}
In particular, the program freezes right here: 特别是,程序在此处冻结:
if(NewJFrame.generateE==true){
while(GCD == false || e>=pqless || e==1){
e = rand.nextInt(pqless);
System.out.println(e);
GCD = EuclideAlgorithm(e, pqless);
}
}
I noticed, however, that the smaller are p and q, the higher are the chances of to bug. 但是,我注意到p和q越小,发生错误的机会就越高。
Hope the code is understandable, I removed the italian comments 'cause IDK if the whole code is really necessary, but whatever. 希望代码是可以理解的,我删除了意大利语注释,因为如果确实需要整个代码,则可以使用IDK,但无论如何。 Thanks for helping me. 谢谢你帮我
while(b != 0)
{
r = a % b;
a = b;
b = r;
System.out.println(b); // add this
}
Is application still freezing after this modification? 修改后应用程序仍然冻结吗?
If p
or q
are equal to 1
then e
will always be equal to pqless
because rand.nextInt(pqless)
generates a random number between 0
and pqless
. 如果p
或q
等于1
则e
将始终等于pqless
因为rand.nextInt(pqless)
生成一个介于0
和pqless
之间的随机数。 With a value of 1
for p
and/or q
, pqless = 0
so e = 0
. 如果p
和/或q
的值为1
,则pqless = 0
所以e = 0
。 Thus, the infinite loop begins. 因此,无限循环开始。
pqless = (q - 1) * (p - 1);
if (NewJFrame.generateE == true) {
while (GCD == false || e >= pqless || e == 1) {
e = rand.nextInt(pqless);
System.out.println(e);
GCD = EuclideAlgorithm(e, pqless);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.