1+ package zomatosearch .example .com .zomatosearch ;
2+
3+ import java .io .File ;
4+ import java .io .FileInputStream ;
5+ import java .io .FileOutputStream ;
6+ import java .security .SecureRandom ;
7+ import java .util .Base64 ;
8+
9+ import javax .crypto .Cipher ;
10+ import javax .crypto .KeyGenerator ;
11+ import javax .crypto .SecretKey ;
12+ import javax .crypto .spec .IvParameterSpec ;
13+ import javax .crypto .spec .SecretKeySpec ;
14+
15+ public class AES_Encryption {
16+
17+ public static void main (String [] args ) throws Exception {
18+ //Generating Key For Encryption
19+ KeyGenerator keyGenerator = KeyGenerator .getInstance ("AES" );
20+ keyGenerator .init (128 );
21+ // Generate Key
22+ SecretKey key = keyGenerator .generateKey ();
23+
24+
25+ byte [] IV = new byte [16 ];
26+ SecureRandom random = new SecureRandom ();
27+ random .nextBytes (IV );
28+
29+
30+ //File to be red
31+ File inputFile = new File (System .getProperty ("user.dir" ) + "/rohit.txt" );
32+ //File to be written using cipher text
33+ File outputFileEnc = new File (System .getProperty ("user.dir" ) + "/rohit_enc.txt" );
34+ //Decrypted File
35+ File outputFileDec = new File (System .getProperty ("user.dir" ) + "/rohit_dec.txt" );
36+
37+ //Stream to read normal file
38+ FileInputStream inputStream = new FileInputStream (inputFile );
39+ //creating byte array of same length as of file
40+ byte [] inputBytes = new byte [(int ) inputFile .length ()];
41+ //Putting into byteArray
42+ inputStream .read (inputBytes );
43+ //getting the cipher text
44+ byte [] cipherText = encrypt (inputBytes , key , IV );
45+ //Writing encrypted text into file
46+ FileOutputStream outputStream = new FileOutputStream (outputFileEnc );
47+ outputStream .write (cipherText );
48+ outputStream = new FileOutputStream (outputFileDec );
49+ byte [] decrypted = new byte [(int ) outputFileEnc .length ()];
50+ FileInputStream fileInputStream = new FileInputStream (outputFileEnc );
51+ fileInputStream .read (decrypted );
52+ outputStream .write (decrypt (decrypted , key , IV ));
53+ inputStream .close ();
54+ outputStream .close ();
55+
56+ }
57+
58+ public static byte [] encrypt (byte [] plaintext , SecretKey key , byte [] IV ) throws Exception {
59+ //Get Cipher Instance
60+ Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5Padding" );
61+
62+ //Create SecretKeySpec
63+ SecretKeySpec keySpec = new SecretKeySpec (key .getEncoded (), "AES" );
64+
65+ //Create IvParameterSpec
66+ IvParameterSpec ivSpec = new IvParameterSpec (IV );
67+
68+ //Initialize Cipher for ENCRYPT_MODE
69+ cipher .init (Cipher .ENCRYPT_MODE , keySpec , ivSpec );
70+
71+ //Perform Encryption
72+ byte [] cipherText = cipher .doFinal (plaintext );
73+
74+ return cipherText ;
75+ }
76+
77+ public static byte [] decrypt (byte [] cipherText , SecretKey key , byte [] IV ) throws Exception {
78+ //Get Cipher Instance
79+ Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5Padding" );
80+
81+ //Create SecretKeySpec
82+ SecretKeySpec keySpec = new SecretKeySpec (key .getEncoded (), "AES" );
83+
84+ //Create IvParameterSpec
85+ IvParameterSpec ivSpec = new IvParameterSpec (IV );
86+
87+ //Initialize Cipher for DECRYPT_MODE
88+ cipher .init (Cipher .DECRYPT_MODE , keySpec , ivSpec );
89+
90+ //Perform Decryption
91+ byte [] decryptedText = cipher .doFinal (cipherText );
92+
93+ return decryptedText ;
94+ }
95+ }
0 commit comments