# RSA.java developing an application of rsa algorithm with java ### Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

### Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

### C Programming Examples

Simple C Programs
C – Arrays
C – Matrix
C – Strings
C – Bitwise Operations
C – Linked Lists
C – Stacks & Queues
C – Searching & Sorting
C – Trees
C – Strings
C – File Handling
C – Mathematical Functions
C – Puzzles & Games
C Programs – Recursion
C Programs – No Recursion

### Java Algorithms

Java – Numerical Problems
Java – Combinatorial Problems
Java – Graph Problems
Java – Hard Graph Problems
Java – Computation Geometry
Java – Sets & Strings
Java – Data-Structures
Java – Collection API Problems

### C++ Algorithms

C++ – Numerical Problems
C++ – Combinatorial Problems
C++ – Graph Problems
C++ – Hard Graph Problems
C++ – Computation Geometry
C++ – Sets & Strings
C++ – Data-Structures
C++ – STL Library

### C Algorithms

C – Numerical Problems
C – Combinatorial Problems
C – Graph Problems
C – Hard Graph Problems
C – Computation Geometry
C – Sets & Strings
C – Data-Structures

« Prev Page
Next Page »

# Java Program to Implement the RSA Algorithm

Posted on

by
dharmendra

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.

1. ` `
2. `package com.sanfoundry.setandstring;`
3. ` `
4. `import java.io.DataInputStream;`
5. `import java.io.IOException;`
6. `import java.math.BigInteger;`
7. `import java.util.Random;`
8. ` `
9. `public class RSA`
10. ` private BigInteger p;`
11. ` private BigInteger q;`
12. ` private BigInteger N;`
13. ` private BigInteger phi;`
14. ` private BigInteger e;`
15. ` private BigInteger d;`
16. ` private int bitlength = 1024;`
17. ` private Random r;`
18. ` `
19. ` public RSA()`
20. ` `
21. ` r = new Random();`
22. ` p = BigInteger.probablePrime(bitlength, r);`
23. ` q = BigInteger.probablePrime(bitlength, r);`
24. ` N = p.multiply(q);`
25. ` phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));`
26. ` e = BigInteger.probablePrime(bitlength / 2, r);`
27. ` while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0)`
28. ` `
29. ` e.add(BigInteger.ONE);`
30. ` `
31. ` d = e.modInverse(phi);`
32. ` `
33. ` `
34. ` public RSA(BigInteger e, BigInteger d, BigInteger N)`
35. ` `
36. ` this.e = e;`
37. ` this.d = d;`
38. ` this.N = N;`
39. ` `
40. ` `
41. ` @SuppressWarnings("deprecation")`
42. ` public static void main(String[] args) throws IOException`
43. ` `
44. ` RSA rsa = new RSA();`
45. ` DataInputStream in = new DataInputStream(System.in);`
46. ` String teststring;`
47. ` System.out.println("Enter the plain text:");`
48. ` teststring = in.readLine();`
49. ` System.out.println("Encrypting String: " + teststring);`
50. ` System.out.println("String in Bytes: "`
51. ` + bytesToString(teststring.getBytes()));`
52. ` // encrypt`
53. ` byte[] encrypted = rsa.encrypt(teststring.getBytes());`
54. ` // decrypt`
55. ` byte[] decrypted = rsa.decrypt(encrypted);`
56. ` System.out.println("Decrypting Bytes: " + bytesToString(decrypted));`
57. ` System.out.println("Decrypted String: " + new String(decrypted));`
58. ` `
59. ` `
60. ` private static String bytesToString(byte[] encrypted)`
61. ` `
62. ` String test = "";`
63. ` for (byte b : encrypted)`
64. ` `
65. ` test += Byte.toString(b);`
66. ` `
67. ` return test;`
68. ` `
69. ` `
70. ` // Encrypt message`
71. ` public byte[] encrypt(byte[] message)`
72. ` `
73. ` return (new BigInteger(message)).modPow(e, N).toByteArray();`
74. ` `
75. ` `
76. ` // Decrypt message`
77. ` public byte[] decrypt(byte[] message)`
78. ` `
79. ` return (new BigInteger(message)).modPow(d, N).toByteArray();`
80. ` `

Output:

advertisement

```\$ javac RSA.java
\$ java RSA

Enter the plain text:
Sanfoundry
Encrypting String: Sanfoundry
String in Bytes: 8397110102111117110100114121
Decrypting Bytes: 8397110102111117110100114121
Decrypted String: Sanfoundry```

Sanfoundry Global Education  Learning Series – 1000 Java Programs.

advertisement

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.

« Prev Page – Java Program to Implement the Checksum Method for Small String Messages and Detect If the Received message is same as the Transmitted
» Next Page – Java Program to Implement the MD5 Algorithm

« Java Program to Implement the Checksum Method for Small String Messages and Detect If the Received message is same as the Transmitted
Java Program to Implement the MD5 Algorithm »  Manish Bhojasia , a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+