Java Randomness Test Suite

JRandTest at SourceForge

com.fasteasytrade.JRandTest.Algo
Class ZAC3

java.lang.Object
  extended bycom.fasteasytrade.JRandTest.Algo.Cipher
      extended bycom.fasteasytrade.JRandTest.Algo.ZAC3

public class ZAC3
extends Cipher

ZAC3 encryption algorithm Copyright (c) 2005 Zur Aougav

Symetric algorithm.

Use 3 ARC4 keys:

Hence, ciphered text length is double input's length.

Before encryption starts, each key is skipped a random number of times (with 4096 <= skip <= 69631). SHA1 digest message (20 bytes) is calculated from the invisible key output. The 20 bytes should be written at the header of the ciphered message/file. Of course, decryption does the reverse, read the hash 20 bytes, and step each key with a check if digest message arrived at is the correct one. Hence, the random number of steps is never exposed directly, but the hash contains implicitly the correct position.

Author:
Zur Aougav

Field Summary
(package private)  byte[] controlBuf
          Original key bytes buffer for controlKey
(package private)  RC4Key controlKey
          controlKey gives one byte to enable swap of encrypt data and fill data (from fillKey)
(package private)  byte[] controlShaDigest
          controlKey is processed controlSkip times, and prng data is hashed using sha1.
(package private)  int controlSkip
          random number we skip controlKey before start using it.
(package private)  byte[] cryptBuf
          Original key bytes buffer for cryptKey
(package private)  RC4Key cryptKey
          encrypt and decrypt data using cryptKey
(package private)  byte[] cryptShaDigest
          cryptKey is processed cryptSkip times, and prng data is hashed using sha1.
(package private)  int cryptSkip
          random number we skip cryptKey before start using it.
(package private)  byte[] fillBuf
          Original key bytes buffer for fillKey
(package private)  RC4Key fillKey
          add one byte for each encrypt data byte
(package private)  byte[] fillShaDigest
          fillKey is processed fillSkip times, and prng data is hashed using sha1.
(package private)  int fillSkip
          random number we skip fillKey before start using it.
(package private)  boolean garbage
          Boolean flag if we ever find an incorrect fill byte in data.
(package private)  Random rnd
          Use java random class to fill new keys put some garbage bytes in output buffer (if decryption fails).
 
Constructor Summary
ZAC3()
          default constructor - does nothing.
ZAC3(byte[] cryptBuf, byte[] fillBuf, byte[] controlBuf)
          constructor with 3 keys to setup.
 
Method Summary
 boolean decrypt(byte[] inputBuf, byte[] outputBuf, int len)
          decrypt buffer.
 boolean encrypt(byte[] inputBuf, byte[] outputBuf)
          encrypt buffer. each input byte becomes two bytes, by adding one byte from fillKey.
protected  void finalize()
          carefull clear of buffers in ZAC3 object
 byte[] getControlBuf()
           
 byte[] getControlShaDigest()
           
 byte[] getCryptBuf()
           
 byte[] getCryptShaDigest()
           
 byte[] getFillBuf()
           
 byte[] getFillShaDigest()
           
 boolean initDecrypt(byte[] cryptShaDigest, byte[] fillShaDigest, byte[] controlShaDigest)
          init keys, using hash on prng of each key.
 void initEncrypt()
          init keys, using hash on prng of each key.
static void main(String[] args)
          Run ZAC3 Algorithm.
 void reset()
          Reset "state" of prng by setting keys to bufkeys and skip each key.
 void setup(byte[] cryptBuf, byte[] fillBuf, byte[] controlBuf)
          setup 3 keys to ZAC3 algorithm.
 
Methods inherited from class com.fasteasytrade.JRandTest.Algo.Cipher
compareBytes
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cryptKey

RC4Key cryptKey
encrypt and decrypt data using cryptKey


fillKey

RC4Key fillKey
add one byte for each encrypt data byte


controlKey

RC4Key controlKey
controlKey gives one byte to enable swap of encrypt data and fill data (from fillKey)


cryptShaDigest

byte[] cryptShaDigest
cryptKey is processed cryptSkip times, and prng data is hashed using sha1. Digest "message" is kept in cryptShaDigest.


fillShaDigest

byte[] fillShaDigest
fillKey is processed fillSkip times, and prng data is hashed using sha1. Digest "message" is kept in fillShaDigest.


controlShaDigest

byte[] controlShaDigest
controlKey is processed controlSkip times, and prng data is hashed using sha1. Digest "message" is kept in controlShaDigest.


cryptBuf

byte[] cryptBuf
Original key bytes buffer for cryptKey


fillBuf

byte[] fillBuf
Original key bytes buffer for fillKey


controlBuf

byte[] controlBuf
Original key bytes buffer for controlKey


cryptSkip

int cryptSkip
random number we skip cryptKey before start using it.


fillSkip

int fillSkip
random number we skip fillKey before start using it.


controlSkip

int controlSkip
random number we skip controlKey before start using it.


garbage

boolean garbage
Boolean flag if we ever find an incorrect fill byte in data.

Only used while decryption.

If true, we fill (silently) all output data with random data from rnd variable.


rnd

Random rnd
Use java random class to
  1. fill new keys
  2. put some garbage bytes in output buffer (if decryption fails).

Constructor Detail

ZAC3

public ZAC3()
default constructor - does nothing.


ZAC3

public ZAC3(byte[] cryptBuf,
            byte[] fillBuf,
            byte[] controlBuf)
constructor with 3 keys to setup.

Parameters:
cryptBuf - kept in obejct cryptBuf and init cryptKey
fillBuf - kept in obejct fillBuf and init fillKey
controlBuf - kept in obejct controlBuf and init controlKey
Method Detail

setup

public void setup(byte[] cryptBuf,
                  byte[] fillBuf,
                  byte[] controlBuf)
setup 3 keys to ZAC3 algorithm. If one of the input buffers is null, we fill it with 256 random bytes.

keep keys and skip data in object so initEncrypt() and reset() can run properly.

Parameters:
cryptBuf - 256 bytes to cryptKey
fillBuf - 256 bytes to fillKey
controlBuf - 256 bytes to controlKey

initEncrypt

public void initEncrypt()
init keys, using hash on prng of each key. Digest message generated from key xyz is kept as xyzShaDigest variable.

initEncrypt method is run before encryption starts.


encrypt

public boolean encrypt(byte[] inputBuf,
                       byte[] outputBuf)
encrypt buffer. each input byte becomes two bytes, by adding one byte from fillKey. The byte from controlKey is controlling the order of the two result bytes:

if control_byte is even
then output order is concat(encrypted_data_byte, fill_byte),
else output order is concat(fill_byte, encrypted_data_byte)

caller gets back a clear input buffer (full of 0x00).

Parameters:
inputBuf - input bytes buffer, length n
outputBuf - output encrypted bytes buffer, must be length 2n
Returns:
returns FALSE if inputs are null or lengths are not correct. Else, encrypt data and returns TRUE.

initDecrypt

public boolean initDecrypt(byte[] cryptShaDigest,
                           byte[] fillShaDigest,
                           byte[] controlShaDigest)
init keys, using hash on prng of each key. Digest message generated from key xyz is compared with xyzShaDigest variable.

initDecrypt method is run before decryption starts.

Returns:
true if all skip counters are found, and state is ready to decrypt cipher data. Else, false.

decrypt

public boolean decrypt(byte[] inputBuf,
                       byte[] outputBuf,
                       int len)
decrypt buffer. Two input bytes become one byte. Determin where is the ciphred data and the fill byte using rhe control byte.

if control_byte is even
then input order is concat(encrypted_data_byte, fill_byte),
else input order is concat(fill_byte, encrypted_data_byte)

If filler byte is not in a correct place, we flag garbage boolean value, and hence forth, fill *any* output buffer with random data without decryption. Note taht no alert is given.

Parameters:
inputBuf - input ciphered bytes buffer, length 2n
outputBuf - output data bytes buffer, must be length n
len - intput buffer length
Returns:
returns FALSE if inputs are null or lengths are not correct. Else, encrypt data and returns TRUE.

reset

public void reset()
Reset "state" of prng by setting keys to bufkeys and skip each key.


getControlShaDigest

public byte[] getControlShaDigest()
Returns:
Returns the controlShaDigest.

getCryptShaDigest

public byte[] getCryptShaDigest()
Returns:
Returns the cryptShaDigest.

getFillShaDigest

public byte[] getFillShaDigest()
Returns:
Returns the fillShaDigest.

getControlBuf

public byte[] getControlBuf()
Returns:
Returns the controlBuf.

getCryptBuf

public byte[] getCryptBuf()
Returns:
Returns the cryptBuf.

getFillBuf

public byte[] getFillBuf()
Returns:
Returns the fillBuf.

finalize

protected void finalize()
                 throws Throwable
carefull clear of buffers in ZAC3 object

Throws:
Throwable

main

public static void main(String[] args)
                 throws Exception
Run ZAC3 Algorithm. Copyright (c) 2005 Zur Aougav

Command line syntax:

java ZAC3 [options] [ops] [inputfilename outputfilename]

options:
-crf <cryptKeyFile.key>       default: cryptKeyFile.key
-fif <fillKeyFile.key>        default: fillKeyFile.key
-cof <controlKeyFile.key>     default: controlKeyFile.key

ops:
-h       print this help message
-e       encrypt inputfilename into outputfilename
-enc     encrypt inputfilename into outputfilename
-d       decrypt inputfilename into outputfilename
-dec     decrypt inputfilename into outputfilename
-compare     compare two files - inputfilename and outputfilename
-gen     generate random keys and write them to key files

Parameters:
args - keys, operations, input and output filenames
Throws:
Exception - missing operands or IOExceptions

Java Randomness Test Suite

JRandTest at SourceForge

JRandTest at SourceForge
Copyright © 2005 Zur Aougav. All Rights Reserved.