Class SecretKeyEncryptor

java.lang.Object
org.deltava.crypt.SecretKeyEncryptor
Direct Known Subclasses:
AESEncryptor

public abstract class SecretKeyEncryptor extends Object
A class to support encrypting/decrypting data using a symetric secret key.
Since:
1.0
Version:
10.1
Author:
Luke
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Create a new encryptor using a given algorithm.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    decrypt(byte[] data)
    Decrypt a given amount of data.
    byte[]
    encrypt(byte[] data)
    Encrypt a given amount of data.
    final String
    Returns the algorithm in use.
    protected void
    Initialize the encryptor using a given key specification.
    protected void
    setKey(SecretKey key, byte[] iv)
    Initialize the encryptor using a given secret key.
    protected static byte[]
    trimKeySize(byte[] rawKey, int maxSize)
    Helper method to trim a key if it is longer than a particular size.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SecretKeyEncryptor

      protected SecretKeyEncryptor(String algorithm)
      Create a new encryptor using a given algorithm.
      Parameters:
      algorithm - the encryption algorithm to use.
  • Method Details

    • initKey

      protected void initKey(KeySpec spec)
      Initialize the encryptor using a given key specification.
      Parameters:
      spec - the secret Key information
      Throws:
      CryptoException - if the Secret Key cannot be initialized by the JVM
    • setKey

      protected void setKey(SecretKey key, byte[] iv)
      Initialize the encryptor using a given secret key.
      Parameters:
      key - the secret Key
      iv - an optional initialization vector
      Throws:
      CryptoException - if the cipher cannot be initialized by the JVM
    • getAlgorithm

      public final String getAlgorithm()
      Returns the algorithm in use.
      Returns:
      the algorithm name
    • encrypt

      public byte[] encrypt(byte[] data) throws CryptoException
      Encrypt a given amount of data.
      Parameters:
      data - the data to encrypt
      Returns:
      the encrypted data
      Throws:
      CryptoException - if something bad happens
    • decrypt

      public byte[] decrypt(byte[] data) throws CryptoException
      Decrypt a given amount of data.
      Parameters:
      data - the data to decrypt
      Returns:
      the clear-text data
      Throws:
      CryptoException - if something bad happens
    • trimKeySize

      protected static byte[] trimKeySize(byte[] rawKey, int maxSize)
      Helper method to trim a key if it is longer than a particular size.
      Parameters:
      rawKey - the raw key data
      maxSize - the maximum size in bytes
      Returns:
      the trimmed key