Question: I need some help with this java program on NetBeans, I need to check where's my mistakes. package rsa; // Java Program to Implement the

I need some help with this java program on NetBeans, I need to check where's my mistakes.

package rsa;

// Java Program to Implement the RSA Algorithm import java.math.*; import java.util.*;

class RSA {

public static void main(String args[]) { int p, q, n, r, d = 0, e;

// The number to be encrypted and decrypted int msg = 12; double c; BigInteger msgback;

// 1st prime number p p = 5;

// 2nd prime number q q = 11; n = p * q; System.out.println("the value of n = " + n); r = (p - 1) * (q - 1); System.out.println("the value of ( n ) = " + r);

for (e = 2; e < r; e++) { // e is for public key exponent if (gcd(e, r) == 1) { break; } } System.out.println("the value of e = " + e); for (int i = 0; i <= 2; i++) { int x = 1 + (i * r);

// d is for private key exponent if (x % e == 0) { d = x / e; break; } } System.out.println("the value of d = " + d); c = (Math.pow(msg, e)) % n; System.out.println("Encrypted message is : " + c);

// converting int value of n to BigInteger BigInteger N = BigInteger.valueOf(n);

// converting float value of c to BigInteger BigInteger C = BigDecimal.valueOf(c).toBigInteger(); msgback = (C.pow(d)).mod(N); System.out.println("Decrypted message is : " + msgback); }

static int gcd(int e, int r) { if (e == 0) { return r; } else { return gcd(r % e, e); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!