JAVA- AES Encryption and Decryption with example

Why AES?

Advanced Encryption Standard which is a symmetric encryption algorithm. AES encryption is used by U.S. for securing sensitive but unclassified material, so we can say it is enough secure.
Advanced Encryption Standard(AES) is a symmetric encryption algorithm. AES is the industry standard as of now as it allows 128 bit, 192 bit and 256 bit encryption.Symmetric encryption is very fast as compared to asymmetric encryption and are used in systems such as database system.


Example:


import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AESKnowledgeFcatory {

private static SecretKeySpec secretKey;
private static byte[] key;

// set Key
public static void setKey(String myKey) {
MessageDigest sha = null;
try {
key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

// method to encrypt the secret text using key
public static String encrypt(String strToEncrypt, String secret) {
try {
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
} catch (Exception e) {
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}

// method to encrypt the secret text using key
public static String decrypt(String strToDecrypt, String secret) {
try {
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
} catch (Exception e) {
System.out.println("Error while decrypting: " + e.toString());
}
return null;
}

public static void main(String[] args) {
// key
final String secretKey = "sibin";
// secret text
String originalString = "knowledgefactory.net";
// Encryption
String encryptedString = AESKnowledgeFcatory.encrypt(originalString, secretKey);
// Decryption
String decryptedString = AESKnowledgeFcatory.decrypt(encryptedString, secretKey);
// Printing originalString,encryptedString,decryptedString
System.out.println("Original String:" + originalString);
System.out.println("Encrypted value:" + encryptedString);
System.out.println("Decrypted value:" + decryptedString);
}
}


Output:

Original String:knowledgefactory.net
Encrypted value:7W9JdoGEuWiV5EFdQxdyE+PYnj7WnkfeMhb3xU5uHgo=
Decrypted value:knowledgefactory.net



This article is contributed by Sibin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Comments