org.xlcloud.encryption.AESEncryptionService.java Source code

Java tutorial

Introduction

Here is the source code for org.xlcloud.encryption.AESEncryptionService.java

Source

/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * 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 org.xlcloud.encryption;

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;
import org.apache.log4j.Logger;

/**
 * Implementation of {@link EncryptionService} based on AES algorithm.
 * @author Konrad Krl, AMG.net
 */
public class AESEncryptionService implements EncryptionService {

    private Logger LOG = Logger.getLogger(AESEncryptionService.class);

    private static final String ENCRYPTION_ALGORITHM = "AES";
    private static final String ENCODING = "UTF-8";

    private String encryptionServiceKey;

    /**
     * Constructor of {@link AESEncryptionService}.  
     * @param encryptionServiceKey - base64 encoded AES encryption key. Maximum key length depends on installed JCE jurisdiction.
     */
    public AESEncryptionService(String encryptionServiceKey) {
        this.encryptionServiceKey = encryptionServiceKey;
    }

    /** {@inheritDoc} */
    public String encrypt(String message) throws EncryptionException {
        try {
            Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, buildEncryptionKey());

            byte[] encrypted = cipher.doFinal(message.getBytes(ENCODING));
            return Base64.encodeBase64String(encrypted);

        } catch (NoSuchAlgorithmException e) {
            LOG.error(e.getMessage());
            throw new EncryptionException(e.getMessage(), e);
        } catch (NoSuchPaddingException e) {
            LOG.error(e.getMessage());
            throw new EncryptionException(e.getMessage(), e);
        } catch (InvalidKeyException e) {
            LOG.error(e.getMessage());
            throw new EncryptionException(e.getMessage(), e);
        } catch (UnsupportedEncodingException e) {
            LOG.error(e.getMessage());
            throw new EncryptionException(e.getMessage(), e);
        } catch (IllegalBlockSizeException e) {
            LOG.error(e.getMessage());
            throw new EncryptionException(e.getMessage(), e);
        } catch (BadPaddingException e) {
            LOG.error(e.getMessage());
            throw new EncryptionException(e.getMessage(), e);
        }
    }

    /** {@inheritDoc} */
    public String decrypt(String encryptedMessage) throws EncryptionException {
        try {
            Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, buildEncryptionKey());

            byte[] decrypted = cipher.doFinal(Base64.decodeBase64(encryptedMessage));
            return new String(decrypted, ENCODING);

        } catch (InvalidKeyException e) {
            LOG.error(e.getMessage());
            throw new EncryptionException(e.getMessage(), e);
        } catch (UnsupportedEncodingException e) {
            LOG.error(e.getMessage());
            throw new EncryptionException(e.getMessage(), e);
        } catch (NoSuchAlgorithmException e) {
            LOG.error(e.getMessage());
            throw new EncryptionException(e.getMessage(), e);
        } catch (NoSuchPaddingException e) {
            LOG.error(e.getMessage());
            throw new EncryptionException(e.getMessage(), e);
        } catch (IllegalBlockSizeException e) {
            LOG.error(e.getMessage());
            throw new EncryptionException(e.getMessage(), e);
        } catch (BadPaddingException e) {
            LOG.error(e.getMessage());
            throw new EncryptionException(e.getMessage(), e);
        }
    }

    private Key buildEncryptionKey() throws UnsupportedEncodingException {
        return new SecretKeySpec(Base64.decodeBase64(encryptionServiceKey), ENCRYPTION_ALGORITHM);
    }
}