# Java Program to Implement the RSA Algorithm

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

Output:

```\$ 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.