Hybrid AES+RSA Encryption Example

This program demonstrates hybrid encryption using AES and RSA.

Problem Statement

Write a Java program that encrypts data using AES and secures the AES key with RSA.

Source Code

1 import java.security.*;
2 import javax.crypto.*;
3 import javax.crypto.spec.SecretKeySpec;
4 public class HybridEncryptionExample {
5 public static void main(String[] args) {
6 System.out.println("Eduinq Hybrid Encryption Example");
7 try {
8 // Generate AES key
9 KeyGenerator kg=KeyGenerator.getInstance("AES");
10 kg.init(128);
11 SecretKey aesKey=kg.generateKey();
12 // Encrypt data with AES
13 Cipher aesCipher=Cipher.getInstance("AES");
14 aesCipher.init(Cipher.ENCRYPT_MODE,aesKey);
15 byte[] encryptedData=aesCipher.doFinal("Eduinq Hybrid Data".getBytes());
16 // Generate RSA keys
17 KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA");
18 kpg.initialize(2048);
19 KeyPair kp=kpg.generateKeyPair();
20 // Encrypt AES key with RSA
21 Cipher rsaCipher=Cipher.getInstance("RSA");
22 rsaCipher.init(Cipher.ENCRYPT_MODE,kp.getPublic());
23 byte[] encryptedKey=rsaCipher.doFinal(aesKey.getEncoded());
24 System.out.println("Eduinq Hybrid Encryption Done");
25 } catch(Exception e) {
26 System.out.println("Eduinq Exception: "+e.getMessage());
27 }
28 }
29 }

Program Output

					Eduinq Hybrid Encryption Example
Eduinq Hybrid Encryption Done
				

Explanation

AES encrypts bulk data efficiently. - RSA secures AES key for safe transfer. - Exception handling ensures safe operations. - In enterprise systems, hybrid encryption balances speed and security. Conclusion: Shows hybrid AES+RSA encryption.

Quick Links to Explore