Class Crypto
java.lang.Object
edu.internet2.middleware.morphString.Crypto
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
-
Method Summary
Modifier and TypeMethodDescriptionCreate the default cipherdecrypt
(InputStream in) the decrypted input streamdecrypt
(OutputStream out) the decrypted output streamDecrypt the stringencrypt
(InputStream in) Get the encrypted input streamencrypt
(OutputStream out) the encrypted output streamEncrypt the stringstatic byte[]
generateKeyBytes
(String cipherName, Integer keybits) Generate a key.static Crypto
protected void
initialize the key and cipher
-
Constructor Details
-
Crypto
public Crypto()Default crypto object -
Crypto
Default crypto object- Parameters:
theKey
- used to encrypt/decrypt
-
-
Method Details
-
getThreadLocalCrypto
- Returns:
- a non-null thread-safe crypto object from a ThreadLocal
-
generateKeyBytes
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
Create the default cipher- Returns:
- the default cipher
-
init
initialize the key and cipher- Parameters:
secret
-
-
encrypt
Encrypt the string- Parameters:
clearText
-- Returns:
- the encrypted String
-
decrypt
Decrypt the string- Parameters:
cipherText
-- Returns:
- the decrypted string
-
encrypt
Get the encrypted input stream- Parameters:
in
-- Returns:
- the encrypted input stream
-
decrypt
the decrypted input stream- Parameters:
in
-- Returns:
- the decrypted input stream
-
encrypt
the encrypted output stream- Parameters:
out
-- Returns:
- the encrypted output stream
-
decrypt
the decrypted output stream- Parameters:
out
-- Returns:
- the decrypted output stream
-