import java.security.GeneralSecurityException; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import com.citi.simpliciti.tempest.TempestRuntimeException; public class AdvancedEncryptionStandard { private static final String ALGORITHM = "AES"; private final SecretKeySpec secretKey; private final Cipher encoder; private final Cipher decoder; public AdvancedEncryptionStandard(byte[] key) { try { secretKey = new SecretKeySpec(key, ALGORITHM); encoder = Cipher.getInstance(ALGORITHM); encoder.init(Cipher.ENCRYPT_MODE, secretKey); decoder = Cipher.getInstance(ALGORITHM); decoder.init(Cipher.DECRYPT_MODE, secretKey); } catch (Exception e) { throw new TempestRuntimeException(e); } } /** * Encrypts the given plain text * * @param plainText The plain text to encrypt * @throws GeneralSecurityException */ public byte[] encrypt(byte[] plainText) throws GeneralSecurityException { synchronized (encoder) { return encoder.doFinal(plainText); } } /** * Decrypts the given byte array * * @param cipherText The data to decrypt * @throws GeneralSecurityException */ public byte[] decrypt(byte[] cipherText) throws GeneralSecurityException { synchronized (decoder) { return decoder.doFinal(cipherText); } } }