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

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

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

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.KeySpec;

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

/**
* A simple composite that can generate a valid key to
* use with a {@link SimpleEncryptionProvider}.
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: SimpleEncryptionKey.java 2712 2011-01-03 00:32:01Z ponderator $
*/
public class SimpleEncryptionKey implements Key {
 
  /**
   * Create a key of the proper length in a consistent way using the
   * original key which can be of any length.
   *
   * @param originalKey The original key.
   * @return A key that can be used with a {@link SimpleEncryptionProvider}.
   */
  public static SimpleEncryptionKey createUsing (Strength strength, String algorithm, byte[] originalKey) {
    byte[] key = new byte[ strength.bytes ];
   
    if (key.length < originalKey.length)
      System.arraycopy(originalKey, 0, key, 0, key.length);
    else {
      System.arraycopy(originalKey, 0, key, 0, originalKey.length);
      int offset = originalKey.length;
      while (offset < key.length) {
        if (offset + originalKey.length < key.length) {
          System.arraycopy(originalKey, 0, key, offset, originalKey.length);
          offset += originalKey.length;
        } else {
          System.arraycopy(originalKey, 0, key, offset, key.length - offset);
          break;
        }
      }
    }
   
    return new SimpleEncryptionKey(strength, algorithm, key);
  }
 
  /**
   * The strength of the key.
   *
   * @author elponderador
   * @author $Author: ponderator $
   * @version $Id: SimpleEncryptionKey.java 2712 2011-01-03 00:32:01Z ponderator $
   */
  public enum Strength {
   
    STANDARD(8), STRONG(16), STRONGEST(32);
   
    private int bytes;
   
    Strength(int bytes) { this.bytes = bytes; }
   
    public int getBytes () { return this.bytes; }
   
  }

  /**
   * The key to use.
   */
  private final Key key;
 
  /**
   * @param strength The strength of the key
   * @param key The length of the key
   */
  public SimpleEncryptionKey (Strength strength, String algorithm, byte[] key) {
    if (key.length != strength.bytes) throw new IllegalArgumentException("Key is not long enough for the strength");
    this.key = this.createKeySpec(key, algorithm);
  }
 
  @Override public String getFormat() {
    return key.getFormat();
  }

  @Override public byte[] getEncoded() {
    return key.getEncoded();
  }

  @Override public String getAlgorithm() {
    return key.getAlgorithm();
  }

  /**
   * @param algorithm The algorithm for which to make a key spec
   * @return The key spec according to the length of the key
   */
  private SecretKeySpec createKeySpec (byte[] key, String algorithm) { return new SecretKeySpec(key, algorithm); }
 
}
TOP

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

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.