Copyright 2009,2010, Lapetus Systems Ltd. (All rights reserved)

com.lapetus_ltd.api.common
Class TLptsCryptoUtil

java.lang.Object
  extended by com.lapetus_ltd.api.common.TLptsCryptoUtil

public class TLptsCryptoUtil
extends java.lang.Object

Class Description : Cryptographic module for encrypting and decrypting transportable data.


This crypto module provides a complete solution for encrypting/decrypting data, both symmetrically and asymmetrically.
The two types of encryption technologies utilised, and available in this module, are RSA and AES.

The RSA encryption is used to encrypt and decrypt the application data, with a default instantiation of a 1024 bit key pair.
The AES symmetric encryption is instantiated with a 128 bit key.

The application can decide to request larger keys for both AES and RSA, which are then available via instantiations of this class.

Every usage of the initCipherRSA function causes a different set of RSA keys to be generated, while the AES key is generated through initCipherAES.
The default instantiation, through the static functions, is used specifically for the credentials of the connections.
When the system first starts RSA and AES keys are generated and stored in the user's home directory, under lapetus, for later use.
The keys are stored in the files lpts.rsa.public.key.xml, lpts.rsa.private.key.xml and lpts.aes.128.key.xml.

These keys should be backed up as their corruption or loss will bring about the inability to decrypt any encrypted data.
Data like the connection credentials or any other application encryption performed with this class, will be illegible without the key files.

The public key from this class can be provided to anyone wishing to encrypt a message that can only be read by this private key.
It is therefore a good basis for starting a communication session, which can then become secure with the exchange of symmetric keys (AES).
How it works is that the public key is sent to the other party on the communication link. The other party then could
generate a symmetric key which is then encrypted with the public key.
Both parties are ensured that the encrypted symmetric key can only be read by the owner of the private key.
The symmetric key (which is much less CPU intensive than processing asymmetric keys) is then used in all communication between the parties.

Another use case is where both parties generate key pairs and exchange them (using the public key of the other) before
creating symmetric key(s). It all depends on how much security is required and how many layers. The possibilities are endless.

The static functions of this class are an easy way to utilise the default encryption for this application.
This default implements RSA 1024 bit and AES 128 bit ciphers.

If your application requires its own version (and key size) of the RSA and AES systems, then instantiate a TLptsCryptoUtil class
object and utilise the required initCipherXXX function(s).

WARNING : do not use the static functions together with your own instantiation of the crypto module class. Although it will work,
the results will not be as expected. In other words, do not do the following:

 
TLptsCryptoUtil myCrypto = new TLptsCryptoUtil();
myCrypto.initCipherRSA(1024);
myCrypto.defaultEncryptRSA(myData);

A quick word about AES:
Although it would seem that the AES keys are small in comparison to the RSA keys, and furthermore most systems do not provide more that 128 bit encryption,
the AES cryptographic mechanism includes advanced mathematics and bit operations, which ensures that it is very secure.
The fact that it is symmetrical generally means that a smaller key bit size is required for secure encryption (as apposed to asymmetrical that is).

Cipher Instantiation types:
The types of ciphers initiated as default are 'RSA' and 'AES/ECB/PKCS5Padding'. These can be changed by using the relative init functions.

$LastChangedRevision: 1177 $
$LastChangedDate:: 2010-11-09 15:27:48#$


Nested Class Summary
static class TLptsCryptoUtil.Credentials
          This is TLptsCryptoUtil.Credentials, which is a simple class used to read and store encrypted credentials.
 
Field Summary
static java.lang.String DEFAULT_AES_INSTANCE_STRING
          The default instance string for the internal 128 bit AES Cipher (used by Cipher.getInstance).
static int DEFAULT_AES_KEY_BIT_SIZE
          The default key size for the internal AES key pair.
static java.lang.String DEFAULT_AES_KEY_STRING
          The default key string for the internal 128 bit AES Cipher (used by KeyFactory.getInstance).
static java.lang.String DEFAULT_RSA_INSTANCE_STRING
          The default instance string for the internal 1024 bit RSA Cipher (used by Cipher.getInstance).
static int DEFAULT_RSA_KEY_BIT_SIZE
          The default key size for the internal RSA key pair.
static java.lang.String DEFAULT_RSA_KEY_STRING
          The default key string for the internal 1024 bit RSA Cipher (used by KeyFactory.getInstance).
 
Constructor Summary
TLptsCryptoUtil()
          This is the constructor of the Crypto module and is only used for instances of the class.
 
Method Summary
 byte[] decryptAES(byte[] encrypted)
           Decrypts the encrypted data with the instantiation's AES cipher.
 TLptsCryptoUtil.Credentials decryptCredentialsAES(byte[] encryptedData)
           Decrypts credentials (user name and password) to the internal Credential format using the default AES cipher.
 TLptsCryptoUtil.Credentials decryptCredentialsRSA(byte[] encryptedData)
           Decrypts credentials (user name and password) to the Credential class format using the default RSA cipher.
 byte[] decryptRSA(byte[] encrypted)
           Decrypts the encrypted data with the instantiation's RSA cipher.
static byte[] defaultDecryptAES(byte[] encrypted)
           Decrypts the encrypted data supplied with the internal default AES Cipher.
static TLptsCryptoUtil.Credentials defaultDecryptCredentialsAES(byte[] encryptedData)
           Decrypts credentials (user name and password) to the Credential class format using the default AES cipher.
static TLptsCryptoUtil.Credentials defaultDecryptCredentialsRSA(byte[] encryptedData)
           Decrypts credentials (user name and password) to the Credential class format using the default RSA cipher.
static byte[] defaultDecryptRSA(byte[] encrypted)
           Decrypts the encrypted data supplied with the internal default RSA Cipher.
static byte[] defaultEncryptAES(byte[] data)
           Encrypts the data supplied with the internal default AES Cipher.
static byte[] defaultEncryptCredentialsAES(java.lang.String user, java.lang.String password)
           Encrypts credentials (user name and password) to the internal Credential format using the default AES cipher.
static byte[] defaultEncryptCredentialsRSA(java.lang.String user, java.lang.String password)
           Encrypts credentials (user name and password) to the internal Credential format using the default RSA cipher.
static byte[] defaultEncryptRSA(byte[] data)
           Encrypts the data supplied with the internal default RSA Cipher.
static byte[] defaultForeignEncryptRSA(byte[] data)
           Encrypts data with a foreign public key, so that it can be transported securely.
static int defaultGetAESKeyBitSize()
           Gets the bit size of the AES key.
static java.math.BigInteger defaultGetPublicRSAExponent()
           Gets the public key exponent from the internal default RSA key pair.
static java.math.BigInteger defaultGetPublicRSAModulus()
           Gets the public key modulus from the internal default RSA key pair.
static int defaultGetRSAKeyBitSize()
           Gets the size of the RSA key in bits.
static boolean defaultInitForeignPublicCipherRSA(java.math.BigInteger modulus, java.math.BigInteger exponent, int keyBitLength)
           Initiates the default local module so that it can encrypt data with a foreign public key.
static boolean defaultInitForeignPublicCipherRSA(java.math.BigInteger modulus, java.math.BigInteger exponent, int keyBitLength, java.lang.String rsaInstanceString, java.lang.String rsaKeyString)
           Initiates the default local module so that it can encrypt data with a foreign public key.
static void defaultWriteAES2File(java.lang.String fileName)
           Writes the default AES key to a file.
static void defaultWriteRSA2Files(java.lang.String privateKeyFile, java.lang.String publicKeyFile)
           Writes the default RSA key pair to two files (private and public).
 byte[] encryptAES(byte[] data)
           Encrypts the data with the instantiation's AES cipher.
 byte[] encryptCredentialsAES(java.lang.String user, java.lang.String password)
           Encrypts credentials (user name and password) to the internal Credential format using the default AES cipher.
 byte[] encryptCredentialsRSA(java.lang.String user, java.lang.String password)
           Encrypts credentials (user name and password) to the internal Credential format using the default RSA cipher.
 byte[] encryptRSA(byte[] data)
           Encrypts the data with the instantiation's RSA cipher.
 byte[] foreignEncryptRSA(byte[] data)
          Encrypts data using the foreign RSA key.
 byte[] generateKey4AES(int bitSize)
          Generates a random key for the AES cipher.
 java.lang.String getAESInstanceString()
           Gets the instance string used to create this instance's AES cipher.
 int getAESKeyBitSize()
           Gets the public key modulus from the internal default RSA key pair.
 java.lang.String getAESKeyString()
           Gets the key string used to create this instance's AES cipher.
 java.math.BigInteger getPublicRSAExponent()
           Gets the public key modulus from the internal default RSA key pair.
 java.math.BigInteger getPublicRSAModulus()
           Gets the public key modulus from the internal default RSA key pair.
 java.lang.String getRSAInstanceString()
           Gets the RSA instance string used to create this instance's cipher (used by Cipher.getInstance).
 int getRSAKeyBitSize()
           Gets the public key modulus from the internal default RSA key pair.
 java.lang.String getRSAKeyString()
           Gets the RSA key string as sed by KeyFactory.getInstance to create the key for this cipher.
 boolean initCipherAES(byte[] key)
           Creates and initialises the AES cipher for this module.
 boolean initCipherAES(byte[] key, java.lang.String aesInstanceString, java.lang.String aesKeyString)
           Creates and initialises the AES cipher for this module.
 boolean initCipherAES(java.lang.String fileName)
           Creates and initialises the AES cipher for this module.
 boolean initCipherRSA(int keyBitSize)
           Initiates the RSA cryptographic capability, using the size and type supplied.
 boolean initCipherRSA(int keyBitSize, java.lang.String rsaInstanceString, java.lang.String rsaKeyString)
           Initiates the RSA cryptographic capability, using the size and type supplied.
 boolean initCipherRSA(java.lang.String privateKeyFile, java.lang.String publicKeyFile)
           Initiates the RSA cryptographic capability, using the key pair in the files supplied.
 boolean initForeignPublicCipherRSA(java.math.BigInteger modulus, java.math.BigInteger exponent, int keyBitLength)
           Initiates the foreign RSA cipher with the public key, so that encryption is possible.
 boolean initForeignPublicCipherRSA(java.math.BigInteger modulus, java.math.BigInteger exponent, int keyBitLength, java.lang.String rsaInstanceString, java.lang.String rsaKeyString)
           Initiates the foreign RSA cipher with the public key, so that encryption is possible.
 void writeAES2File(java.lang.String keyFile)
           Writes the AES key to a file for later use.
 void writeRSA2Files(java.lang.String privateKeyFile, java.lang.String publicKeyFile)
           Writes the RSA key pair to two files (private and public).
static void zI()
          Obfuscated, as it is not required by the application.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_AES_INSTANCE_STRING

public static final java.lang.String DEFAULT_AES_INSTANCE_STRING
The default instance string for the internal 128 bit AES Cipher (used by Cipher.getInstance).

See Also:
Constant Field Values

DEFAULT_AES_KEY_BIT_SIZE

public static final int DEFAULT_AES_KEY_BIT_SIZE
The default key size for the internal AES key pair.

See Also:
Constant Field Values

DEFAULT_AES_KEY_STRING

public static final java.lang.String DEFAULT_AES_KEY_STRING
The default key string for the internal 128 bit AES Cipher (used by KeyFactory.getInstance).

See Also:
Constant Field Values

DEFAULT_RSA_INSTANCE_STRING

public static final java.lang.String DEFAULT_RSA_INSTANCE_STRING
The default instance string for the internal 1024 bit RSA Cipher (used by Cipher.getInstance).

See Also:
Constant Field Values

DEFAULT_RSA_KEY_BIT_SIZE

public static final int DEFAULT_RSA_KEY_BIT_SIZE
The default key size for the internal RSA key pair.

See Also:
Constant Field Values

DEFAULT_RSA_KEY_STRING

public static final java.lang.String DEFAULT_RSA_KEY_STRING
The default key string for the internal 1024 bit RSA Cipher (used by KeyFactory.getInstance).

See Also:
Constant Field Values
Constructor Detail

TLptsCryptoUtil

public TLptsCryptoUtil()
This is the constructor of the Crypto module and is only used for instances of the class. It is not required if we decide to use the default ciphers through TLptsCryptoUtil.defaultXXX.

Method Detail

decryptAES

public byte[] decryptAES(byte[] encrypted)

Decrypts the encrypted data with the instantiation's AES cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes

Notes : There is no limit to the length of the data. (unlicenced limit of 128 bytes)
Example :

 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();

 if (!cryptoModule.initCipherAES(128))
 {
   System.out.println("Could not instantiate AES cipher!");
   return;
 }
 byte[] data = initMyData();
 byte[] encryptedData = cryptoModule.encryptAES(data);
 ... // store it or send it
 byte[] decryptedData = cryptoModule.decryptAES(encryptedData);
 ...
 

Parameters:
encrypted - The data to be decrypted.
Returns:
Returns the decrypted bytes or null if there is an error.

decryptCredentialsAES

public TLptsCryptoUtil.Credentials decryptCredentialsAES(byte[] encryptedData)

Decrypts credentials (user name and password) to the internal Credential format using the default AES cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes.

Notes : The encrypted data should come from a call to encryptCredentialsRSA.
This uses the Credentials class of the TLptsCryptoUtil.

Example :

 String userName = "myUser";
 String password = "veryDifficultPassword";

 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();

 if (!cryptoModule.initCipherAES(128))
 {
   System.out.println("Could not instantiate AES cipher!");
   return;
 }

 byte[] aesEncrypted = cryptoModule.encryptCredentialsAES(userName,password);

 TLptsCryptoUtil.Credentials aesCredentials = cryptoModule.decryptCredentialsAES(aesEncrypted);
 System.out.println("User Name and Password are : " + aesCredentials.userName + " & " + aesCredentials.password);
 

Parameters:
encryptedData - The encrypted data which holds the credential information.
Returns:
Decrypted data of the TLptsCryptoUtil.Credentials class.

decryptCredentialsRSA

public TLptsCryptoUtil.Credentials decryptCredentialsRSA(byte[] encryptedData)

Decrypts credentials (user name and password) to the Credential class format using the default RSA cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes.

Notes : The encrypted data should come from a call to encryptCredentialsRSA.
This uses the Credentials class of the TLptsCryptoUtil.

Example :

 String userName = "myUser";
 String password = "veryDifficultPassword";

 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();

 if (!cryptoModule.initCipherRSA(1024))
 {
   System.out.println("Could not instantiate RSA cipher!");
   return;
 }

 byte[] rsaEncrypted = cryptoModule.encryptCredentialsRSA(userName,password);

 TLptsCryptoUtil.Credentials rsaCredentials = cryptoModule.decryptCredentialsRSA(rsaEncrypted);
 System.out.println("User Name and Password are : " + rsaCredentials.userName + " & " + rsaCredentials.password);
 

Parameters:
encryptedData - The data previously encrypted by
Returns:
Returns an instance of TLptsCryptoUtil.Credentials or null if there is an error.

decryptRSA

public byte[] decryptRSA(byte[] encrypted)

Decrypts the encrypted data with the instantiation's RSA cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes

Notes : There is no limit to the length of the data. (unlicenced limit of 128 bytes)
Example :

 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();

 if (!cryptoModule.initCipherRSA(1024))
 {
   System.out.println("Could not instantiate RSA cipher!");
   return;
 }
 byte[] data = initMyData();
 byte[] encryptedData = cryptoModule.encryptRSA(data);
 ... // store it or send it
 byte[] decryptedData = cryptoModule.decryptRSA(encryptedData);
 ...
 

Parameters:
encrypted - The data to be decrypted.
Returns:
Returns the decrypted bytes or null if there is an error.

defaultDecryptAES

public static byte[] defaultDecryptAES(byte[] encrypted)

Decrypts the encrypted data supplied with the internal default AES Cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes.

Notes : There is no limit to the length of the data. (unlicenced limit of 128 bytes)

Example :

 byte[] myData = new byte[2048];
 .. initialise myData
 byte[] encryptedData = TLptsCryptoUtil.defaultEncryptAES(myData);
 byte[] decryptedData = TLptsCryptoUtil.defaultDecryptAES(encryptedData);
 Result: myData = decryptedData
 

Parameters:
encrypted - The encrypted data from the default AES Cipher.
Returns:
The decrypted data or null if there is an error. Check TLptsLogger for logger error.

defaultDecryptCredentialsAES

public static TLptsCryptoUtil.Credentials defaultDecryptCredentialsAES(byte[] encryptedData)

Decrypts credentials (user name and password) to the Credential class format using the default AES cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes.

Notes : The encrypted data should come from a call to defaultEncryptCredentialsAES.
This uses the Credentials class of the TLptsCryptoUtil.

Example :

 String userName = "myUser";
 String password = "veryDifficultPassword";
 byte[] aesEncrypted = TLptsCryptoUtil.defaultEncryptCredentialsAES(userName,password);
 TLptsCryptoUtil.Credentials aesDefaultCredentials = TLptsCryptoUtil.defaultDecryptCredentialsAES(aesEncrypted);
 System.out.println("User Name and Password are : " + aesDefaultCredentials.userName + " & " + aesDefaultCredentials.password);
 

Parameters:
encryptedData - The data previously encrypted by
Returns:
Returns an instance of TLptsCryptoUtil.Credentials or null if there is an error.

defaultDecryptCredentialsRSA

public static TLptsCryptoUtil.Credentials defaultDecryptCredentialsRSA(byte[] encryptedData)

Decrypts credentials (user name and password) to the Credential class format using the default RSA cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes.

Notes : The encrypted data should come from a call to defaultEncryptCredentialsRSA.
This uses the Credentials class of the TLptsCryptoUtil.

Example :

 String userName = "myUser";
 String password = "veryDifficultPassword";
 byte[] rsaEncrypted = TLptsCryptoUtil.defaultEncryptCredentialsRSA(userName,password);
 TLptsCryptoUtil.Credentials rsaDefaultCredentials = TLptsCryptoUtil.defaultDecryptCredentialsRSA(rsaEncrypted);
 System.out.println("User Name and Password are : " + rsaDefaultCredentials.userName + " & " + rsaDefaultCredentials.password);
 

Parameters:
encryptedData - The data previously encrypted by defaultEncryptCredentialsRSA
Returns:
Returns an instance of TLptsCryptoUtil.Credentials or null if there is an error.

defaultDecryptRSA

public static byte[] defaultDecryptRSA(byte[] encrypted)

Decrypts the encrypted data supplied with the internal default RSA Cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes.

Notes : There is no limit to the length of the data. (unlicenced limit of 128 bytes)

Example :

 byte[] myData = new byte[2048];
 .. initialise myData
 byte[] encryptedData = TLptsCryptoUtil.defaultEncryptRSA(myData);
 byte[] decryptedData = TLptsCryptoUtil.defaultDecryptRSA(encryptedData);
 Result: myData = decryptedData
 

Parameters:
encrypted - The encrypted data from the default RSA Cipher.
Returns:
The decrypted data or null if there is an error. Check TLptsLogger for logger error.

defaultEncryptAES

public static byte[] defaultEncryptAES(byte[] data)

Encrypts the data supplied with the internal default AES Cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes.

Notes : There is no limit to the length of the data. (unlicenced limit of 128 bytes)

Example :

 byte[] myData = new byte[2048];
 .. initialise myData
 byte[] encryptedData = TLptsCryptoUtil.defaultEncryptAES(myData);
 byte[] decryptedData = TLptsCryptoUtil.defaultDecryptAES(encryptedData);
 Result: myData = decryptedData
 

Parameters:
data - The data that should be encrypted.
Returns:
The encrypted data or null if there is an error. Check TLptsLogger for logger error.

defaultEncryptCredentialsAES

public static byte[] defaultEncryptCredentialsAES(java.lang.String user,
                                                  java.lang.String password)

Encrypts credentials (user name and password) to the internal Credential format using the default AES cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes.

Notes : The encrypted data can only be read by defaultDecryptCredentialsAES.

Example :

 String userName = "myUser";
 String password = "veryDifficultPassword";
 byte[] aesEncrypted = TLptsCryptoUtil.defaultEncryptCredentialsAES(userName,password);
 TLptsCryptoUtil.Credentials aesDefaultCredentials = TLptsCryptoUtil.defaultDecryptCredentialsAES(aesEncrypted);
 System.out.println("User Name and Password are : " + aesDefaultCredentials.userName + " & " + aesDefaultCredentials.password);
 

Parameters:
user - The user name string to be encrypted together with the password.
password - The password string to be encrypted together with the user name.
Returns:
Encrypted bytes of the TLptsCryptoUtil.Credentials class with the credentials supplied.

defaultEncryptCredentialsRSA

public static byte[] defaultEncryptCredentialsRSA(java.lang.String user,
                                                  java.lang.String password)

Encrypts credentials (user name and password) to the internal Credential format using the default RSA cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes.

Notes : The encrypted data can only be read by defaultDecryptCredentialsRSA.

Example :

 String userName = "myUser";
 String password = "veryDifficultPassword";
 byte[] rsaEncrypted = TLptsCryptoUtil.defaultEncryptCredentialsRSA(userName,password);
 TLptsCryptoUtil.Credentials rsaDefaultCredentials = TLptsCryptoUtil.defaultDecryptCredentialsRSA(rsaEncrypted);
 System.out.println("User Name and Password are : " + rsaDefaultCredentials.userName + " & " + rsaDefaultCredentials.password);
 

Parameters:
user - The user name string to be encrypted together with the password.
password - The password string to be encrypted together with the user name.
Returns:
Encrypted bytes of the TLptsCryptoUtil.Credentials class with the credentials supplied.

defaultEncryptRSA

public static byte[] defaultEncryptRSA(byte[] data)

Encrypts the data supplied with the internal default RSA Cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes.

Notes : There is no limit to the length of the data. (unlicenced limit of 128 bytes)

Example :

 byte[] myData = new byte[2048];
 .. initialise myData
 byte[] encryptedData = TLptsCryptoUtil.defaultEncryptRSA(myData);
 byte[] decryptedData = TLptsCryptoUtil.defaultDecryptRSA(encryptedData);
 Result: myData = decryptedData
 

Parameters:
data - The data that should be encrypted.
Returns:
The encrypted data or null if there is an error. Check TLptsLogger for logger error.

defaultForeignEncryptRSA

public static byte[] defaultForeignEncryptRSA(byte[] data)

Encrypts data with a foreign public key, so that it can be transported securely.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes.

Notes : Use this as part of initialising a foreign key for encryption.

Example :

 // somewhere else on this lovely planet of ours
 TLptsCryptoUtil foreignCrypto = new TLptsCryptoUtil();
 foreignCrypto.initCipherRSA(2048, "RSA/ECB/PKCS1Padding","RSA");

 // at our humble dwelling
 TLptsCryptoUtil.defaultInitForeignPublicCipherRSA(foreignCrypto.getPublicRSAModulus(), foreignCrypto.getPublicRSAExponent(),
                                                     foreignCrypto.getRSAKeyBitSize(),
                                                     foreignCrypto.getRSAInstanceString(), foreignCrypto.getRSAKeyString());

 byte[] encryptedData = TLptsCryptoUtil.defaultForeignEncryptRSA(data);

 // back at that lovely place mentioned earlier
 byte[] decryptedData = foreignCrypto.decryptRSA(encryptedData);
 ...
 

Parameters:
data - The local data to encrypt with the foreign public key.
Returns:
The encrypted data or null in case of an error. Check TLptsLogger for logger error.

defaultGetAESKeyBitSize

public static int defaultGetAESKeyBitSize()

Gets the bit size of the AES key.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : N/A

Notes : This is just used for output to the user.

Example :

 int aesKeySize = TLptsCryptoUtil.defaultGetAESKeyBitSize();
 System.out.println("The AES key is " + aesKeySize + " bits in length");
 

Returns:
The number of bits in the key of the default AES cipher.

defaultGetPublicRSAExponent

public static java.math.BigInteger defaultGetPublicRSAExponent()

Gets the public key exponent from the internal default RSA key pair.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes

Notes : Use this as part of initialising a foreign key for encryption.

Example :

 // somewhere on the other side of the pond
 TLptsCryptoUtil foreignCrypto = new TLptsCryptoUtil();

 // on this end of the pond
 sendOurLocalComponents(TLptsCryptoUtil.defaultGetPublicRSAModulus(), TLptsCryptoUtil.defaultGetPublicRSAExponent(),
                        TLptsCryptoUtil.DEFAULT_RSA_KEY_BIT_SIZE,
                        TLptsCryptoUtil.DEFAULT_RSA_INSTANCE_STRING, TLptsCryptoUtil.DEFAULT_RSA_KEY_STRING );

 // back on the other end of the pond
 BigInteger theirModulus = readTheirModulus();
 BigInteger theirExponent = readTheirExponent();
 int theirKeySize = readTheirKeySize();
 String theirInstanceString = readTheirInstanceString();
 String theirKeyString = readTheirKeyString();

 foreignCrypto.initForeignPublicCipherRSA(theirModulus, theirExponent , theirKeySize , theirInstanceString , theirKeyString );

 byte[] encryptedData = foreignCrypto.foreignEncryptRSA(data);

 // On this end of the pond we can decrypt
 byte[] decryptedData = TLptsCryptoUtil.defaultDecryptRSA(encryptedData);
 ...
 

Returns:
The exponent part of the RSA internal Cipher.

defaultGetPublicRSAModulus

public static java.math.BigInteger defaultGetPublicRSAModulus()

Gets the public key modulus from the internal default RSA key pair.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes

Notes : Use this as part of initialising a foreign key for encryption.

Example :

 // somewhere on the other side of the pond
 TLptsCryptoUtil foreignCrypto = new TLptsCryptoUtil();

 // on this end of the pond
 sendOurLocalComponents(TLptsCryptoUtil.defaultGetPublicRSAModulus(), TLptsCryptoUtil.defaultGetPublicRSAExponent(),
                        TLptsCryptoUtil.DEFAULT_RSA_KEY_BIT_SIZE,
                        TLptsCryptoUtil.DEFAULT_RSA_INSTANCE_STRING, TLptsCryptoUtil.DEFAULT_RSA_KEY_STRING );

 // back on the other end of the pond
 BigInteger theirModulus = readTheirModulus();
 BigInteger theirExponent = readTheirExponent();
 int theirKeySize = readTheirKeySize();
 String theirInstanceString = readTheirInstanceString();
 String theirKeyString = readTheirKeyString();

 foreignCrypto.initForeignPublicCipherRSA(theirModulus, theirExponent , theirKeySize , theirInstanceString , theirKeyString );

 byte[] encryptedData = foreignCrypto.foreignEncryptRSA(data);

 // On this end of the pond we can decrypt
 byte[] decryptedData = TLptsCryptoUtil.defaultDecryptRSA(encryptedData);
 ...
 

Returns:
The modulus part of the RSA internal Cipher.

defaultGetRSAKeyBitSize

public static int defaultGetRSAKeyBitSize()

Gets the size of the RSA key in bits.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : N/A

Notes : Use this as part of the initiation of a foreign key.

Example :

 // somewhere else on this lovely planet of ours
 TLptsCryptoUtil foreignCrypto = new TLptsCryptoUtil();
 foreignCrypto.initCipherRSA(2048, "RSA/ECB/PKCS1Padding","RSA");

 // at our humble dwelling
 TLptsCryptoUtil.defaultInitForeignPublicCipherRSA(foreignCrypto.getPublicRSAModulus(), foreignCrypto.getPublicRSAExponent(),
                                                     foreignCrypto.getRSAKeyBitSize(),
                                                     foreignCrypto.getRSAInstanceString(), foreignCrypto.getRSAKeyString());

 byte[] encryptedData = TLptsCryptoUtil.defaultForeignEncryptRSA(data);

 // back at that lovely place mentioned earlier
 byte[] decryptedData = foreignCrypto.decryptRSA(encryptedData);
 ...
 

Returns:
The number of bits in the key of the default RSA cipher.

defaultInitForeignPublicCipherRSA

public static boolean defaultInitForeignPublicCipherRSA(java.math.BigInteger modulus,
                                                        java.math.BigInteger exponent,
                                                        int keyBitLength)

Initiates the default local module so that it can encrypt data with a foreign public key.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : N/A

Notes : Use this if the other crypto has initialised with the default instance and key strings. ("RSA","RSA")
If not, then use the other Init function where the strings can be specified.
Function disabled in unlicenced version.

Example :

 // somewhere else on this lovely planet of ours
 TLptsCryptoUtil foreignCrypto = new TLptsCryptoUtil();
 foreignCrypto.initCipherRSA(2048); // default Cipher instance TLptsCryptoUtil.DEFAULT_RSA_INSTANCE_STRING

 // at our humble dwelling
 TLptsCryptoUtil.defaultInitForeignPublicCipherRSA(foreignCrypto.getPublicRSAModulus(), foreignCrypto.getPublicRSAExponent(),
                                                     foreignCrypto.getRSAKeyBitSize());

 byte[] encryptedData = TLptsCryptoUtil.defaultForeignEncryptRSA(data);

 // back at that lovely place mentioned earlier
 byte[] decryptedData = foreignCrypto.decryptRSA(encryptedData);
 ...
 

Parameters:
modulus - The foreign modulus. If this is coming over the wire as byte[], create this parameter with 'new BigInteger(byte[])'; Use BigInteger.toByteArray() to send this.
exponent - The exponent part of the foreign public key. The same applies here for initialising or getting the bytes.
keyBitLength - The key length in bits.
Returns:
Returns the success status of the foreign key initialisation.

defaultInitForeignPublicCipherRSA

public static boolean defaultInitForeignPublicCipherRSA(java.math.BigInteger modulus,
                                                        java.math.BigInteger exponent,
                                                        int keyBitLength,
                                                        java.lang.String rsaInstanceString,
                                                        java.lang.String rsaKeyString)

Initiates the default local module so that it can encrypt data with a foreign public key.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : N/A

Notes : Use this to initialise a foreign specialised (ie not default) cipher.
Function disabled in unlicenced version.

Example :

 // somewhere else on this lovely planet of ours
 TLptsCryptoUtil foreignCrypto = new TLptsCryptoUtil();
 foreignCrypto.initCipherRSA(2048, "RSA/ECB/PKCS1Padding","RSA");

 // at our humble dwelling
 TLptsCryptoUtil.defaultInitForeignPublicCipherRSA(foreignCrypto.getPublicRSAModulus(), foreignCrypto.getPublicRSAExponent(),
                                                     foreignCrypto.getRSAKeyBitSize(),
                                                     foreignCrypto.getRSAInstanceString(), foreignCrypto.getRSAKeyString());

 byte[] encryptedData = TLptsCryptoUtil.defaultForeignEncryptRSA(data);

 // back at that lovely place mentioned earlier
 byte[] decryptedData = foreignCrypto.decryptRSA(encryptedData);
 ...
 

Parameters:
modulus - The foreign modulus. If this is coming over the wire as byte[], create this parameter with 'new BigInteger(byte[])'; Use BigInteger.toByteArray() to send this.
exponent - The exponent part of the foreign public key. The same applies here for initialising or getting the bytes.
keyBitLength - The key length in bits.
rsaInstanceString - The instantiation string to use for the Cipher. (used by Cipher.getInstance)
rsaKeyString - The instantiation string to use for the Key. (used by KeyFactory.getInstance)
Returns:
Returns the success status of the foreign key initialisation.

defaultWriteAES2File

public static void defaultWriteAES2File(java.lang.String fileName)

Writes the default AES key to a file.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : N/A

Notes : The default key can be written, but it cannot be loaded into the static default crypto.
A new instance is required for loading the saved key.

Example :

 TLptsCryptoUtil.defaultWriteAES2File(TLptsFileUtil.getUserHomeLapetusDirectory() + "aes.default.key.xml");
 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();
 if (!cryptoModule.initCipherAES(TLptsFileUtil.getUserHomeLapetusDirectory() + "aes.default.key.xml"))
   System.out.println("Could not initiate an instance of the crypto module with the default AES key.");
 

Parameters:
fileName - The path of the file that will hold the key in xml hexadecimal format.

defaultWriteRSA2Files

public static void defaultWriteRSA2Files(java.lang.String privateKeyFile,
                                         java.lang.String publicKeyFile)

Writes the default RSA key pair to two files (private and public).

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : N/A

Notes : The default keys can be written, but they cannot be loaded into the static default crypto.
A new instance is required for loading the saved keys.

Example :

 TLptsCryptoUtil.defaultWriteRSA2Files(TLptsFileUtil.getUserHomeLapetusDirectory() + "rsa.default.private.key.xml",
                                         TLptsFileUtil.getUserHomeLapetusDirectory() + "rsa.default.public.key.xml");

 TLptsCryptoUtil anotherCryptoModule = new TLptsCryptoUtil();
 if (!anotherCryptoModule.initCipherRSA(TLptsFileUtil.getUserHomeLapetusDirectory() + "rsa.default.private.key.xml",
                                        TLptsFileUtil.getUserHomeLapetusDirectory() + "rsa.default.public.key.xml"))
 {
   System.out.println("Could not instantiate RSA cipher from file!");
   return;
 }

 

Parameters:
privateKeyFile - The full path of the file to save the private key. (keep this file, not to be shared)
publicKeyFile - The full path of the file to save the public key. (give this file to others for encrypting data that only you can read)

encryptAES

public byte[] encryptAES(byte[] data)

Encrypts the data with the instantiation's AES cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes

Notes : There is no limit to the length of the data. (unlicenced limit of 128 bytes)
Example :

 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();

 if (!cryptoModule.initCipherAES(128))
 {
   System.out.println("Could not instantiate AES cipher!");
   return;
 }
 byte[] data = initMyData();
 byte[] encryptedData = cryptoModule.encryptAES(data);
 ... // store it or send it
 byte[] decryptedData = cryptoModule.decryptAES(encryptedData);
 ...
 

Parameters:
data - The data to be encrypted.
Returns:
Returns the encrypted bytes or null if there is an error.

encryptCredentialsAES

public byte[] encryptCredentialsAES(java.lang.String user,
                                    java.lang.String password)

Encrypts credentials (user name and password) to the internal Credential format using the default AES cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes.

Notes : The encrypted data can only be read by defaultDecryptCredentialsAES.

Example :

 String userName = "myUser";
 String password = "veryDifficultPassword";

 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();

 if (!cryptoModule.initCipherAES(128))
 {
   System.out.println("Could not instantiate AES cipher!");
   return;
 }

 byte[] aesEncrypted = cryptoModule.encryptCredentialsAES(userName,password);

 TLptsCryptoUtil.Credentials aesCredentials = cryptoModule.decryptCredentialsAES(aesEncrypted);
 System.out.println("User Name and Password are : " + aesCredentials.userName + " & " + aesCredentials.password);
 

Parameters:
user - The user name string to be encrypted together with the password.
password - The password string to be encrypted together with the user name.
Returns:
Encrypted bytes of the TLptsCryptoUtil.Credentials class with the credentials supplied.

encryptCredentialsRSA

public byte[] encryptCredentialsRSA(java.lang.String user,
                                    java.lang.String password)

Encrypts credentials (user name and password) to the internal Credential format using the default RSA cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes.

Notes : The encrypted data can only be read by decryptCredentialsRSA.

Example :

 String userName = "myUser";
 String password = "veryDifficultPassword";

 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();

 if (!cryptoModule.initCipherRSA(1024))
 {
   System.out.println("Could not instantiate RSA cipher!");
   return;
 }

 byte[] rsaEncrypted = cryptoModule.encryptCredentialsRSA(userName,password);

 TLptsCryptoUtil.Credentials rsaCredentials = cryptoModule.decryptCredentialsRSA(rsaEncrypted);
 System.out.println("User Name and Password are : " + rsaCredentials.userName + " & " + rsaCredentials.password);
 

Parameters:
user - The user name string to be encrypted together with the password.
password - The password string to be encrypted together with the user name.
Returns:
Encrypted bytes of the TLptsCryptoUtil.Credentials class with the credentials supplied.

encryptRSA

public byte[] encryptRSA(byte[] data)

Encrypts the data with the instantiation's RSA cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes

Notes : There is no limit to the length of the data. (unlicenced limit of 128 bytes)
Example :

 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();

 if (!cryptoModule.initCipherRSA(4096)) // pretty large key
 {
   System.out.println("Could not instantiate RSA cipher!");
   return;
 }
 byte[] data = initMyData();
 byte[] encryptedData = cryptoModule.encryptRSA(data);
 ... // store it or send it
 byte[] decryptedData = cryptoModule.decryptRSA(encryptedData);
 ...
 

Parameters:
data - The data to be encrypted.
Returns:
Returns the encrypted bytes or null if there is an error.

foreignEncryptRSA

public byte[] foreignEncryptRSA(byte[] data)
Encrypts data using the foreign RSA key.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes.

Notes : There is no limit to the length of the data. (unlicenced version returns null)

Example :

 // somewhere on the other side of the pond
 TLptsCryptoUtil foreignCrypto = new TLptsCryptoUtil();
 foreignCrypto.initCipherRSA(2048);

 // on this end of the pond
 TLptsCryptoUtil localCrypto = new TLptsCryptoUtil();
 localCrypto.initForeignPublicCipherRSA(foreignCrypto.getPublicRSAModulus(), foreignCrypto.getPublicRSAExponent(),
                                        foreignCrypto.getRSAKeyBitSize());

 byte[] encryptedData = localCrypto.foreignEncryptRSA(data);

 // back on the other end of the pond
 byte[] decryptedData = foreignCrypto.decryptRSA(encryptedData);
 ...
 

Parameters:
data - The data to be encrypted.
Returns:
The encrypted data which can only be read by the foreign cipher (with it's private key).

generateKey4AES

public byte[] generateKey4AES(int bitSize)
Generates a random key for the AES cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes.

Notes : Use this every time a new cipher is created. Useful in generating a new key for every communication session.

In the example below there are log messages for the attempts to create 256 and 192 bit ciphers (if they are not successful).
Example :

 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();

 if (!cryptoModule.initCipherAES(cryptoModule.generateKey4AES(256)))       // may not be able to do this
   if (!cryptoModule.initCipherAES(cryptoModule.generateKey4AES(192)))     // may not be able to do this
     if (!cryptoModule.initCipherAES(cryptoModule.generateKey4AES(128)))   // should be able to do this
     {
       System.out.println("Could not generate at least a 128 bit key for AES");
       return;
     }
 

Parameters:
bitSize - Either 128, 192 or 256.
Returns:
The newly generate key or null if not successful. A warning is logged if not successful.

getAESInstanceString

public java.lang.String getAESInstanceString()

Gets the instance string used to create this instance's AES cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes, but is initialised internally to "AES/ECB/PKCS5Padding". Could be changed by initCipherAES.

Notes :

Example :


 TLptsCryptoUtil crypto = new TLptsCryptoUtil();
 crypto.initCipherAES(128,"AES","AES");

 System.out.println("AES Key Bit Size = " + cryptoModule.getAESKeyBitSize());
 System.out.println("AES Instance String = " + cryptoModule.getAESInstanceString());
 System.out.println("AES Key String = " + cryptoModule.getAESKeyString());
 ...
 Result : AES Key Bit Size = 128
          AES Instance String = AES          //  not default
          AES Key String = AES

 

Returns:
The modulus part of the RSA internal Cipher.

getAESKeyBitSize

public int getAESKeyBitSize()

Gets the public key modulus from the internal default RSA key pair.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes

Notes : Use this as part of initialising a foreign key for encryption.

Example :


 TLptsCryptoUtil crypto = new TLptsCryptoUtil();
 crypto.initCipherAES(128,"AES","AES");

 System.out.println("AES Key Bit Size = " + cryptoModule.getAESKeyBitSize());
 System.out.println("AES Instance String = " + cryptoModule.getAESInstanceString());
 System.out.println("AES Key String = " + cryptoModule.getAESKeyString());
 ...
 Result : AES Key Bit Size = 128
          AES Instance String = AES          //  not standard
          AES Key String = AES

 

Returns:
The modulus part of the RSA internal Cipher.

getAESKeyString

public java.lang.String getAESKeyString()

Gets the key string used to create this instance's AES cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes, but is initialised internally to "AES". Could be changed by initCipherAES.

Notes :

Example :


 TLptsCryptoUtil crypto = new TLptsCryptoUtil();
 crypto.initCipherAES(128,"AES","AES");

 System.out.println("AES Key Bit Size = " + cryptoModule.getAESKeyBitSize());
 System.out.println("AES Instance String = " + cryptoModule.getAESInstanceString());
 System.out.println("AES Key String = " + cryptoModule.getAESKeyString());
 ...
 Result : AES Key Bit Size = 128
          AES Instance String = AES          //  not standard
          AES Key String = AES

 

Returns:
The modulus part of the RSA internal Cipher.

getPublicRSAExponent

public java.math.BigInteger getPublicRSAExponent()

Gets the public key modulus from the internal default RSA key pair.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes

Notes : Use this as part of initialising a foreign key for encryption.

Example :

 // somewhere on the other side of the pond
 TLptsCryptoUtil foreignCrypto = new TLptsCryptoUtil();
 foreignCrypto.initCipherRSA(2048);

 // on this end of the pond
 TLptsCryptoUtil localCrypto = new TLptsCryptoUtil();
 localCrypto.initForeignPublicCipherRSA(foreignCrypto.getPublicRSAModulus(), foreignCrypto.getPublicRSAExponent(), foreignCrypto.getRSAKeyBitSize(),
                                        foreignCrypto.getRSAInstanceString(), foreignCrypto.getRSAKeyString());

 byte[] encryptedData = localCrypto.foreignEncryptRSA(data);

 // back on the other end of the pond
 byte[] decryptedData = foreignCrypto.decryptRSA(encryptedData);
 ...
 

Returns:
The modulus part of the RSA internal Cipher.

getPublicRSAModulus

public java.math.BigInteger getPublicRSAModulus()

Gets the public key modulus from the internal default RSA key pair.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes

Notes : Use this as part of initialising a foreign key for encryption.

Example :

 // somewhere on the other side of the pond
 TLptsCryptoUtil foreignCrypto = new TLptsCryptoUtil();
 foreignCrypto.initCipherRSA(2048);

 // on this end of the pond
 TLptsCryptoUtil localCrypto = new TLptsCryptoUtil();
 localCrypto.initForeignPublicCipherRSA(foreignCrypto.getPublicRSAModulus(), foreignCrypto.getPublicRSAExponent(), foreignCrypto.getRSAKeyBitSize(),
                                        foreignCrypto.getRSAInstanceString(), foreignCrypto.getRSAKeyString());

 byte[] encryptedData = localCrypto.foreignEncryptRSA(data);

 // back on the other end of the pond
 byte[] decryptedData = foreignCrypto.decryptRSA(encryptedData);
 ...
 

Returns:
The modulus part of the RSA internal Cipher.

getRSAInstanceString

public java.lang.String getRSAInstanceString()

Gets the RSA instance string used to create this instance's cipher (used by Cipher.getInstance).

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes, but is initialised internally to "RSA". Could be changed by initCipherRSA.

Notes : Use this as part of initialising a foreign key for encryption.

Example :

 // somewhere on the other side of the pond
 TLptsCryptoUtil foreignCrypto = new TLptsCryptoUtil();
 foreignCrypto.initCipherRSA(2048);

 // on this end of the pond
 TLptsCryptoUtil localCrypto = new TLptsCryptoUtil();
 localCrypto.initForeignPublicCipherRSA(foreignCrypto.getPublicRSAModulus(), foreignCrypto.getPublicRSAExponent(),
                                        foreignCrypto.getRSAKeyBitSize(),
                                        foreignCrypto.getRSAInstanceString(), foreignCrypto.getRSAKeyString());

 byte[] encryptedData = localCrypto.foreignEncryptRSA(data);

 // back on the other end of the pond
 byte[] decryptedData = foreignCrypto.decryptRSA(encryptedData);
 ...
 

Returns:
The string for the cipher instance. Default is "RSA".

getRSAKeyBitSize

public int getRSAKeyBitSize()

Gets the public key modulus from the internal default RSA key pair.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes

Notes : Use this as part of initialising a foreign key for encryption.

Example :

 // somewhere on the other side of the pond
 TLptsCryptoUtil foreignCrypto = new TLptsCryptoUtil();
 foreignCrypto.initCipherRSA(2048);

 // on this end of the pond
 TLptsCryptoUtil localCrypto = new TLptsCryptoUtil();
 localCrypto.initForeignPublicCipherRSA(foreignCrypto.getPublicRSAModulus(), foreignCrypto.getPublicRSAExponent(),
                                        foreignCrypto.getRSAKeyBitSize(),
                                        foreignCrypto.getRSAInstanceString(), foreignCrypto.getRSAKeyString());

 byte[] encryptedData = localCrypto.foreignEncryptRSA(data);

 // back on the other end of the pond
 byte[] decryptedData = foreignCrypto.decryptRSA(encryptedData);
 ...
 

Returns:
The modulus part of the RSA internal Cipher.

getRSAKeyString

public java.lang.String getRSAKeyString()

Gets the RSA key string as sed by KeyFactory.getInstance to create the key for this cipher.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Yes, but is initialised internally to "RSA". Could be changed by initCipherRSA.

Notes : Use this as part of initialising a foreign key for encryption.

Example :

 // somewhere on the other side of the pond
 TLptsCryptoUtil foreignCrypto = new TLptsCryptoUtil();
 foreignCrypto.initCipherRSA(2048);

 // on this end of the pond
 TLptsCryptoUtil localCrypto = new TLptsCryptoUtil();
 localCrypto.initForeignPublicCipherRSA(foreignCrypto.getPublicRSAModulus(), foreignCrypto.getPublicRSAExponent(),
                                        foreignCrypto.getRSAKeyBitSize(),
                                        foreignCrypto.getRSAInstanceString(), foreignCrypto.getRSAKeyString());

 byte[] encryptedData = localCrypto.foreignEncryptRSA(data);

 // back on the other end of the pond
 byte[] decryptedData = foreignCrypto.decryptRSA(encryptedData);
 ...
 

Returns:
The modulus part of the RSA internal Cipher.

initCipherAES

public boolean initCipherAES(byte[] key)

Creates and initialises the AES cipher for this module.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : N/A.

Notes :
Example :

 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();

 if (!cryptoModule.initCipherAES(cryptoModule.generateKey4AES(256)))       // may not be able to do this
   if (!cryptoModule.initCipherAES(cryptoModule.generateKey4AES(192)))     // may not be able to do this
     if (!cryptoModule.initCipherAES(cryptoModule.generateKey4AES(128)))   // should be able to do this
     {
       System.out.println("Could not generate at least a 128 bit key for AES");
       return;
     }
 

Parameters:
key - The key to use for creating this cipher.
Returns:
Returns true on success. Otherwise an error is logged in TLptsLogger and false is returned.

initCipherAES

public boolean initCipherAES(byte[] key,
                             java.lang.String aesInstanceString,
                             java.lang.String aesKeyString)

Creates and initialises the AES cipher for this module.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : N/A.

Notes : In the example below the cipher created can only be used for encrypted data up to the block size of the cipher.
The block size is normally the size of the key for the AES cipher, so the cipher below will report a padding error if the data is larger than 16 bytes.
To solve this the instance string should be "AES/ECB/PKCS5Padding", which allows the crypto module to chain endless amounts of blocks for encryption.
Example :

 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();

 if (!cryptoModule.initCipherAES(cryptoModule.generateKey4AES(128),"AES","AES"))
 {
   System.out.println("Could not instantiate AES cipher!");
   return;
 }

 

Parameters:
key - The key to use for creating this cipher.
aesInstanceString - Format "AES/Blocking/Padding". The default is "AES/ECB/PKCS5Padding". (used by Cipher.getInstance)
aesKeyString - Default is "AES". (used by KeyFactory.getInstance)
Returns:
Returns true on success. Otherwise an error is logged in TLptsLogger and false is returned.

initCipherAES

public boolean initCipherAES(java.lang.String fileName)

Creates and initialises the AES cipher for this module.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : N/A.

Notes : The instance string and key string (used by Cipher.getInstance and KeyFactory.getInstance) are taken from the key file
and used to initiate the cipher. (which emulates the cipher that was used to save the file)
Example :

 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();

 if (!cryptoModule.initCipherAES(TLptsFileUtil.getUserHomeLapetusDirectory() + "my.aes.key.xml"))
 {
   System.out.println("Could not instantiate AES cipher!");
   return;
 }
 System.out.println("AES Key Bit Size = " + cryptoModule.getAESKeyBitSize());
 System.out.println("AES Instance String = " + cryptoModule.getAESInstanceString());
 System.out.println("AES Key String = " + cryptoModule.getAESKeyString());

 

Parameters:
fileName - The path to the xml key file, which must have the root element XLptsCipherKeyType. (normally created with writeAES2File)
Returns:
Returns true on success. Otherwise an error is logged in TLptsLogger and false is returned.

initCipherRSA

public boolean initCipherRSA(int keyBitSize)

Initiates the RSA cryptographic capability, using the size and type supplied.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : n/a

Notes :

Example :

 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();
 if (!cryptoModule.initCipherRSA(4096))  / initiated with "RSA" and "RSA"
 {
   System.out.println("Could not instantiate RSA cipher!");
   return;
 }
 

Parameters:
keyBitSize - Either 512, 1024, 2048 or 4096.
Returns:
Returns true on success. Otherwise an error is logged in TLptsLogger and false is returned.

initCipherRSA

public boolean initCipherRSA(int keyBitSize,
                             java.lang.String rsaInstanceString,
                             java.lang.String rsaKeyString)

Initiates the RSA cryptographic capability, using the size and type supplied.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : n/a

Notes :

Example :

 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();
 if (!cryptoModule.initCipherRSA(1024,"RSA/ECB/PKCS1Padding","RSA"))
 {
   System.out.println("Could not instantiate RSA cipher!");
   return;
 }
 

Parameters:
keyBitSize - Either 512, 1024, 2048 or 4096.
rsaInstanceString - Format "RSA/Blocking/Padding". The default is "RSA".
rsaKeyString - Default is "RSA".
Returns:
Returns true on success. Otherwise an error is logged in TLptsLogger and false is returned.

initCipherRSA

public boolean initCipherRSA(java.lang.String privateKeyFile,
                             java.lang.String publicKeyFile)

Initiates the RSA cryptographic capability, using the key pair in the files supplied.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : n/a

Notes :

Example :

 TLptsCryptoUtil.defaultWriteRSA2Files(TLptsFileUtil.getUserHomeLapetusDirectory() + "rsa.default.private.key.xml",
                                         TLptsFileUtil.getUserHomeLapetusDirectory() + "rsa.default.public.key.xml");

 TLptsCryptoUtil anotherCryptoModule = new TLptsCryptoUtil();
 if (!anotherCryptoModule.initCipherRSA(TLptsFileUtil.getUserHomeLapetusDirectory() + "rsa.default.private.key.xml",
                                        TLptsFileUtil.getUserHomeLapetusDirectory() + "rsa.default.public.key.xml"))
 {
   System.out.println("Could not instantiate RSA cipher from file!");
   return;
 }
 

Parameters:
privateKeyFile - The xml file with the element XLptsCipherKeyType that contains the private key of the RSA key pair.
publicKeyFile - The XLptsCipherKeyType element xml file with the public key of the RSA key pair.
Returns:
Returns true on success. Otherwise an error is logged in TLptsLogger and false is returned.

initForeignPublicCipherRSA

public boolean initForeignPublicCipherRSA(java.math.BigInteger modulus,
                                          java.math.BigInteger exponent,
                                          int keyBitLength)

Initiates the foreign RSA cipher with the public key, so that encryption is possible.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : N/A.

Notes : Use this as part of initialising a foreign key for encryption. (unlicenced version returns false)

Example :

 // somewhere on the other side of the pond
 TLptsCryptoUtil foreignCrypto = new TLptsCryptoUtil();
 foreignCrypto.initCipherRSA(2048);

 // on this end of the pond
 TLptsCryptoUtil localCrypto = new TLptsCryptoUtil();
 localCrypto.initForeignPublicCipherRSA(foreignCrypto.getPublicRSAModulus(), foreignCrypto.getPublicRSAExponent(),
                                        foreignCrypto.getRSAKeyBitSize());,

 byte[] encryptedData = localCrypto.foreignEncryptRSA(data);

 // back on the other end of the pond
 byte[] decryptedData = foreignCrypto.decryptRSA(encryptedData);
 ...
 

Parameters:
modulus - The foreign modulus. If this is coming over the wire as byte[], create this parameter with 'new BigInteger(byte[])'; Use BigInteger.toByteArray() to send this.
exponent - The exponent part of the foreign public key. The same applies here for initialising or getting the bytes.
keyBitLength - The bit length of the foreign cipher.
Returns:
Returns true on success. Otherwise an error is logged in TLptsLogger and false is returned.

initForeignPublicCipherRSA

public boolean initForeignPublicCipherRSA(java.math.BigInteger modulus,
                                          java.math.BigInteger exponent,
                                          int keyBitLength,
                                          java.lang.String rsaInstanceString,
                                          java.lang.String rsaKeyString)

Initiates the foreign RSA cipher with the public key, so that encryption is possible.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : N/A.

Notes : Use this as part of initialising a foreign key for encryption. (unlicenced version returns false)

Example :

 // somewhere on the other side of the pond
 TLptsCryptoUtil foreignCrypto = new TLptsCryptoUtil();
 foreignCrypto.initCipherRSA(2048);

 // on this end of the pond
 TLptsCryptoUtil localCrypto = new TLptsCryptoUtil();
 localCrypto.initForeignPublicCipherRSA(foreignCrypto.getPublicRSAModulus(), foreignCrypto.getPublicRSAExponent(),
                                        foreignCrypto.getRSAKeyBitSize(),
                                        foreignCrypto.getRSAInstanceString(), foreignCrypto.getRSAKeyString());

 byte[] encryptedData = localCrypto.foreignEncryptRSA(data);

 // back on the other end of the pond
 byte[] decryptedData = foreignCrypto.decryptRSA(encryptedData);
 ...
 

Parameters:
modulus - The foreign modulus. If this is coming over the wire as byte[], create this parameter with 'new BigInteger(byte[])'; Use BigInteger.toByteArray() to send this.
exponent - The exponent part of the foreign public key. The same applies here for initialising or getting the bytes.
keyBitLength - The bit length of the foreign cipher.
rsaInstanceString - Format "RSA/Blocking/Padding". The default is "RSA".
rsaKeyString - Default is "RSA".
Returns:
Returns true on success. Otherwise an error is logged in TLptsLogger and false is returned.

writeAES2File

public void writeAES2File(java.lang.String keyFile)

Writes the AES key to a file for later use.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : N/A

Notes :

Example :

 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();

 if (!cryptoModule.initCipherAES(128))
 {
   System.out.println("Could not instantiate AES cipher!");
   return;
 }
 cryptoModule.writeAES2File(TLptsFileUtil.getUserHomeLapetusDirectory() + "aes.default.key.xml");

 TLptsCryptoUtil anotherCryptoModule = new TLptsCryptoUtil();
 if (!anotherCryptoModule.initCipherAES(TLptsFileUtil.getUserHomeLapetusDirectory() + "aes.default.key.xml"))
   System.out.println("Could not initiate an instance of the crypto module with the AES key.");
 

Parameters:
keyFile - The path of the file that will hold the key in xml hexadecimal format (XLptsCipherKeyType element)

writeRSA2Files

public void writeRSA2Files(java.lang.String privateKeyFile,
                           java.lang.String publicKeyFile)

Writes the RSA key pair to two files (private and public).

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : N/A

Notes :

Example :

 TLptsCryptoUtil cryptoModule = new TLptsCryptoUtil();

 if (!cryptoModule.initCipherRSA(2048))
 {
   System.out.println("Could not instantiate RSA cipher!");
   return;
 }
 cryptoModule.writeRSA2Files(TLptsFileUtil.getUserHomeLapetusDirectory() + "rsa.default.private.key.xml",
                             TLptsFileUtil.getUserHomeLapetusDirectory() + "rsa.default.public.key.xml");

 TLptsCryptoUtil anotherCryptoModule = new TLptsCryptoUtil();
 if (!anotherCryptoModule.initCipherRSA(TLptsFileUtil.getUserHomeLapetusDirectory() + "rsa.default.private.key.xml",
                                        TLptsFileUtil.getUserHomeLapetusDirectory() + "rsa.default.public.key.xml"))
 {
   System.out.println("Could not instantiate RSA cipher from file!");
   return;
 }

 

Parameters:
privateKeyFile - The full path of the file to save the private key. (keep this file, not to be shared)
publicKeyFile - The full path of the file to save the public key. (give this file to others for encrypting data that only you can read)

zI

public static void zI()
Obfuscated, as it is not required by the application.



Copyright 2009,2010, Lapetus Systems Ltd. (All rights reserved)