org.jppf.security
Class CryptoUtils

java.lang.Object
  extended by org.jppf.security.CryptoUtils

public class CryptoUtils
extends Object

Utility methods to handle encryption and decryption, digital signing, and key exchange protocols.

Author:
Laurent Cohen

Constructor Summary
CryptoUtils()
           
 
Method Summary
static byte[] decrypt(byte[] data)
          Encrypt some data.
static byte[] decrypt(SecretKey key, byte[] data)
          Encrypt some data.
static byte[] encrypt(byte[] data)
          Encrypt some data.
static byte[] encrypt(SecretKey key, byte[] data)
          Encrypt some data using athe specified key.
static KeyPair generateDHKeyPair()
          Generate a Diffie Hellman private/public pair of keys.
static KeyPair generateKeyPair()
          Generate a private/public pair of keys.
static SecretKey generateSecretKey()
          Generate a secret key.
static byte[] generateSignature(byte[] data)
          Digitally sign some data.
static SecretKey getSecretKey()
          Get the secret key used for encryption and decryption.
static SecretKey getSecretKeyFromEncoded(byte[] encoded)
          Get a secret key from its encoded representation.
static void main(String... args)
          Entry pont to test this class.
static byte[] readKeyFile(String filename)
          Read a public or private key from a file.
static boolean verifySignature(byte[] signature, byte[] data)
          Check a digital signature for some data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CryptoUtils

public CryptoUtils()
Method Detail

main

public static void main(String... args)
Entry pont to test this class.

Parameters:
args - no used.

generateKeyPair

public static KeyPair generateKeyPair()
                               throws Exception
Generate a private/public pair of keys.

Returns:
a pair of private and corresponding public key, as a KeyPair instance.
Throws:
Exception - if an exception is raised while generating the pair of keys.

generateDHKeyPair

public static KeyPair generateDHKeyPair()
                                 throws Exception
Generate a Diffie Hellman private/public pair of keys.

Returns:
a pair of private and corresponding public key, as a KeyPair instance.
Throws:
Exception - if an exception is raised while generating the pair of keys.

generateSecretKey

public static SecretKey generateSecretKey()
                                   throws Exception
Generate a secret key.

Returns:
a SecretKey instance.
Throws:
Exception - if th ekey could not be generated.

readKeyFile

public static byte[] readKeyFile(String filename)
                          throws IOException
Read a public or private key from a file.

Parameters:
filename - the name of the file to read the key from.
Returns:
the encoded key value, as an array of bytes.
Throws:
IOException - if an exception is raised while readsing the file.

generateSignature

public static byte[] generateSignature(byte[] data)
                                throws Exception
Digitally sign some data.

Parameters:
data - the data to sign.
Returns:
the digital data signature as an array of bytes.
Throws:
Exception - if an exception is raised while generating the signature.

verifySignature

public static boolean verifySignature(byte[] signature,
                                      byte[] data)
                               throws Exception
Check a digital signature for some data.

Parameters:
signature - data signature.
data - the signed data.
Returns:
the digital data signature as an array of bytes.
Throws:
Exception - if an exception is raised while generating the signature.

getSecretKey

public static SecretKey getSecretKey()
                              throws Exception
Get the secret key used for encryption and decryption.

Returns:
a SecretKey instance.
Throws:
Exception - if the secret key could not be obtained.

getSecretKeyFromEncoded

public static SecretKey getSecretKeyFromEncoded(byte[] encoded)
                                         throws Exception
Get a secret key from its encoded representation.

Parameters:
encoded - the secret key in encoded format.
Returns:
a SecretKey instance.
Throws:
Exception - if the secret key could not be obtained.

encrypt

public static byte[] encrypt(byte[] data)
                      throws Exception
Encrypt some data.

Parameters:
data - the data as an array of bytes.
Returns:
the encrypted data as an array of bytes.
Throws:
Exception - if the data encryption failed.

encrypt

public static byte[] encrypt(SecretKey key,
                             byte[] data)
                      throws Exception
Encrypt some data using athe specified key.

Parameters:
key - the key to use for encryption.
data - the data as an array of bytes.
Returns:
the encrypted data as an array of bytes.
Throws:
Exception - if the data encryption failed.

decrypt

public static byte[] decrypt(byte[] data)
                      throws Exception
Encrypt some data.

Parameters:
data - the data as an array of bytes.
Returns:
the encrypted data as an array of bytes.
Throws:
Exception - if the data encryption failed.

decrypt

public static byte[] decrypt(SecretKey key,
                             byte[] data)
                      throws Exception
Encrypt some data.

Parameters:
key - the key to use for decryption.
data - the data as an array of bytes.
Returns:
the encrypted data as an array of bytes.
Throws:
Exception - if the data encryption failed.


Copyright © 2005-2010 JPPF Team.