How do i make encrypt text in java - Java Encryption Decryption AES algorithm

In this java example we are going to learn how to do encrypt data in java program and decrypt the encrpted data. To make the data secure is major part in any programming languages. In this java example to Encrypt data will use AES encryption algorithm by AES Cipher (CBC) with 128 bit key. 

EncDecr.java

public class EncDecr {

    private static String CIPHER_NAME = "AES/CBC/PKCS5PADDING";
    private static int CIPHER_KEY_LEN = 16; //128 bits

    
    public static String encrypt(String keyStr, String ivStr, String data) {
        try {
            if (keyStr.length() < EncDecr.CIPHER_KEY_LEN) {
                int numPad = EncDecr.CIPHER_KEY_LEN - key.length();
                
                for(int i = 0; i < numPad; i++){
                    keyStr += "0"; //0 pad to len 16 bytes
                }
                
            } else if (keyStr.length() > EncDecr.CIPHER_KEY_LEN) {
                keyStr = keyStr.substring(0, CIPHER_KEY_LEN); //truncate to 16 bytes
            }
            
            
            IvParameterSpec initVector = new IvParameterSpec(ivStr.getBytes("ISO-8859-1"));
            SecretKeySpec skeySpec = new SecretKeySpec(keyStr.getBytes("ISO-8859-1"), "AES");

            Cipher cipher = Cipher.getInstance(EncDecr.CIPHER_NAME);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, initVector);

            byte[] encryptedData = cipher.doFinal((data.getBytes()));
            
            String base64_EncrData = Base64.getEncoder().encodeToString(encryptedData);
            String base64_IV_data = Base64.getEncoder().encodeToString(iv.getBytes("ISO-8859-1"));
            
            return base64_EncrData + ":" + base64_IV_data;
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

   
    public static String decrypt(String key, String data) {
        try {
            if (key.length() < EncDecr.CIPHER_KEY_LEN) {
                int numPad = EncDecr.CIPHER_KEY_LEN - key.length();
                
                for(int i = 0; i < numPad; i++){
                    key += "0"; //0 pad to len 16 bytes
                }
                
            } else if (key.length() > EncDecr.CIPHER_KEY_LEN) {
                key = key.substring(0, CIPHER_KEY_LEN); //truncate to 16 bytes
            }
            
            String[] parts = data.split(":");
            
            IvParameterSpec iv = new IvParameterSpec(Base64.getDecoder().decode(parts[1]));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("ISO-8859-1"), "AES");

            Cipher cipher = Cipher.getInstance(EncDecr.CIPHER_NAME);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

            byte[] decodedEncryptedData = Base64.getDecoder().decode(parts[0]);

            byte[] original = cipher.doFinal(decodedEncryptedData);

            return new String(original);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

}

 

 

in the above the arguments are
key - key to use should be 16 bytes long (128 bits)
iv - initialization vector
data - data to encrypt

 

How to call these Encrypt/Decrypt meethods, Lets see

JavaAES.java

 

public class DemoDriver {

    public static void main(String[] args) {

        String keyStr = "0123456789abcdef"; // 128 bit key
        String initVector = "fedcba9876543210"; // 16 bytes IV

        System.out.println(decrypt(keyStr,
                encrypt(keyStr, initVector, "Hi")));
        
         System.out.println(decrypt(keyStr, "Kx+c85B7tWjre4j6cdXHJQ==:ZmVkY2JhOTg3NjU0MzIxMA==")); 
    }
}

 

 

Here key should be our encrypt key

initVector should be 16 bytes length

 

Conclusion: In this Java example we learn how to encrypt the text in java and How do you decrypt the encrypted text in java with AES algorithm.

Related

Java HTTP Server Example

Create a new file in Java