0% found this document useful (0 votes)
10 views4 pages

Algorithm: Step 1

The document outlines the RSA algorithm, detailing the steps to generate a private key, encrypt a message, and decrypt it. It includes a Java code example demonstrating the process of finding the public and private keys, calculating the cipher text, and retrieving the original message. The output of the example shows the values of the public key, private key, cipher text, and decrypted message.

Uploaded by

swatisharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Algorithm: Step 1

The document outlines the RSA algorithm, detailing the steps to generate a private key, encrypt a message, and decrypt it. It includes a Java code example demonstrating the process of finding the public and private keys, calculating the cipher text, and retrieving the original message. The output of the example shows the values of the public key, private key, cipher text, and decrypted message.

Uploaded by

swatisharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

The RSA technique contains three parts.

In the first part, we need to find the


private key. In the second part, we need to encrypt the message, and in the last
part, we need to decrypt the message.

Below, we have given the step-by-step guide for writing the RSA algorithm.

Algorithm

Step 1 - Initialize the 'd' variable with 0, a private key, and 'e' to store the
exponent. Also, define the prime1 and prime2 variables and initialize them.

Step 2 - Also, Initialize the message with a positive integer value.

Step 3 - After that, multiply prime1 and prime2 and store them in the primeMul
variable.

Step 4 - Next, multiply prime1 - 1 and prime2 - 1 and store them in the
primeMul1 variable.

Step 5 - Now, we need to find the value of 'e' in such a way so that the GCD of
'e' and primeMul1 is 1. The value of e can be between 2 and primeMul1.

Step 5.1 - In the getGCD() function, return the num value if the mod is zero.
Otherwise, recursively call the getGCD() function.

Step 6 - Our public key is {e, n}.

Step 7 - Now, find the private key.

Step 7.1 - Traverse through 1 to 9 digits. In the loop, if 1 + (m * primeMul1) is


divisible by 'e', store (1 + (m * primeMul1))/e to the 'd', which will be used to
create a private key.

Step 8 - Our private key is {d, n}.


Step 9 - To get the cipher text, use the Math.pow() method to find the message
and take its modulo with the primeMul variable's value.

Step 10 - We got the cipher text successfully. We need to decrypt the cipher
text to convert it into plain text.

Step 11 - To get the plain text again, we need to take (cipherd % primeMul).

Example

import java.math.*;
import java.util.*;
public class Main {
public static int getGCD(int mod, int num) {
// If the mod is zero, return the num
if (mod == 0)
return num;
else
// recursive function call
return getGCD(num % mod, mod);
}
public static void main(String args[]) {
int d = 0, e; // Intialization
int message = 32; // number message
int prime1 = 5; // 1st prime number p
int prime2 = 7; // 2nd prime number q
int primeMul = prime1 * prime2; // performing
operations
int primeMul1 = (prime1 - 1) * (prime2 - 1);
System.out.println("primeMul1 is equal to : " +
primeMul1 + "\n");
// Finding the valid public key
for (e = 2; e < primeMul1; e++) {
// Here e is a public key
if (getGCD(e, primeMul1) == 1) {
break;
}
}
// Printing the public key
System.out.println("Public key e is = " + e);
// Calculating the private key
for (int m = 0; m <= 9; m++) {
// get the value of temp
int temp = 1 + (m * primeMul1);
// private key
if (temp % e == 0) {
d = temp / e;
break;
}
}
System.out.println("d is : " + d);
double cipher;
BigInteger d_message;
// getting the cipher text
cipher = (Math.pow(message, e)) % primeMul;
System.out.println("Cipher text is : " + cipher);
// Int to BigInteger
BigInteger bigN = BigInteger.valueOf(primeMul);
// Float to bigINt
BigInteger bigC =
BigDecimal.valueOf(cipher).toBigInteger();
// decrypting the message
d_message = (bigC.pow(d)).mod(bigN);
// print decrypted message
System.out.println("Decrypted text is : " +
d_message);
}
}

Output
primeMul1 is equal to : 24
Public key e is = 5
d is : 5
Cipher text is : 2.0
Decrypted text is : 32

You might also like