Friday, March 16, 2012

Java basic encryption decrypt util


Because I always need to do this for passwords in files and various utilities I thought I would post the code.

Note: it is more secure to encrypt one way (cryptographic hashing) this is for simple no hassle cases.



package za.co.test.common.util;



import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;


public class EncryptionUtil {
private static final byte[] SECRET_KEY = "TESTKEY2013StormersRugby".getBytes();
    public static byte[] encryptToByteArray(String input)
        throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException {
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] inputBytes = input.getBytes();
        inputBytes = cipher.doFinal(inputBytes);
        return Base64.encodeBase64(inputBytes);
    }

    public static String decryptByteArray(byte[] encryptionBytes)
        throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException {
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] recoveredBytes = Base64.decodeBase64(encryptionBytes);
        recoveredBytes = cipher.doFinal(recoveredBytes);
        String recovered = new String(recoveredBytes);
        return recovered;
    }

    public static String encrypt(String input)
        throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException,
               UnsupportedEncodingException {
        byte[] inputBytes = encryptToByteArray(input);
        return new String(inputBytes);
    }

    public static String decrypt(String encryptionBytes)
        throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException {
        return decryptByteArray(encryptionBytes.getBytes());
    }

    private static Key generateKey() {
        Key key = new SecretKeySpec(SECRET_KEY, ALGORITHM);
        return key;
    }

    public static void main(String[] args)
        throws Exception {
        if (args == null || args.length < 1 || args[0] == null || args[0].length() < 1) {
            throw new NullPointerException("Please enter a password. Usage : java za.co.test.common.util.EncryptionUtil ");
        }
        String password = args[0];
        String pwd = EncryptionUtil.encrypt(password);
        System.out.println("The encrypted password is ---> " + pwd);
    }
}


No comments:

Post a Comment