org.elkoserver.foundation.json
Class Cryptor

java.lang.Object
  extended by org.elkoserver.foundation.json.Cryptor

public class Cryptor
extends Object

Simple AES-based string encryptor/decryptor, for passing sealed bundles of data through an untrusted party.


Constructor Summary
Cryptor(String keyStr)
          Constructor.
 
Method Summary
 String decrypt(String str)
          Decrypt a (base-64 encoded) encrypted string
 JSONObject decryptJSONObject(String str)
          Decrypt a (base-64 encoded) encrypted JSON object literal.
 Object decryptObject(Class baseType, String str)
          Decrypt and decode a (base-64 encoded) encrypted object serialized as a JSON object literal.
 String encrypt(String str)
          Produce a (base-64 encoded) encrypted version of a string.
static String generateKey()
          Generate a new, random key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Cryptor

public Cryptor(String keyStr)
        throws IOException
Constructor.

Parameters:
keyStr - Base64-encoded symmetric key.
Throws:
IOException - if the key string is malformed.
Method Detail

decrypt

public String decrypt(String str)
               throws IOException
Decrypt a (base-64 encoded) encrypted string

Parameters:
str - Encrypted string to be decrypted, as generated by the encrypt method of this class.
Returns:
the decrypted plaintext encoded in 'str'
Throws:
IOException - if the input string is malformed.

decryptJSONObject

public JSONObject decryptJSONObject(String str)
                             throws IOException,
                                    SyntaxError
Decrypt a (base-64 encoded) encrypted JSON object literal.

Parameters:
str - Encrypted string to be decrypted.
Returns:
The decrypted and parsed JSON object encoded in 'str'
Throws:
IOException - if the input string is malformed
SyntaxError - if the decrypted JSON literal is invalid

decryptObject

public Object decryptObject(Class baseType,
                            String str)
                     throws IOException
Decrypt and decode a (base-64 encoded) encrypted object serialized as a JSON object literal.

Parameters:
baseType - The desired class of the resulting Java object. The result will not necessarily be of this class, but will be assignable to a variable of this class.
str - Encrypted string to be decrypted.
Returns:
a new Java object assignable to the class in 'baseType' as described by the JSON literal obtained by decrypting 'str', or null if the JSON literal was syntactically malformed or if the object could not be decoded for some reason.
Throws:
IOException - if the input string is malformed

encrypt

public String encrypt(String str)
Produce a (base-64 encoded) encrypted version of a string. The first 22 characters of the result string are the base64-encoded IV (minus a terminal "=="). The remainder of the string is the base64-encoded ciphertext.

Parameters:
str - String to be encrypted
Returns:
an encoded, encrypted version of the given string

generateKey

public static String generateKey()
Generate a new, random key.

Returns:
the generated key, as a string containing a base64-encoding of the key bits.