com.softabar.crypt
Class SoftaCrypt

java.lang.Object
  extended bycom.softabar.crypt.SoftaCrypt

public class SoftaCrypt
extends java.lang.Object

SoftaCrypt is a single class implementation for common encryption and message digest algorithms. This class implements following encryption algorithms:

and following digest algorithms: This class implements also: SoftaCrypt class requires JDK 1.1 or later.

AES and TwoFish uses 128-bit or 256-bit key and block size is 128-bits for both algorithms.
XTEA uses 128-bit key and block size 64-bits.

Version:
1.0 Copyright (C) 2005 softabar.com

Field Summary
static int AES
          Constant for AES (Advanced Encryption Standard) block cipher algorithm.
static int TWOFISH
          Constant for TwoFish block cipher algorithm.
static int XTEA
          Constant for XTEA (eXtended Tiny Encryption Algorithm) block cipher algorithm.
 
Constructor Summary
SoftaCrypt()
          Empty constructor.
 
Method Summary
 boolean areEqual(byte[] a, byte[] b)
          Convenience method to check byte array equality.
 void base64Decode(java.io.InputStream inputStream, java.io.OutputStream outputStream)
          Decodes inputStream to outputStream using Base64.
 void base64Encode(java.io.InputStream inputStream, java.io.OutputStream outputStream)
          Encodes inputStream to outputStream using Base64.
 int crc32(java.io.InputStream inputStream)
          Calculates Cyclic Redundancy Check, CRC32, from inputStream.
 void decrypt(java.io.InputStream inputStream, java.io.OutputStream outputStream)
          Decrypts inputStream to outputStream using operation specified by setAlgorithm(int) and key specified by setKey(byte[]).
 void encrypt(java.io.InputStream inputStream, java.io.OutputStream outputStream)
          Encrypts inputStream to outputStream using operation specified by setAlgorithm(int) and key specified by setKey(byte[]).
 int getAlgorithm()
          Returns current algorithm.
 byte[] getKey()
          Returns current key.
 int getXTEARounds()
           
 boolean isCloseStreams()
           
 void reset()
          Resets instance for use in other operation.
 void setAlgorithm(int algorithm)
          Sets encryption/decryption algorithm for this instance.
 void setCloseStreams(boolean closeStreams)
          Specify whether or not to close stream after operation (encrypt/decrypt, hash or base64).
 void setKey(byte[] key)
          Sets key for encryption/decryption.
 void setXTEARounds(int rounds)
          Set rounds for XTEA operation.
 byte[] sha1(java.io.InputStream inputStream)
          Calculates 160-bit message digest using SHA-1 from inputStream.
 byte[] sha224(java.io.InputStream inputStream)
          Calculates 224-bit message digest using SHA-224 from inputStream.
 byte[] sha256(java.io.InputStream inputStream)
          Calculates 256-bit message digest using SHA-256 from inputStream.
 byte[] sha384(java.io.InputStream inputStream)
          Calculates 384-bit message digest using SHA-384 from inputStream.
 byte[] sha512(java.io.InputStream inputStream)
          Calculates 512-bit message digest using SHA-512 from inputStream.
 byte[] to16Bytes(byte[] bytes)
          Modifies array to 16 bytes.
 byte[] to32Bytes(byte[] bytes)
          Modifies array to 32 bytes.
 java.lang.String toHexString(byte[] b)
          Convenience method to print byte array as hexadecimal string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TWOFISH

public static final int TWOFISH
Constant for TwoFish block cipher algorithm.

See Also:
Constant Field Values

AES

public static final int AES
Constant for AES (Advanced Encryption Standard) block cipher algorithm.

See Also:
Constant Field Values

XTEA

public static final int XTEA
Constant for XTEA (eXtended Tiny Encryption Algorithm) block cipher algorithm.

See Also:
Constant Field Values
Constructor Detail

SoftaCrypt

public SoftaCrypt()
Empty constructor. SoftaCrypt instance must be initialised using setKey(byte[]) and setAlgorithm(int).

Method Detail

setKey

public void setKey(byte[] key)
Sets key for encryption/decryption. Key length must be 16-bytes for 128-bit key and 32-bytes for 256-bit key.

Parameters:
key - Key as byte array.
Throws:
java.lang.IllegalArgumentException - If key length is not 16 or 32 bytes.

getKey

public byte[] getKey()
Returns current key.

Returns:
Current key.

setAlgorithm

public void setAlgorithm(int algorithm)
Sets encryption/decryption algorithm for this instance. Algorithm must be AES, TWOFISH or XTEA.
Default is AES.

Parameters:
algorithm - Algorithm to use in encryption/decryption.
Throws:
java.lang.IllegalArgumentException - If algorithm is not AES, TWOFISH or XTEA.

getAlgorithm

public int getAlgorithm()
Returns current algorithm.

Returns:
Current algorithm.

setCloseStreams

public void setCloseStreams(boolean closeStreams)
Specify whether or not to close stream after operation (encrypt/decrypt, hash or base64).
Default is true.

Parameters:
closeStreams - True to close streams after operation. False otherwise.

isCloseStreams

public boolean isCloseStreams()
Returns:
Current value for closeStreams.

reset

public void reset()
Resets instance for use in other operation. This must be called before each consequent operation. If not called results are not correct.


encrypt

public void encrypt(java.io.InputStream inputStream,
                    java.io.OutputStream outputStream)
             throws java.io.IOException
Encrypts inputStream to outputStream using operation specified by setAlgorithm(int) and key specified by setKey(byte[]). Data is read from inputStream and encrypted to outputStream.

Input data is padded when necessary.

Parameters:
inputStream - Data to encrypt.
outputStream - Encrypted data.
Throws:
java.io.IOException - If error occurred while reading from inputStream or writing to outputStream.
java.lang.IllegalStateException - If key is not specified or if using 256-bit key for XTEA algorithm.

decrypt

public void decrypt(java.io.InputStream inputStream,
                    java.io.OutputStream outputStream)
             throws java.io.IOException
Decrypts inputStream to outputStream using operation specified by setAlgorithm(int) and key specified by setKey(byte[]). Encrypted data is read from inputStream and decrypted to outputStream.

Output data includes padding if input data was padded during encrypt operation.

Parameters:
inputStream - Data to decrypt.
outputStream - Decrypted data.
Throws:
java.io.IOException - If error occurred while reading from inputStream or writing to outputStream.
java.lang.IllegalStateException - If key is not specified or if using 256-bit key for XTEA algorithm.

sha1

public byte[] sha1(java.io.InputStream inputStream)
            throws java.io.IOException
Calculates 160-bit message digest using SHA-1 from inputStream.

Parameters:
inputStream - Calculates SHA-1 from this InputStream.
Returns:
SHA-1 digest as byte array (length 20 bytes).
Throws:
java.io.IOException - If error occurred while reading from inputStream.

sha224

public byte[] sha224(java.io.InputStream inputStream)
              throws java.io.IOException
Calculates 224-bit message digest using SHA-224 from inputStream.

Parameters:
inputStream - Calculates SHA-224 from this InputStream.
Returns:
SHA-224 digest as byte array (length 28 bytes).
Throws:
java.io.IOException - If error occurred while reading from inputStream.

sha256

public byte[] sha256(java.io.InputStream inputStream)
              throws java.io.IOException
Calculates 256-bit message digest using SHA-256 from inputStream.

Parameters:
inputStream - Calculates SHA-256 from this InputStream.
Returns:
SHA-256 digest as byte array (length 32 bytes).
Throws:
java.io.IOException - If error occurred while reading from inputStream.

sha384

public byte[] sha384(java.io.InputStream inputStream)
              throws java.io.IOException
Calculates 384-bit message digest using SHA-384 from inputStream.

Parameters:
inputStream - Calculates SHA-384 from this InputStream.
Returns:
SHA-384 digest as byte array (length 48 bytes).
Throws:
java.io.IOException - If error occurred while reading from inputStream.

sha512

public byte[] sha512(java.io.InputStream inputStream)
              throws java.io.IOException
Calculates 512-bit message digest using SHA-512 from inputStream.

Parameters:
inputStream - Calculates SHA-512 from this InputStream.
Returns:
SHA-512 digest as byte array (length 64 bytes).
Throws:
java.io.IOException - If error occurred while reading from inputStream.

crc32

public int crc32(java.io.InputStream inputStream)
          throws java.io.IOException
Calculates Cyclic Redundancy Check, CRC32, from inputStream.

CRC32 value is calculated using polynomial
X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
Start value of calculation is 0xffffffff and result is XORed with 0xffffffff.
This is the same CRC32 calculation as used in ZIP.

Parameters:
inputStream - Data to calculate CRC32.
Returns:
32-bit CRC32 value of input data.
Throws:
java.io.IOException - If error occurred while reading from inputStream.

base64Encode

public void base64Encode(java.io.InputStream inputStream,
                         java.io.OutputStream outputStream)
                  throws java.io.IOException
Encodes inputStream to outputStream using Base64. Data to encode is read from inputStream and encoded data is written to outputStream. Encoded Base64 data has 68 characters per line and '\r\n' is used as line break.

Parameters:
inputStream - Data to encode.
outputStream - Decoded data.
Throws:
java.io.IOException - If error occurred while reading from inputStream or writing to outputStream.

base64Decode

public void base64Decode(java.io.InputStream inputStream,
                         java.io.OutputStream outputStream)
                  throws java.io.IOException
Decodes inputStream to outputStream using Base64. Data to decode is read from inputStream and decoded data is written to outputStream.

Parameters:
inputStream - Data to decode.
outputStream - Decoded data.
Throws:
java.io.IOException - If error occurred while reading from inputStream or writing to outputStream.

setXTEARounds

public void setXTEARounds(int rounds)
Set rounds for XTEA operation. More rounds means more secure. Default is 32, 6 is sufficient for encryption of real-time data.

Parameters:
rounds - Rounds for XTEA operation.

getXTEARounds

public int getXTEARounds()
Returns:
Current rounds for XTEA operation.

toHexString

public java.lang.String toHexString(byte[] b)
Convenience method to print byte array as hexadecimal string.

Parameters:
b - Byte array to print.
Returns:
Hexadecimal string presentation of byte array.

areEqual

public boolean areEqual(byte[] a,
                        byte[] b)
Convenience method to check byte array equality.

Parameters:
a - Byte array a.
b - Byte array b.
Returns:
True if a has the same length and content as b. False otherwise.

to16Bytes

public byte[] to16Bytes(byte[] bytes)
Modifies array to 16 bytes.
If original length is less than 16 bytes -> zeros are appended to array.
If original length is more than 16 bytes -> bytes at the end of array are discarded and resulting array is the left most 16 bytes of the original array.

This method can be used to make sure that encryption/decryption keys have correct length.

Parameters:
bytes - Byte array to modify.
Returns:
Modified byte array.

to32Bytes

public byte[] to32Bytes(byte[] bytes)
Modifies array to 32 bytes.
If original length is less than 32 bytes -> zeros are appended to array.
If original length is more than 32 bytes -> bytes at the end of array are discarded and resulting array is the left most 32 bytes of the original array.

This method can be used to make sure that encryption/decryption keys have correct length.

Parameters:
bytes - Byte array to modify.
Returns:
Modified byte array.