com.dyuproject.util
Class Cryptography

java.lang.Object
  extended by com.dyuproject.util.Cryptography

public final class Cryptography
extends Object

Encryption and Decryption utility

Author:
David Yu
Date created:
Sep 12, 2008

Field Summary
static String DES
          The algorithm used for 8-character secret keys.
static String DESede
          The algorithm used for 24-character secret keys.
 
Method Summary
static Cryptography create(byte[] secretKey, String algorithm)
          Creates a new instance with the given bytes secretKey and algorithm.
static Cryptography create(String secretKey, char pad)
          Creates a new instance with the given secretKey and the character pad for padding if the key is not long enough.
static Cryptography create(String secretKey, String algorithm)
          Creates a new instance with the given secretKey and algorithm.
static Cryptography create(String secretKey, String charset, String algorithm)
          Creates a new instance with the given secretKey, charset and algorithm.
static Cryptography createDES(String secretKey)
          Creates a new instance with the given secretKey.
static Cryptography createDESede(String secretKey)
          Creates a new instance with the given secretKey.
 byte[] decrypt(byte[] input)
          Decrypts the given bytes input.
 String decrypt(String input)
          Decrypts the given string input with ISO-8859-1 encoding.
 String decrypt(String input, String charset)
          Decrypts the given string input with ISO-8859-1 encoding.
 String decryptDecode(String input)
          Decodes the given string input with base 64 using ISO-8859-1 and decrypts the decoded string.
 String decryptDecode(String input, String charset)
          Decodes the given string input with base 64 using the given charset and decrypts the decoded string.
 byte[] encrypt(byte[] input)
          Encrypts the given bytes input.
 String encrypt(String input)
          Encrypts the given string input with ISO-8859-1 encoding.
 String encrypt(String input, String charset)
          Encrypts the given string input encoded with the given charset.
 String encryptEncode(String input)
          Encrypts the given string input encoded with ISO-8859-1; The encrypted bytes will then be b64 encoded.
 String encryptEncode(String input, String charset)
          Encrypts the given string input encoded with the given charset; The encrypted bytes will then be b64 encoded.
static Cryptography generateDESedeRandom()
          Creates a new instance with a random secret that is 24 characters long.
static Cryptography generateDESRandom()
          Creates a new instance with a random secret that is 8 characters long.
static Cryptography generateRandom(String algorithm)
          Creates a new instance with a random secret with the given algorithm.
 Key getKey()
          Gets the secret key.
static String pad(String secretKey, char pad)
          Returns a string padded with the given character pad if the length is not equal to 8 or 24.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DES

public static final String DES
The algorithm used for 8-character secret keys.

See Also:
Constant Field Values

DESede

public static final String DESede
The algorithm used for 24-character secret keys.

See Also:
Constant Field Values
Method Detail

pad

public static String pad(String secretKey,
                         char pad)
Returns a string padded with the given character pad if the length is not equal to 8 or 24.


create

public static Cryptography create(String secretKey,
                                  char pad)
                           throws Exception
Creates a new instance with the given secretKey and the character pad for padding if the key is not long enough.

Throws:
IllegalArgumentException - if the secretKey is more than 24 characters long.
Exception

createDES

public static Cryptography createDES(String secretKey)
                              throws Exception
Creates a new instance with the given secretKey.

Throws:
IllegalArgumentException - if the secretKey is not 8 characters long.
Exception

createDESede

public static Cryptography createDESede(String secretKey)
                                 throws Exception
Creates a new instance with the given secretKey.

Throws:
IllegalArgumentException - if the secretKey is not 24 characters long.
Exception

create

public static Cryptography create(String secretKey,
                                  String algorithm)
                           throws Exception
Creates a new instance with the given secretKey and algorithm.

Throws:
Exception

create

public static Cryptography create(String secretKey,
                                  String charset,
                                  String algorithm)
                           throws Exception
Creates a new instance with the given secretKey, charset and algorithm.

Throws:
Exception

create

public static Cryptography create(byte[] secretKey,
                                  String algorithm)
                           throws Exception
Creates a new instance with the given bytes secretKey and algorithm.

Throws:
Exception

generateDESRandom

public static Cryptography generateDESRandom()
                                      throws Exception
Creates a new instance with a random secret that is 8 characters long.

Throws:
Exception

generateDESedeRandom

public static Cryptography generateDESedeRandom()
                                         throws Exception
Creates a new instance with a random secret that is 24 characters long.

Throws:
Exception

generateRandom

public static Cryptography generateRandom(String algorithm)
                                   throws Exception
Creates a new instance with a random secret with the given algorithm.

Throws:
Exception

encrypt

public byte[] encrypt(byte[] input)
               throws Exception
Encrypts the given bytes input.

Throws:
Exception

encrypt

public String encrypt(String input)
               throws Exception
Encrypts the given string input with ISO-8859-1 encoding.

Throws:
Exception

encrypt

public String encrypt(String input,
                      String charset)
               throws Exception
Encrypts the given string input encoded with the given charset.

Throws:
Exception

encryptEncode

public String encryptEncode(String input)
                     throws Exception
Encrypts the given string input encoded with ISO-8859-1; The encrypted bytes will then be b64 encoded.

Throws:
Exception

encryptEncode

public String encryptEncode(String input,
                            String charset)
                     throws Exception
Encrypts the given string input encoded with the given charset; The encrypted bytes will then be b64 encoded.

Throws:
Exception

decrypt

public byte[] decrypt(byte[] input)
               throws Exception
Decrypts the given bytes input.

Throws:
Exception

decrypt

public String decrypt(String input)
               throws Exception
Decrypts the given string input with ISO-8859-1 encoding.

Throws:
Exception

decrypt

public String decrypt(String input,
                      String charset)
               throws Exception
Decrypts the given string input with ISO-8859-1 encoding.

Throws:
Exception

decryptDecode

public String decryptDecode(String input)
                     throws Exception
Decodes the given string input with base 64 using ISO-8859-1 and decrypts the decoded string.

Throws:
Exception

decryptDecode

public String decryptDecode(String input,
                            String charset)
                     throws Exception
Decodes the given string input with base 64 using the given charset and decrypts the decoded string.

Throws:
Exception

getKey

public Key getKey()
Gets the secret key.



Copyright © 2008-2013. All Rights Reserved.