Package net.sourceforge.javautil.common.encryption.impl

Source Code of net.sourceforge.javautil.common.encryption.impl.SimpleEncryptionProvider

package net.sourceforge.javautil.common.encryption.impl;

import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.KeySpec;

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

import net.sourceforge.javautil.common.encode.EncodingAlgorithmAbstract;
import net.sourceforge.javautil.common.encryption.DecryptingInputStream;
import net.sourceforge.javautil.common.encryption.EncryptingOutputStream;
import net.sourceforge.javautil.common.encryption.IEncryptionProvider;
import net.sourceforge.javautil.common.exception.ThrowableManagerRegistry;

/**
* A simple/standard implementation for providing encryption services.
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: SimpleEncryptionProvider.java 2712 2011-01-03 00:32:01Z ponderator $
*/
public class SimpleEncryptionProvider extends EncodingAlgorithmAbstract implements IEncryptionProvider {
 
  public static final String ALGORITHM_AES = "AES";
 
  private final SecureRandom random = new SecureRandom();
  private final Key key;
  private final Cipher encryptor;
  private final Cipher decryptor;
 
  /**
   * @see #SimpleEncryptionProvider(String, SimpleEncryptionKey)
   */
  public SimpleEncryptionProvider(Key key) throws InvalidKeyException {
    this.key = key;
    this.encryptor = this.createEncryptingCipher();
    this.decryptor = this.createDecryptingCipher();
  }

  public String getAlgorithm() { return this.encryptor.getAlgorithm(); }

  public byte[] decrypt(byte[] encrypted) {
    try {
      return this.decryptor.doFinal(encrypted);
    } catch (IllegalBlockSizeException e) {
      throw ThrowableManagerRegistry.caught(e);
    } catch (BadPaddingException e) {
      throw ThrowableManagerRegistry.caught(e);
    }
  }

  public byte[] encrypt(byte[] original) {
    try {
      return this.encryptor.doFinal(original);
    } catch (IllegalBlockSizeException e) {
      throw ThrowableManagerRegistry.caught(e);
    } catch (BadPaddingException e) {
      throw ThrowableManagerRegistry.caught(e);
    }
  }

  public InputStream getDecoderStream(InputStream source) {
    return this.getDecryptingInputStream(source);
  }

  public OutputStream getEncoderStream(OutputStream target) {
    return this.getEncryptingOutputStream(target);
  }

  public InputStream getDecryptingInputStream(InputStream encrypted) {
    return new DecryptingInputStream(this, encrypted);
  }

  public OutputStream getEncryptingOutputStream(OutputStream original) {
    return new EncryptingOutputStream(this, original);
  }

  public Cipher createDecryptingCipher() {
    try {
      Cipher cipher = Cipher.getInstance(key.getAlgorithm());
      cipher.init(Cipher.DECRYPT_MODE, key, random);
      return cipher;
    } catch (NoSuchPaddingException e) {
      throw ThrowableManagerRegistry.caught(e);
    } catch (InvalidKeyException e) {
      throw ThrowableManagerRegistry.caught(e);
    } catch (NoSuchAlgorithmException e) {
      throw ThrowableManagerRegistry.caught(e);
    }
  }

  public Cipher createEncryptingCipher() {
    try {
      Cipher cipher = Cipher.getInstance(key.getAlgorithm());
      cipher.init(Cipher.ENCRYPT_MODE, key, random);
      return cipher;
    } catch (NoSuchPaddingException e) {
      throw ThrowableManagerRegistry.caught(e);
    } catch (InvalidKeyException e) {
      throw ThrowableManagerRegistry.caught(e);
    } catch (NoSuchAlgorithmException e) {
      throw ThrowableManagerRegistry.caught(e);
    }
  }

}
TOP

Related Classes of net.sourceforge.javautil.common.encryption.impl.SimpleEncryptionProvider

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.