Decrypt File with AES Example
This program demonstrates decrypting a file using AES.
Problem Statement
Write a Java program that decrypts an AES encrypted file.
Source Code
| 1 | import java.io.*; |
| 2 | import javax.crypto.*; |
| 3 | import javax.crypto.spec.SecretKeySpec; |
| 4 | public class AESDecryptExample { |
| 5 | public static void main(String[] args) { |
| 6 | System.out.println("Eduinq AES Decrypt Example"); |
| 7 | try { |
| 8 | SecretKeySpec key=new SecretKeySpec("1234567890123456".getBytes(),"AES"); |
| 9 | Cipher cipher=Cipher.getInstance("AES"); |
| 10 | cipher.init(Cipher.DECRYPT_MODE,key); |
| 11 | FileInputStream fis=new FileInputStream("encrypted.aes"); |
| 12 | CipherInputStream cis=new CipherInputStream(fis,cipher); |
| 13 | FileOutputStream fos=new FileOutputStream("decrypted.txt"); |
| 14 | byte[] buffer=new byte[1024]; |
| 15 | int len; |
| 16 | while((len=cis.read(buffer))!=-1){ |
| 17 | fos.write(buffer,0,len); |
| 18 | } |
| 19 | cis.close();fis.close();fos.close(); |
| 20 | System.out.println("Eduinq File Decrypted Successfully"); |
| 21 | } catch(Exception e) { |
| 22 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 23 | } |
| 24 | } |
| 25 | } |
Program Output
Eduinq AES Decrypt Example Eduinq File Decrypted Successfully
Explanation
AES decryption reverses encryption using same key. - Restores original file content. - Exception handling ensures safe operations. - In enterprise systems, AES decryption retrieves secure data. Conclusion: Shows decrypting files with AES.