This is a java program to implement RSA algorithm. RSA is one of the first practicable public-key cryptosystems and is widely used for secure data transmission. In such a cryptosystem, the encryption key is public and differs from the decryption key which is kept secret. In RSA, this asymmetry is based on the practical difficulty of factoring the product of two large prime numbers, the factoring problem. RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman.
Here is the source code of the Java Program to Implement the RSA Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
package com.maixuanviet.setandstring; import java.io.DataInputStream; import java.io.IOException; import java.math.BigInteger; import java.util.Random; public class RSA { private BigInteger p; private BigInteger q; private BigInteger N; private BigInteger phi; private BigInteger e; private BigInteger d; private int bitlength = 1024; private Random r; public RSA() { r = new Random(); p = BigInteger.probablePrime(bitlength, r); q = BigInteger.probablePrime(bitlength, r); N = p.multiply(q); phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE)); e = BigInteger.probablePrime(bitlength / 2, r); while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0) { e.add(BigInteger.ONE); } d = e.modInverse(phi); } public RSA(BigInteger e, BigInteger d, BigInteger N) { this.e = e; this.d = d; this.N = N; } @SuppressWarnings("deprecation") public static void main(String[] args) throws IOException { RSA rsa = new RSA(); DataInputStream in = new DataInputStream(System.in); String teststring; System.out.println("Enter the plain text:"); teststring = in.readLine(); System.out.println("Encrypting String: " + teststring); System.out.println("String in Bytes: " + bytesToString(teststring.getBytes())); // encrypt byte[] encrypted = rsa.encrypt(teststring.getBytes()); // decrypt byte[] decrypted = rsa.decrypt(encrypted); System.out.println("Decrypting Bytes: " + bytesToString(decrypted)); System.out.println("Decrypted String: " + new String(decrypted)); } private static String bytesToString(byte[] encrypted) { String test = ""; for (byte b : encrypted) { test += Byte.toString(b); } return test; } // Encrypt message public byte[] encrypt(byte[] message) { return (new BigInteger(message)).modPow(e, N).toByteArray(); } // Decrypt message public byte[] decrypt(byte[] message) { return (new BigInteger(message)).modPow(d, N).toByteArray(); } }
Output:
$ javac RSA.java $ java RSA Enter the plain text: MaiXuanViet Encrypting String: MaiXuanViet String in Bytes: 8397110102111117110100114121 Decrypting Bytes: 8397110102111117110100114121 Decrypted String: MaiXuanViet
Related posts:
Java Program to Implement Aho-Corasick Algorithm for String Matching
How to Get All Spring-Managed Beans?
Java Program to find the maximum subarray sum using Binary Search approach
Java Program to Implement Segment Tree
Java Program to Implement Tarjan Algorithm
Convert String to int or Integer in Java
Java Program to Implement ArrayDeque API
Java Program to Implement the One Time Pad Algorithm
Spring Boot Configuration with Jasypt
Serve Static Resources with Spring
Guide to UUID in Java
Check If Two Lists are Equal in Java
Giới thiệu Google Guice – Dependency injection (DI) framework
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Setting the Java Version in Maven
Java Program to Solve a Matching Problem for a Given Specific Case
Convert char to String in Java
Java Program to Represent Graph Using Linked List
Comparing Strings in Java
Date Time trong Java 8
Introduction to the Functional Web Framework in Spring 5
Spring Boot - Enabling Swagger2
Spring Data – CrudRepository save() Method
Reactive WebSockets with Spring 5
Java Program to Permute All Letters of an Input String
Java Program to Implement Triply Linked List
Most commonly used String methods in Java
Java Program to Construct an Expression Tree for an Postfix Expression
Spring Security OAuth Login with WebFlux
How to Set TLS Version in Apache HttpClient
Introduction to the Java NIO2 File API
Jackson – Change Name of Field