Friday, April 26, 2024
HomeJavaAES Encryption and Decryption in Java

AES Encryption and Decryption in Java


Hi there Java programmers, in case you are in search of an instance to encrypt and decrypt a take a look at utilizing the AES algorithm in Java then you could have come to the fitting place. Earlier, I’ve proven you Base 64 encode and decode a String in Java, and on this article, I’ll educate you encode and decode a string utilizing AES Algorithm.  If you do not know, AES stands for Superior Encryption Commonplace algorithm and it’s a symmetric block cipher that’s used to guard labeled data by many Authorities and Safety organizations. You should use AES to encrypt delicate knowledge in your software config file like database password, consumer login particulars and keys and so forth. In our instance we might be utilizing an AES Algorithm with AES – 128, AES has a set block dimension of 128 bits. We might be utilizing a password primarily based secret key.

The encrypted knowledge returned by doFinal is binary, and so it can’t be printed (it’s going to seem as a bunch of gibberish.) The Base64 encoding converts the binary to a set of ASCII characters, this makes it simply readable and in addition makes it doable to make use of the encrypted knowledge in conditions the place solely plaintext knowledge can be utilized.

The Base64 encoding does not add any further encryption or safety, it merely makes the encrypted knowledge usable in conditions the place you’ll be able to’t use binary. That is truly utilizing 128-bit AES, not 256. The secret is 16 bytes; 16 bytes * 8 bits per byte = 128 bit key

The ensuing AES-256 encrypted worth can include some uncommon characters that, when printed, or despatched over the web, might be modified or misunderstood, truncated, or changed throughout transmission or visible illustration.

Base64 supplies a mechanism to encode/decode values, to allow them to “journey” with out the content material being modified. The consumer who wrote this code you discovered, most likely would want to retailer or transport this worth.

This makes use of a 128-bit encryption key – java apparently will not do 256-bit encryption out-of-the-box. Here’s a good diagram which explains how encryption and decryption work to guard delicate knowledge like login particulars and password and so forth:

AES Encryption and Decryption in Java - AES Encoding Decoding Example


Java Program to Encrypt and Decrypt String utilizing AES Algorithm

Right here is our full Java program to encrypt and decrypt a string utilizing the AES Algorithm 

import java.safety.Key;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import solar.misc.BASE64Decoder;
import solar.misc.BASE64Encoder;
 

public class AESEncryptionDemo{
 
    non-public static ultimate String ALGO = "AES";
    non-public static ultimate byte[] aes_128bit_key = getByteArray(16);
    non-public static ultimate byte[] aes_256bit_key = getByteArray(32);
 
    public static void fundamental(String[] args) throws Exception {
 
        Scanner cmd = new Scanner(System.in);
        System.out.println("Please enter knowledge to be encrypted utilizing AES algorithm");
        String content material = cmd.nextLine();
       
        System.out.println("Would you like 128 bit encryption or 256 bit ?");
        int size = cmd.nextInt();
        byte[] keyValue = null;
        if(size == 128){
            keyValue = aes_128bit_key;
        }else{
            keyValue = aes_256bit_key;
        }
        String encypted = encrypt(content material, keyValue);
        String decrypted = decrypt(encypted, keyValue);
 
        System.out.println("Information in Plain Textual content : " + content material);
        System.out.println("Encrypted Textual content utilizing AES algorithm: " + encypted);
        System.out.println("Decrypted Textual content utilizing AES algo: " + decrypted);
    }
 
    public static String encrypt(String Information, byte[] keyValue) throws Exception {
        Key key = generateKey(keyValue);
        Cipher c = Cipher.getInstance("AES");
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Information.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");  
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }
 
    non-public static Key generateKey(byte[] worth) throws Exception {
        Key key = new SecretKeySpec(worth, "AES");  
        return key;
    }
   
    non-public static byte[] getByteArray(int bits){
        byte[] worth = new byte[bits];
        for(int i=0; i++){ 
       worth[i] = (byte) i; 
       } 
    return worth; 
 } 
} 

Output 
Please enter knowledge to be encrypted utilizing AES algorithm 
Java 8 is Useful and object oriented
Would you like 128 bit encryption or 256 bit ? 
128 
Information in Plain Textual content : Java 8 is Useful and object oriented 

Encrypted Textual content utilizing AES algorithm: 
i7N3jZKgFblP79tTctXzG0jIdc3qgeZwKyg6WxO5LMEKGUQUNLs+FYdJre6YtEB9

Decrypted Textual content utilizing AES algo: Java 8 is Useful and object oriented 

Please enter knowledge to be encrypted utilizing AES algorithm 
Great Java 
Would you like 128 bit encryption or 256 bit ? 
256 
Exception in thread "fundamental" java.safety.InvalidKeyException: 
Unlawful key dimension 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)

That is all about do encryption and decryption in AES Algorithm in Java. It is good for any Java developer to not simply aware of important encryption algorithms like blowfish, AES, DES, bas64 but in addition use them in Java programming language. Now a days you can’t retailer delicate data like password in file, they have to be encrypted and that is the place you want these instruments and performance to encrypt and decrypt password, keys, and different delicate data. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments