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:
Lớp TreeMap trong Java
Java Program to Generate Randomized Sequence of Given Range of Numbers
Java – Write to File
Send email with authentication
How to Set TLS Version in Apache HttpClient
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Split a String in Java
The Spring @Controller and @RestController Annotations
Introduction to Spring Method Security
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Spring 5 Testing with @EnabledIf Annotation
Hướng dẫn Java Design Pattern – Chain of Responsibility
Difference Between Wait and Sleep in Java
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
Java Program to Implement Binomial Heap
Java Program to Implement TreeMap API
Spring Autowiring of Generic Types
Java Program to Implement Maximum Length Chain of Pairs
Java – Try with Resources
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Spring Boot - Code Structure
Java Collections Interview Questions
File Upload with Spring MVC
Custom Exception trong Java
Java Program to Implement Circular Singly Linked List
Java Program to Implement Floyd-Warshall Algorithm
Jackson vs Gson
Remove All Occurrences of a Specific Value from a List
Logout in an OAuth Secured Application
Removing all Nulls from a List in Java
Java Program to Implement Min Heap
Introduction to Spring Cloud CLI