AESEncryptor - symmetic key encryption made simple

Dependency-less AES encryption and decryption. If you stick to AES256 you probably have a good reason to do so...

			
/*
 * Lithium Utilities
 * 
 * Copyright 2005-2011 Tibor Bősze <tibor.boesze@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.lithium.utils.crypto;

import java.security.InvalidKeyException;
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;

/**
 * Utility class for AES encryption/decryption of short but sensitive data.
 * 
 * @author tibor.boesze@gmail.com
 * @version 1.0
 */
public class AESEncryptor {
	
	private static final String algo = "AES";
	private final SecretKeySpec keySpec;

	public AESEncryptor(byte[] key) {
		keySpec = new SecretKeySpec(key, algo);
	}
	
	public AESEncryptor(char[] base64key) {
		this(Base64.decode(base64key));
	}

	public AESEncryptor(String base64key) {
		this(Base64.decode(base64key));
	}

	public byte[] encrypt(byte[] raw) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, 
			IllegalBlockSizeException, BadPaddingException {
		Cipher c = Cipher.getInstance(algo);
		c.init(Cipher.ENCRYPT_MODE, keySpec);
		return c.doFinal(raw);
	}

	public byte[] decrypt(byte[] raw) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
			IllegalBlockSizeException, BadPaddingException {
		Cipher c = Cipher.getInstance(algo);
		c.init(Cipher.DECRYPT_MODE, keySpec);
		return c.doFinal(raw);
	}

	public String encryptToBase64(byte[] raw) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
			IllegalBlockSizeException, BadPaddingException {
		return new String(Base64.encode(encrypt(raw)));
	}

	public byte[] decryptFromBase64(String encb64) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, 
			IllegalBlockSizeException, BadPaddingException {
		return decrypt(Base64.decode(encb64));
	}
	
	public static void main(String[] args) throws Exception {
		
		if (args.length != 3) {
			System.out.println("Usage: {encrypt|decrypt} <base64key> <data>");
			return;
		}
		
		boolean decrypt = "decrypt".equalsIgnoreCase(args[0]);
		String key = args[1]==null?"MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY=":args[1];
		String data = args[2]==null?"example":args[2];
		
		AESEncryptor aes = new AESEncryptor(key);
		if (!decrypt) {
			System.out.println("encrypt(" + data + "): " + aes.encryptToBase64(data.getBytes()));
		} else {
			System.out.println("decrypt(" + data + "): " + new String(aes.decryptFromBase64(data)));
		}
	}

}
			
		
© 2003-2020 lithium.io7.org
Content on this site is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.