JAVA - AES Encryption and Decryption example

Advanced Encryption Standard which is a symmetric encryption algorithm. AES encryption is used by the 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.

The following illustration highlights how symmetric cryptography works:


Example:
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

public class AESDemo {

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

public static void main(String[] args)
throws NoSuchAlgorithmException {

// key
String secretKey = "your secret key";
// secret text
String originalString = "knowledgefactory.net";
// Encryption
String encryptedString =
AESDemo.encrypt
(originalString, secretKey);
// Decryption
String decryptedString =
AESDemo.decrypt
(encryptedString, secretKey);
// OriginalString,encryptedString,decryptedString
System.out.println("Original String:" + originalString);
System.out.println("Encrypted value:" + encryptedString);
System.out.println("Decrypted value:" + decryptedString);
}

// 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);
}
return null;
}

// method to decrypt 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);
}
return null;
}
}

Output:

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


More...

Popular posts from this blog

Spring boot video streaming example-HTML5

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

DataTable-Pagination example with Spring boot, jQuery and ajax

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Node JS mini projects with source code - free download

Spring boot web project free download:User Registration System

Spring Boot + Apache Commons Export Data to CSV Example