org.n52.security.common.crypto
Class EncryptionUtil

java.lang.Object
  extended by org.n52.security.common.crypto.EncryptionUtil

public final class EncryptionUtil
extends Object

Encapsulates some convinient encryption/decryption tasks.

The methods are not intended to be used with big contents! They are not optimized for performance!

Author:
Marko Reiprecht

Field Summary
static String DEFAULT_ASYMMETRIC_ALGORITHM
           
static int DEFAULT_ASYMMETRIC_KEYSIZE
           
static String DEFAULT_SYMMETRIC_ALGORITHM
           
static int DEFAULT_SYMMETRIC_KEYSIZE
           
static int KEY_TYPE_PRIVATE
           
static int KEY_TYPE_PUBLIC
           
static int KEY_TYPE_SECRET
           
 
Method Summary
static KeyPair createAsymmetricKeys()
          Creates a KeyPair with default settings (RSA 512).
static KeyPair createAsymmetricKeys(int size)
          Creates KeyPair with default algorithm (RSA) but given size.
static KeyPair createAsymmetricKeys(int size, String algorithm)
          Creates new key pair with given algorithm and size.
static Key createSymmetricKey()
          creates symmetric key with default algorithm (AES 128).
static Key createSymmetricKey(int size)
          creates symmetric key with default algorithm (AES) but different key size.
static Key createSymmetricKey(int size, String algorithm)
          creates symmetric key with given algorithm and key size.
static String decrypt(String encryptedBytesAsBase64, Key key, String transformation)
          Decryptes the given result of a previous encrypt (base64 byte string) with the given key and using the given algorithm.
static String decryptAsymmetric(String encryptedBytesAsBase64, Key asymmetricKey)
          Default method for asymmetric decryption using the RSA algorithm.
static String decryptFromHex(String encryptedBytesAsHex, Key key, String transformation)
          Decrypts the given result of a previous encrypt (hexadecimal byte string) with the given key and using the given algorithm.
static String decryptSymmetric(String encryptedBytesAsBase64, Key symmetricKey)
          Default method for symmetric decryption using the AES algorithm.
static String encrypt(String content, Key key, String transformation)
          Encrypts the contents of the input string, with the given key and the given algorithm.
static String encryptAsHex(String content, Key key, String transformation)
          Encrypts the contents of the input string, with the given key and the given algorithm.
static String encryptAsymmetric(String content, Key asymmetricKey)
          Default method for symmetric encryption using the RSA algorithm.
static String encryptSymmetric(String content, Key symmetricKey)
          Default method for symmetric encryption using the AES algorithm.
static int getKeyType(Key key)
          Resolves the key type from the key.
static Key unwrapAsymmetric(String encryptedKeyBytesAsBase64, String encryptedKeyAlgorithm, int encryptedKeyType, Key asymmetricKey)
          Default method for asymmetric unwrapping using the AES algorithm.
static Key unwrapKey(String encryptedKeyBytesAsBase64, String encryptedKeyAlgorithm, int encryptedKeyType, Key key, String transformation)
          Unwraps a secret key, next to the base64 bytes of the wrapped key you need to know the algorithm used in the key and the type of the key (e.g.
static Key unwrapSymmetric(String encryptedKeyBytesAsBase64, String encryptedKeyAlgorithm, int encryptedKeyType, Key symmetricKey)
          Default method for symmetric unwrapping using the AES algorithm.
static String wrapAsymmetric(Key toWrap, Key asymmetricKey)
          Default method for asymmetric key wrapping using the AES algorithm.
static String wrapKey(Key toWrap, Key key, String transformation)
          Wraps(encrypts) a secret key to securly transport it over a network.
static String wrapSymmetric(Key toWrap, Key symmetricKey)
          Default method for symmetric key wrapping using the AES algorithm.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SYMMETRIC_ALGORITHM

public static final String DEFAULT_SYMMETRIC_ALGORITHM
See Also:
Constant Field Values

DEFAULT_SYMMETRIC_KEYSIZE

public static final int DEFAULT_SYMMETRIC_KEYSIZE
See Also:
Constant Field Values

DEFAULT_ASYMMETRIC_ALGORITHM

public static final String DEFAULT_ASYMMETRIC_ALGORITHM
See Also:
Constant Field Values

DEFAULT_ASYMMETRIC_KEYSIZE

public static final int DEFAULT_ASYMMETRIC_KEYSIZE
See Also:
Constant Field Values

KEY_TYPE_PRIVATE

public static final int KEY_TYPE_PRIVATE
See Also:
Constant Field Values

KEY_TYPE_PUBLIC

public static final int KEY_TYPE_PUBLIC
See Also:
Constant Field Values

KEY_TYPE_SECRET

public static final int KEY_TYPE_SECRET
See Also:
Constant Field Values
Method Detail

getKeyType

public static int getKeyType(Key key)
Resolves the key type from the key.

Parameters:
key - a key.
Returns:
the type of the key.

encrypt

public static String encrypt(String content,
                             Key key,
                             String transformation)
Encrypts the contents of the input string, with the given key and the given algorithm.

The resulting bytes of the encryption are codes with BASE64 and returned as string.

Parameters:
content - input.
key - the key to use.
transformation - the transformation to use.
Returns:
the encrypted bytes as base64 string.

decrypt

public static String decrypt(String encryptedBytesAsBase64,
                             Key key,
                             String transformation)
Decryptes the given result of a previous encrypt (base64 byte string) with the given key and using the given algorithm.

Parameters:
encryptedBytesAsBase64 - the encryption bytes.
key - the key to decrypt.
transformation - the algorithm to use.
Returns:
the decrypted string.

wrapKey

public static String wrapKey(Key toWrap,
                             Key key,
                             String transformation)
Wraps(encrypts) a secret key to securly transport it over a network.

Parameters:
toWrap - the key to wrap.
key - the key used in the wrapping encryption.
transformation - the algorithm.
Returns:
the bytes of the wrapped key as base64 string.

unwrapKey

public static Key unwrapKey(String encryptedKeyBytesAsBase64,
                            String encryptedKeyAlgorithm,
                            int encryptedKeyType,
                            Key key,
                            String transformation)
Unwraps a secret key, next to the base64 bytes of the wrapped key you need to know the algorithm used in the key and the type of the key (e.g. {@link #KEY_TYPE_SECRET).

Parameters:
encryptedKeyBytesAsBase64 - the string produced by the wrap method.
encryptedKeyAlgorithm - the algorithm of the encrypted key.
encryptedKeyType - the type of the encrypted Key (e.g. {@link #KEY_TYPE_SECRET)).
key - the key used to decrypt.
transformation - the algorithm used to decrypt the key.
Returns:
a decrypted key.

encryptSymmetric

public static String encryptSymmetric(String content,
                                      Key symmetricKey)
Default method for symmetric encryption using the AES algorithm.

Parameters:
content - the content to encrypt.
symmetricKey - the symmetric key to use.
Returns:
base 64 encoded encrypted bytes.

decryptSymmetric

public static String decryptSymmetric(String encryptedBytesAsBase64,
                                      Key symmetricKey)
Default method for symmetric decryption using the AES algorithm.

Parameters:
encryptedBytesAsBase64 - base64 encrypted bytes.
symmetricKey - the symmetric key.
Returns:
the plain content.

wrapSymmetric

public static String wrapSymmetric(Key toWrap,
                                   Key symmetricKey)
Default method for symmetric key wrapping using the AES algorithm.

Parameters:
toWrap - the Key to encrypt.
symmetricKey - the symmetric key to use.
Returns:
base 64 encoded encrypted bytes.

unwrapSymmetric

public static Key unwrapSymmetric(String encryptedKeyBytesAsBase64,
                                  String encryptedKeyAlgorithm,
                                  int encryptedKeyType,
                                  Key symmetricKey)
Default method for symmetric unwrapping using the AES algorithm.

Parameters:
encryptedKeyBytesAsBase64 - the string produced by the wrap method.
encryptedKeyAlgorithm - the algorithm of the encrypted key.
encryptedKeyType - the type of the encrypted Key (e.g. {@link #KEY_TYPE_SECRET)).
symmetricKey - the symmetric key.
Returns:
the unwraped key.

encryptAsymmetric

public static String encryptAsymmetric(String content,
                                       Key asymmetricKey)
Default method for symmetric encryption using the RSA algorithm.

Parameters:
content - the content to encrypt.
asymmetricKey - the key to use.
Returns:
base 64 encoded encrypted bytes.

decryptAsymmetric

public static String decryptAsymmetric(String encryptedBytesAsBase64,
                                       Key asymmetricKey)
Default method for asymmetric decryption using the RSA algorithm.

Parameters:
encryptedBytesAsBase64 - base64 encrypted bytes.
asymmetricKey - the asymmetric key.
Returns:
the plain content.

wrapAsymmetric

public static String wrapAsymmetric(Key toWrap,
                                    Key asymmetricKey)
Default method for asymmetric key wrapping using the AES algorithm.

Parameters:
toWrap - the Key to encrypt.
asymmetricKey - the asymmetric key to use.
Returns:
base 64 encoded encrypted bytes.

unwrapAsymmetric

public static Key unwrapAsymmetric(String encryptedKeyBytesAsBase64,
                                   String encryptedKeyAlgorithm,
                                   int encryptedKeyType,
                                   Key asymmetricKey)
Default method for asymmetric unwrapping using the AES algorithm.

Parameters:
encryptedKeyBytesAsBase64 - the string produced by the wrap method.
encryptedKeyAlgorithm - the algorithm of the encrypted key.
encryptedKeyType - the type of the encrypted Key (e.g. {@link #KEY_TYPE_SECRET)).
asymmetricKey - the asymmetric key.
Returns:
the unwraped key.

createAsymmetricKeys

public static KeyPair createAsymmetricKeys()
Creates a KeyPair with default settings (RSA 512).

Returns:
asymmetric keypair.

createAsymmetricKeys

public static KeyPair createAsymmetricKeys(int size)
Creates KeyPair with default algorithm (RSA) but given size.

Parameters:
size - key size.
Returns:
a new KeyPair.

createAsymmetricKeys

public static KeyPair createAsymmetricKeys(int size,
                                           String algorithm)
Creates new key pair with given algorithm and size.

Parameters:
size - key size.
algorithm - algorithm.
Returns:
new keypair.

createSymmetricKey

public static Key createSymmetricKey()
creates symmetric key with default algorithm (AES 128).

Returns:
new key.

createSymmetricKey

public static Key createSymmetricKey(int size)
creates symmetric key with default algorithm (AES) but different key size.

Returns:
new key.

createSymmetricKey

public static Key createSymmetricKey(int size,
                                     String algorithm)
creates symmetric key with given algorithm and key size.

Returns:
new key.

encryptAsHex

public static String encryptAsHex(String content,
                                  Key key,
                                  String transformation)
Encrypts the contents of the input string, with the given key and the given algorithm.

The resulting bytes of the encryption are codes with hexadecimal and returned as string.

Parameters:
content - input.
key - the key to use.
transformation - the transformation to use.
Returns:
the encrypted bytes as hexadecimal string.

decryptFromHex

public static String decryptFromHex(String encryptedBytesAsHex,
                                    Key key,
                                    String transformation)
Decrypts the given result of a previous encrypt (hexadecimal byte string) with the given key and using the given algorithm.

Parameters:
encryptedBytesAsHex - the encryption bytes.
key - the key to decrypt.
transformation - the algorithm to use.
Returns:
the decrypted string.


Copyright © 2004–2013 52north.org. All rights reserved.