Decrypt File with RSA Example

This program demonstrates decrypting data using RSA private key.

Problem Statement

Write a Java program that decrypts RSA encrypted data using private key.

Source Code

1 import java.security.*;
2 import javax.crypto.Cipher;
3 public class RSADecryptExample {
4 public static void main(String[] args) {
5 System.out.println("Eduinq RSA Decrypt Example");
6 try {
7 KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA");
8 kpg.initialize(2048);
9 KeyPair kp=kpg.generateKeyPair();
10 Cipher cipher=Cipher.getInstance("RSA");
11 cipher.init(Cipher.ENCRYPT_MODE,kp.getPublic());
12 byte[] encrypted=cipher.doFinal("Eduinq Secure Data".getBytes());
13 cipher.init(Cipher.DECRYPT_MODE,kp.getPrivate());
14 byte[] decrypted=cipher.doFinal(encrypted);
15 System.out.println("Eduinq Decrypted Data: "+new String(decrypted));
16 } catch(Exception e) {
17 System.out.println("Eduinq Exception: "+e.getMessage());
18 }
19 }
20 }

Program Output

					Eduinq RSA Decrypt Example
Eduinq Decrypted Data: Eduinq Secure Data
				

Explanation

RSA decryption uses private key. - Restores original data. - Exception handling ensures safe operations. - In enterprise systems, RSA decryption retrieves secure information. Conclusion: Shows decrypting data with RSA.

Quick Links to Explore