Java Program to Implement the RSA Algorithm Last Updated : 27 Apr, 2021 Comments Improve Suggest changes Like Article Like Report RSA or Rivest–Shamir–Adleman is an algorithm employed by modern computers to encrypt and decrypt messages. It is an asymmetric cryptographic algorithm. Asymmetric means that there are two different keys. This is also called public-key cryptography because one among the keys are often given to anyone. The other is the private key which is kept private. The algorithm is predicated on the very fact that finding the factors of an outsized number is difficult: when the factors are prime numbers, the matter is named prime factorization. It is also a key pair (public and personal key) generator. Example: Generating Public Key 1. Select two prime no's. Suppose P = 53 and Q = 59. Now First part of the Public key : n = P*Q = 3127. 2. We also need a small exponent say e : But e Must be -An integer. -Not be a factor of n. -1 < e < Φ(n) [Φ(n) is discussed below], Let us now consider it to be equal to 3. The public key has been made of n and e Generating Private Key 1. We need to calculate Φ(n) : Such that Φ(n) = (P-1)(Q-1) so, Φ(n) = 3016 2. Now calculate Private Key, d : d = (k*Φ(n) + 1) / e for some integer k 3. For k = 2, value of d is 2011. The private key has been made of dImplementation of RSA Algorithm: Consider two prime numbers p and q.Compute n = p*qCompute Ï•(n) = (p – 1) * (q – 1)Choose e such gcd(e , Ï•(n) ) = 1Calculate d such e*d mod Ï•(n) = 1Public Key {e,n} Private Key {d,n}Cipher text C = Pe mod n where P = plaintextFor Decryption D = Dd mod n where D will refund the plaintext.Below is the implementation of the above approach: Java // 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, z, d = 0, e, i; // The number to be encrypted and decrypted int msg = 12; double c; BigInteger msgback; // 1st prime number p p = 3; // 2nd prime number q q = 11; n = p * q; z = (p - 1) * (q - 1); System.out.println("the value of z = " + z); for (e = 2; e < z; e++) { // e is for public key exponent if (gcd(e, z) == 1) { break; } } System.out.println("the value of e = " + e); for (i = 0; i <= 9; i++) { int x = 1 + (i * z); // 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 z) { if (e == 0) return z; else return gcd(z % e, e); } } Comment More infoAdvertise with us Next Article Java Program to Implement the RSA Algorithm M mayanktyagi1709 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Java Program to Implement the Karatsuba Multiplication Algorithm It is a replacement for the algorithm that we have used since childhood, which is mainly for multiplying numbers of bigger digits. The Karatsuba Algorithm is used for the fast multiplication of large numbers, using a famous technique called as the Divide and Conquer ,developed by Anatolii Alexeevitc 4 min read Java Program to Implement Naor-Reingold Pseudo Random Function Naor-Reingold Pseudo-Random Function is a function of generating random numbers. Moni Naor and Omer Reingold described efficient constructions for various cryptographic primitives in the private key as well as public-key cryptography. Example: Input : N = 5 Output: 9.0, 9.0, 3.0, 9.0, 3.0 Input : N 2 min read Java Program to Rotate digits of a given number by K Given two integers N and K, the task is to rotate the digits of N by K. If K is a positive integer, left rotate its digits. Otherwise, right rotate its digits. Examples: Input: N = 12345, K = 2Output: 34512 Explanation: Left rotating N(= 12345) by K(= 2) modifies N to 34512. Therefore, the required 2 min read Java Program to Convert Binary to Octal A Binary (base 2) number is given, and our task is to convert it into an Octal (base 8) number. There are different ways to convert binary to decimal, which we will discuss in this article.Example of Binary to Octal Conversion:Input : 100100Output: 44Input : 1100001Output : 141A Binary Number System 5 min read Java Program to Find GCD and LCM of Two Numbers Using Euclidâs Algorithm GCD or the Greatest Common Divisor of two given numbers A and B is the highest number dividing both A and B completely, i.e., leaving remainder 0 in each case. LCM or the Least Common Multiple of two given numbers A and B is the Least number which can be divided by both A and B, leaving remainder 0 3 min read Java Program to Convert Integer Values into Binary Given an integer in Java, your task is to write a Java program to convert this given integer into a binary number. Example: Input: = 45 Output: = 101101 Input: = 32 Output: = 100000 Integers: Integers are numbers whose base value is 10. The Integer or int data type is a 32-bit signed twoâs complemen 4 min read Java Program to Perform the Unique Factorization of a Given Number Given a number n, the task is to write an efficient program to print all unique prime factors of n. Example Input: 12 Output: 2, 3 Explanation: All prime factors or 12 are 2, 2, 3. In those factors, unique factors are 2 and 3. Input: 315 Output: 3, 5, 7 Steps to find all prime factors 1) While n is 6 min read Java Program For Decimal to Hexadecimal Conversion Given a decimal number N, convert N into an equivalent hexadecimal number i.e. convert the number with base value 10 to base value 16. The decimal number system uses 10 digits 0-9 and the Hexadecimal number system uses 0-9, A-F to represent any numeric value.Examples of Decimal to Hexadecimal Conver 3 min read Java implementation of Digital Signatures in Cryptography Digital Signatures are an Asymmetrically encrypted hash of a digital message(data). It is a value that can provide a guarantee of authenticity, non-repudiation, and integrity. In other terms, it means you can verify the sender, date & time and message content have not been revealed or compromise 4 min read Reverse Number Program in Java In Java, reversing a number means that the digit at the first position should be swapped with the last digit, the second digit will be swapped with the second last digit, and so on, till the middle element.Example of reversing a number:Input: n = 357Output: 753Input n = 100Output: 1 ( leading zeros 3 min read Like