edu.internet2.middleware.grouperClientExt.edu.internet2.middleware.morphString
Class Crypto

java.lang.Object
  extended by edu.internet2.middleware.grouperClientExt.edu.internet2.middleware.morphString.Crypto

public class Crypto
extends Object

The purpose of this class is to provide encryption and decryption using standard Java libraries, for potentially large amounts of data.

This class provides default encryption using AES with a constant 128 bit key. If you want something more secure feel free to override the defaults however you please.

This class works in one of two ways, (1) in memory using Strings, or (2) via I/O streams (preferred for large amounts of data).

Crypo objects, or more specifically the default ciphers they create, are not threadsafe and are not computationally cheap, so a threadlocal factory method is provided for convenience. This is the preferred means of usage, but feel free to create these objects however you please.

Note that you can encrypt BLOB fields by specifying encryption in the configurator (Crypto is the default encryption mechanism for that).


Constructor Summary
Crypto()
          Default crypto object
Crypto(String theKey)
          Default crypto object
 
Method Summary
 Cipher createDefaultCipher()
          Create the default cipher
 InputStream decrypt(InputStream in)
          the decrypted input stream
 OutputStream decrypt(OutputStream out)
          the decrypted output stream
 String decrypt(String cipherText)
          Decrypt the string
 InputStream encrypt(InputStream in)
          Get the encrypted input stream
 OutputStream encrypt(OutputStream out)
          the encrypted output stream
 String encrypt(String clearText)
          Encrypt the string
static byte[] generateKeyBytes(String cipherName, Integer keybits)
          Generate a key.
static Crypto getThreadLocalCrypto()
           
protected  void init(String secret)
          initialize the key and cipher
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Crypto

public Crypto()
Default crypto object


Crypto

public Crypto(String theKey)
Default crypto object

Parameters:
theKey - used to encrypt/decrypt
Method Detail

getThreadLocalCrypto

public static Crypto getThreadLocalCrypto()
Returns:
a non-null thread-safe crypto object from a ThreadLocal

generateKeyBytes

public static byte[] generateKeyBytes(String cipherName,
                                      Integer keybits)
Generate a key.

Parameters:
cipherName - the name of the cipher, if null will default to "AES"
keybits - the number of bits in the key, if null will default to 128
Returns:
the bytes comprising the key

createDefaultCipher

public Cipher createDefaultCipher()
Create the default cipher

Returns:
the default cipher

init

protected void init(String secret)
initialize the key and cipher

Parameters:
secret -

encrypt

public String encrypt(String clearText)
Encrypt the string

Parameters:
clearText -
Returns:
the encrypted String

decrypt

public String decrypt(String cipherText)
Decrypt the string

Parameters:
cipherText -
Returns:
the decrypted string

encrypt

public InputStream encrypt(InputStream in)
Get the encrypted input stream

Parameters:
in -
Returns:
the encrypted input stream

decrypt

public InputStream decrypt(InputStream in)
the decrypted input stream

Parameters:
in -
Returns:
the decrypted input stream

encrypt

public OutputStream encrypt(OutputStream out)
the encrypted output stream

Parameters:
out -
Returns:
the encrypted output stream

decrypt

public OutputStream decrypt(OutputStream out)
the decrypted output stream

Parameters:
out -
Returns:
the decrypted output stream


Copyright © 2012 Internet2. All Rights Reserved.