This commit is contained in:
Libkyy
2022-04-21 14:47:17 +02:00
parent 404910b1c1
commit 6ada5362b3
2 changed files with 63 additions and 0 deletions

BIN
src/RSA/RSA.class Normal file

Binary file not shown.

63
src/RSA/RSA.java Normal file
View File

@@ -0,0 +1,63 @@
public class RSA
{
double q,alpha,Xa,Xb;
public static void main(String[] args)
{
RSA pair1 = new RSA(71, 7, 5, 12);
System.out.println(pair1.getYa());
System.out.println(pair1.getYb());
System.out.println(pair1.getKey());
}
RSA(double q , double alpha, double Xa, double Xb)
{
if(isPrime(q))
{
this.q = q;
this.alpha = alpha;
this.Xa = Xa;
this.Xb = Xb;
}
else
System.out.println("q must be prime!");
}
public double getYa()
{
return Math.pow(alpha,Xa) % q;
}
public double getYb()
{
return Math.pow(alpha,Xb) % q;
}
public double getKey()
{
double Kab,Kba;
Kab = Math.pow(getYb(),Xa) % (int) q;
System.out.println(Kab);
Kba = Math.pow(getYa(),Xb) % (int) q;
System.out.println(Kba);
if(Kab == Kba)
return Kab;
else if(Math.abs(Kab - Kba) <= 1.9 && Kab > Kba)
return Kab;
else if(Math.abs(Kab - Kba) <= 1.9 && Kba > Kab)
return Kba;
else
return 0;
}
//Check if number is prime
public static boolean isPrime(double n)
{
for(double i=2;i<n;i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}
}