Cipher Encryption and Decryption using Self Encryption Key.
Here are two different classes for Cipher Encryption and Decryption using Self Encryption Key.
Note: Verfy myEncryptionKey length (It should be 8, 16, 32 ……bytes)
package test;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.util.Scanner;
public class AESEncryptionTest {
private static final String ALGORITHM = “AES”;
//private static final String myEncryptionKey = “ThisIsFoundation”;
private static final String myEncryptionKey = “averylongtext!@$3434&&&&&&&&((((((34@#$#@$#*&(*&}{23432@#@#00000000232432432dsfsdf”;
private static final String UNICODE_FORMAT = “UTF8”;
public static String encrypt(String valueToEnc) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encValue = c.doFinal(valueToEnc.getBytes());
String encryptedValue = new BASE64Encoder().encode(encValue);
return encryptedValue;
}
private static Key generateKey() throws Exception {
byte[] keyAsBytes;
keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
Key key = new SecretKeySpec(keyAsBytes, ALGORITHM);
return key;
}
public static void main(String[] args) throws Exception {
String input = “”;
System.out.println(“Enter String to Encrypt:-“);
Scanner s = new Scanner(System.in);
input = s.next();
String valueEnc = AESEncryptionDecryptionTest.encrypt(input);
System.out.println(“Encrypted : ” + valueEnc);
}
}
************************************************************************************
package test;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.util.Scanner;
public class AESDecryptionTest {
private static final String ALGORITHM = “AES”;
//private static final String myEncryptionKey = “ThisIsFoundation”;
private static final String myEncryptionKey = “averylongtext!@$3434&&&&&&&&((((((34@#$#@$#*&(*&}{23432@#@#00000000232432432dsfsdf”;
private static final String UNICODE_FORMAT = “UTF8”;
public static String decrypt(String encryptedValue) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey() throws Exception {
byte[] keyAsBytes;
keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
Key key = new SecretKeySpec(keyAsBytes, ALGORITHM);
return key;
}
public static void main(String[] args) throws Exception {
String input = “”;
System.out.println(“Enter Encrypted String to Decrypt:-“);
Scanner s = new Scanner(System.in);
input = s.next();
// String valueEnc = “VCh4WpXtMDmMxTaPzvxj9A==”;
String valueDec = AESEncryptionDecryptionTest.decrypt(input);
System.out.println(“Decrypted : ” + valueDec);
}
}
Test Results:
Enter String to Encrypt:-
test
Encrypted : a/YBcxcz92oHcxGD8qLtcg==
Enter Encrypted String to Decrypt:-
a/YBcxcz92oHcxGD8qLtcg==
Decrypted : test
Good Luck.