uk.org.ogsadai.common.rolemap
Class Cryptography

java.lang.Object
  |
  +--uk.org.ogsadai.common.rolemap.Cryptography

public class Cryptography
extends java.lang.Object

A collection of methods related to encrypting and decrypting strings.

The class uses a password value assumed to be within an ogsadai.password system property.

Author:
The OGSA-DAI Project Team

Field Summary
private static java.lang.String ALGORITHM
           
private static java.lang.String COPYRIGHT_NOTICE
           
private static int COUNT
           
private static java.lang.String PASSWORD
           
private static byte[] SALT
           
 
Constructor Summary
Cryptography()
           
 
Method Summary
private static byte[] decode(byte[] string)
          Base64 decode an array of bytes.
private static byte[] decrypt(java.lang.String password, byte[] cypherText)
          Decrypt an array of bytes, using the given password, and return the decrypted bytes.
private static byte[] decrypt(java.lang.String password, java.lang.String cypherText)
          Decrypt a string, using the given password, and return the decrypted string as an array of bytes.
private static byte[] encode(byte[] string)
          Base64 encode an array of bytes.
private static byte[] encrypt(java.lang.String password, byte[] plainText)
          Encrypt an array of bytes, using the given password, and return the encrypted bytes.
private static byte[] encrypt(java.lang.String password, java.lang.String plainText)
          Encrypt a string, using the given password, and return the encrypted string as an array of bytes.
private static javax.crypto.Cipher getCypher(java.lang.String password, int encryptMode)
          Returns a fully-initialised Cipher object which can be used for encryption or decryption.
static java.lang.String getPassword()
          Returns the value of the ogsadai.password system property.
static java.lang.String scramble(java.lang.String password, byte[] plainText)
          Encrypts an array of bytes, using a given password, and returns the encrypted bytes as a Base64 encoded string.
static java.lang.String scramble(java.lang.String password, java.lang.String plainText)
          Encrypts a string, using a given password, and returns the encrypted text as a Base64 encoded string.
static java.lang.String unscramble(java.lang.String scrambled)
          Unencrypts a Base64 encoded string, using the value of the ogsadai.password system property as a password, and returns the unencrypted string.
static java.lang.String unscramble(java.lang.String password, byte[] scrambled)
          Unencrypts an array of Base64 encoded bytes, using a given password, and returns the unencrypted bytes as a string.
static java.lang.String unscramble(java.lang.String password, java.lang.String scrambled)
          Unencrypts a Base64 encoded string, using a given password, and returns the unencrypted string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COPYRIGHT_NOTICE

private static final java.lang.String COPYRIGHT_NOTICE
See Also:
Constant Field Values

ALGORITHM

private static final java.lang.String ALGORITHM
See Also:
Constant Field Values

SALT

private static final byte[] SALT

COUNT

private static final int COUNT
See Also:
Constant Field Values

PASSWORD

private static final java.lang.String PASSWORD
See Also:
Constant Field Values
Constructor Detail

Cryptography

public Cryptography()
Method Detail

getPassword

public static java.lang.String getPassword()
                                    throws MissingSystemPropertyException
Returns the value of the ogsadai.password system property.

Returns:
the value of the password property.
Throws:
MissingSystemPropertyException - if the property is not set.

scramble

public static java.lang.String scramble(java.lang.String password,
                                        byte[] plainText)
                                 throws CryptographyException
Encrypts an array of bytes, using a given password, and returns the encrypted bytes as a Base64 encoded string.

Parameters:
password - The password to use as a key.
plainText - The bytes to encrypt.
Returns:
the encrypted and Base64 encoded string.
Throws:
CryptographyException - if a problem occurred.

scramble

public static java.lang.String scramble(java.lang.String password,
                                        java.lang.String plainText)
                                 throws CryptographyException
Encrypts a string, using a given password, and returns the encrypted text as a Base64 encoded string.

Parameters:
password - The password to use as a key.
plainText - The string to encrypt.
Returns:
the encrypted and Base64 encoded string.
Throws:
CryptographyException - if a problem occurred.

unscramble

public static java.lang.String unscramble(java.lang.String password,
                                          byte[] scrambled)
                                   throws CryptographyException
Unencrypts an array of Base64 encoded bytes, using a given password, and returns the unencrypted bytes as a string.

Parameters:
password - The password to use as a key.
scrambled - The Base64 encoded and encrypted bytes to decrypt.
Returns:
the decrypted string.
Throws:
CryptographyException - if a problem occurred.

unscramble

public static java.lang.String unscramble(java.lang.String password,
                                          java.lang.String scrambled)
                                   throws CryptographyException
Unencrypts a Base64 encoded string, using a given password, and returns the unencrypted string.

Parameters:
password - The password to use as a key.
scrambled - The Base64 encoded and encrypted string to decrypt.
Returns:
the decrypted string.
Throws:
CryptographyException - if a problem occurred.

unscramble

public static java.lang.String unscramble(java.lang.String scrambled)
                                   throws CryptographyException,
                                          MissingSystemPropertyException
Unencrypts a Base64 encoded string, using the value of the ogsadai.password system property as a password, and returns the unencrypted string.

Parameters:
scrambled - The Base64 encoded and encrypted string to decrypt.
Returns:
the decrypted string.
Throws:
CryptographyException - if a problem occurred.
MissingSystemPropertyException - if the property is not set.

encode

private static byte[] encode(byte[] string)
Base64 encode an array of bytes.

Parameters:
string - The bytes to encode.
Returns:
the encoded bytes.
See Also:
Base64.encode(byte[])

decode

private static byte[] decode(byte[] string)
Base64 decode an array of bytes.

Parameters:
string - The bytes to decode.
Returns:
the decoded bytes
See Also:
Base64.decode(byte[])

decrypt

private static byte[] decrypt(java.lang.String password,
                              byte[] cypherText)
                       throws CryptographyException
Decrypt an array of bytes, using the given password, and return the decrypted bytes.

Parameters:
password - The password to use as a key.
cypherText - The encrypted bytes to decrypt
Returns:
the decrypted bytes.
Throws:
CryptographyException - if a problem occurred.

decrypt

private static byte[] decrypt(java.lang.String password,
                              java.lang.String cypherText)
                       throws CryptographyException
Decrypt a string, using the given password, and return the decrypted string as an array of bytes.

Parameters:
password - The password to use as a key.
cypherText - The encrypted string to decrypt
Returns:
the decrypted bytes.
Throws:
CryptographyException - if a problem occurred.

encrypt

private static byte[] encrypt(java.lang.String password,
                              byte[] plainText)
                       throws CryptographyException
Encrypt an array of bytes, using the given password, and return the encrypted bytes.

Parameters:
password - The password to use as a key.
plainText - The plain text bytes to encrypt
Returns:
the encrypted bytes.
Throws:
CryptographyException - if a problem occurred.

encrypt

private static byte[] encrypt(java.lang.String password,
                              java.lang.String plainText)
                       throws CryptographyException
Encrypt a string, using the given password, and return the encrypted string as an array of bytes.

Parameters:
password - The password to use as a key.
plainText - The plain text string to encrypt
Returns:
the encrypted bytes.
Throws:
CryptographyException - if a problem occurred.

getCypher

private static javax.crypto.Cipher getCypher(java.lang.String password,
                                             int encryptMode)
                                      throws CryptographyException
Returns a fully-initialised Cipher object which can be used for encryption or decryption.

Parameters:
password - The password to use as a key.
encryptMode - Either Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE.
Returns:
the initialised Cipher object.
Throws:
CryptographyException - if a problem occurred.