1+ package com .cdac .encrypt .aes ;
2+
3+ import java .io .UnsupportedEncodingException ;
4+ import java .security .MessageDigest ;
5+ import java .security .NoSuchAlgorithmException ;
6+ import java .util .Arrays ;
7+ import javax .crypto .Cipher ;
8+ import javax .crypto .spec .SecretKeySpec ;
9+ import java .util .Base64 ;
10+
11+ /*
12+ * import java.util.Base64; this class is available from java 1.8
13+ * else you can use apache library for Base64
14+ */
15+ public class AESEncryption {
16+
17+ public static void main (String [] args ) {
18+ final String strToEncrypt = "My text to encrypt" ;
19+ final String strPssword = "f2fc2007-b24b-4ab5-b62f-8dba873d0341" ;
20+ AESEncryption .setKey (strPssword );
21+ AESEncryption .encrypt (strToEncrypt .trim ());
22+ System .out .println ("String to Encrypt: " + strToEncrypt );
23+ System .out .println ("Encrypted: " + AESEncryption .getEncryptedString ());
24+ final String strToDecrypt = AESEncryption .getEncryptedString ();
25+ AESEncryption .decrypt (strToDecrypt .trim ());
26+ System .out .println ("String To Decrypt : " + strToDecrypt );
27+ System .out .println ("Decrypted : " + AESEncryption .getDecryptedString ());
28+ }
29+
30+ private static SecretKeySpec secretKey ;
31+ private static byte [] key ;
32+ private static String decryptedString ;
33+ private static String encryptedString ;
34+
35+ public static void setKey (String myKey ) {
36+ MessageDigest sha = null ;
37+ try {
38+ key = myKey .getBytes ("UTF-8" );
39+ System .out .println (key .length );
40+ sha = MessageDigest .getInstance ("SHA-256" );
41+ key = sha .digest (key );
42+ //key = Arrays.copyOf(key, 32);
43+ System .out .println (key .length );
44+ System .out .println (new String (key , "UTF-8" ));
45+ secretKey = new SecretKeySpec (key , "AES" );
46+ } catch (NoSuchAlgorithmException e ) {
47+ e .printStackTrace ();
48+ } catch (UnsupportedEncodingException e ) {
49+ e .printStackTrace ();
50+ }
51+ }
52+
53+ public static String getDecryptedString () {
54+ return decryptedString ;
55+ }
56+
57+ public static void setDecryptedString (String decString ) {
58+ decryptedString = decString ;
59+ }
60+
61+ public static String getEncryptedString () {
62+ return encryptedString ;
63+ }
64+
65+ public static void setEncryptedString (String encString ) {
66+ encryptedString = encString ;
67+ }
68+
69+ public static String encrypt (String strToEncrypt ) {
70+ try {
71+ Cipher cipher = Cipher .getInstance ("AES/ECB/PKCS5Padding" );
72+ cipher .init (Cipher .ENCRYPT_MODE , secretKey );
73+ byte [] buff = cipher .doFinal (strToEncrypt .getBytes ("UTF-8" ));
74+ setEncryptedString (Base64 .getEncoder ().encodeToString (buff ));
75+ } catch (Exception e ) {
76+ System .out .println ("Error while encrypting: " + e .toString ());
77+ }
78+ return null ;
79+ }
80+
81+ public static String decrypt (String strToDecrypt ) {
82+ try {
83+ Cipher cipher = Cipher .getInstance ("AES/ECB/PKCS5PADDING" );
84+ cipher .init (Cipher .DECRYPT_MODE , secretKey );
85+ setDecryptedString (new String (cipher .doFinal (Base64 .getDecoder ().decode (strToDecrypt ))));
86+ } catch (Exception e ) {
87+ System .out .println ("Error while decrypting: " + e .toString ());
88+ }
89+ return null ;
90+ }
91+ }
0 commit comments