Hello Java programmers, if you are looking for an example to encrypt and decrypt a test using the AES algorithm in Java then you have come to the right place. Earlier, I have shown you how to Base 64 encode and decode a String in Java, and in this article, I will teach you how to encode and decode a string using AES Algorithm. If you don't know, AES stands for Advanced Encryption Standard algorithm and it is a symmetric block cipher that is used to protect classified information by many Government and Security organizations. You can use AES to encrypt sensitive data in your application config file like database password, user login details and keys etc. In our example we will be using an AES Algorithm with AES - 128, AES has a fixed block size of 128 bits. We will be using a password based secret key.
The encrypted data returned by doFinal is binary, and so it cannot be printed (it'll appear as a bunch of gibberish.) The Base64 encoding converts the binary to a set of ASCII characters, this makes it easily readable and also makes it possible to use the encrypted data in situations where only plaintext data can be used.
The Base64 encoding doesn't add any extra encryption or security, it simply makes the encrypted data usable in situations where you can't use binary. This is actually using 128-bit AES, not 256. The key is 16 bytes; 16 bytes * 8 bits per byte = 128 bit key
The resulting AES-256 encrypted value can contain some unusual characters that, when printed, or sent over the internet, can be modified or misunderstood, truncated, or replaced during transmission or visual representation.
Base64 provides a mechanism to encode/decode values, so they can "travel" without the content being modified. The user who wrote this code you found, probably would need to store or transport this value.
This uses a 128-bit encryption key - java apparently won't do 256-bit encryption out-of-the-box. Here is a nice diagram which explains how encryption and decryption work to protect sensitive data like login details and password etc:
Here is our complete Java program to encrypt and decrypt a string using the AES Algorithm
import java.security.Key;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* Java program encrypt and decrypt data in AES algorithm.
*/
public class AESEncryptionDemo{
private static final String ALGO = "AES";
private static final byte[] aes_128bit_key = getByteArray(16);
private static final byte[] aes_256bit_key = getByteArray(32);
public static void main(String[] args) throws Exception {
Scanner cmd = new Scanner(System.in);
System.out.println("Please enter data to be encrypted using AES algorithm");
String content = cmd.nextLine();
System.out.println("Do you want 128 bit encryption or 256 bit ?");
int length = cmd.nextInt();
byte[] keyValue = null;
if(length == 128){
keyValue = aes_128bit_key;
}else{
keyValue = aes_256bit_key;
}
String encypted = encrypt(content, keyValue);
String decrypted = decrypt(encypted, keyValue);
System.out.println("Data in Plain Text : " + content);
System.out.println("Encrypted Text using AES algorithm: " + encypted);
System.out.println("Decrypted Text using AES algo: " + decrypted);
}
public static String encrypt(String Data, byte[] keyValue) throws Exception {
Key key = generateKey(keyValue);
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
String encryptedValue = new BASE64Encoder().encode(encVal);
return encryptedValue;
}
public static String decrypt(String encryptedData, byte[] keyValue) throws Exception {
Key key = generateKey(keyValue);
Cipher c = Cipher.getInstance("AES"); // Use ALGO
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey(byte[] value) throws Exception {
Key key = new SecretKeySpec(value, "AES"); // Use ALGO
return key;
}
private static byte[] getByteArray(int bits){
byte[] value = new byte[bits];
for(int i=0; i++){
value[i] = (byte) i;
}
return value;
}
}
Output
Please enter data to be encrypted using AES algorithm
Java 8 is Functional and object oriented
Do you want 128 bit encryption or 256 bit ?
128
Data in Plain Text : Java 8 is Functional and object oriented
Encrypted Text using AES algorithm:
i7N3jZKgFblP79tTctXzG0jIdc3qgeZwKyg6WxO5LMEKGUQUNLs+FYdJre6YtEB9
Decrypted Text using AES algo: Java 8 is Functional and object oriented
Please enter data to be encrypted using AES algorithm
Wonderful Java
Do you want 128 bit encryption or 256 bit ?
256
Exception in thread "main" java.security.InvalidKeyException:
Illegal key size or default parameters at
javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1021)
at javax.crypto.Cipher.implInit(Cipher.java:796)
at javax.crypto.Cipher.chooseProvider(Cipher.java:859)
at javax.crypto.Cipher.init(Cipher.java:1229)
at javax.crypto.Cipher.init(Cipher.java:1166)
at AESEncryptionDemo.encrypt(AESEncryptionDemo.java:44)
No comments:
Post a Comment