Java Randomness Test Suite

JRandTest at SourceForge

com.fasteasytrade.JRandTest.Algo
Class ZAC5

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

public class ZAC5
extends Cipher

ZAC5 encryption algorithm Copyright (c) 2005 Zur Aougav

Symetric algorithm.

Use 5 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)  int numCyclesOfPermutations
          input and output buffers are permuted numCyclesOfPermutations times
(package private)  byte[] postPermutationBuf
          Original key bytes buffer for postPermutationKey
(package private)  RC4Key postPermutationKey
          postPermutationKey permutes ciphered output buffer after encryption completed
(package private)  byte[] postPermutationShaDigest
          postPermutationKey is processed postPermutationSkip times, and prng data is hashed using sha1.
(package private)  int postPermutationSkip
          random number we skip postPermutationKey before start using it.
(package private)  byte[] prePermutationBuf
          Original key bytes buffer for prePermutationKey
(package private)  RC4Key prePermutationKey
          prePermutationKey permutes input buffer before encryption begins
(package private)  byte[] prePermutationShaDigest
          prePermutationKey is processed prePermutationSkip times, and prng data is hashed using sha1.
(package private)  int prePermutationSkip
          random number we skip prePermutationKey before start using it.
(package private)  Random rnd
          Use java random class to fill new keys put some garbage bytes in output buffer (if decryption fails).
 
Constructor Summary
ZAC5()
          default constructor - does nothing.
ZAC5(byte[] cryptBuf, byte[] fillBuf, byte[] controlBuf, byte[] prePermutationBuf, byte[] postPermutationBuf)
          constructor with 5 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 ZAC5 object
 byte[] getControlBuf()
           
 byte[] getControlShaDigest()
           
 byte[] getCryptBuf()
           
 byte[] getCryptShaDigest()
           
 byte[] getFillBuf()
           
 byte[] getFillShaDigest()
           
 byte[] getPostPermutationShaDigest()
           
 byte[] getPrePermutationShaDigest()
           
 boolean initDecrypt(byte[] cryptShaDigest, byte[] fillShaDigest, byte[] controlShaDigest, byte[] prePermutationShaDigest, byte[] postPermutationShaDigest)
          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 ZAC5 Algorithm.
 void reset()
          Reset "state" of prng by setting keys to bufkeys and skip each key.
 void setup(byte[] cryptBuf, byte[] fillBuf, byte[] controlBuf, byte[] prePermutationBuf, byte[] postPermutationBuf)
          setup 5 keys to ZAC5 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)


prePermutationKey

RC4Key prePermutationKey
prePermutationKey permutes input buffer before encryption begins


postPermutationKey

RC4Key postPermutationKey
postPermutationKey permutes ciphered output buffer after encryption completed


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.


prePermutationShaDigest

byte[] prePermutationShaDigest
prePermutationKey is processed prePermutationSkip times, and prng data is hashed using sha1. Digest "message" is kept in prePermutationShaDigest.


postPermutationShaDigest

byte[] postPermutationShaDigest
postPermutationKey is processed postPermutationSkip times, and prng data is hashed using sha1. Digest "message" is kept in postPermutationShaDigest.


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


prePermutationBuf

byte[] prePermutationBuf
Original key bytes buffer for prePermutationKey


postPermutationBuf

byte[] postPermutationBuf
Original key bytes buffer for postPermutationKey


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.


prePermutationSkip

int prePermutationSkip
random number we skip prePermutationKey before start using it.


postPermutationSkip

int postPermutationSkip
random number we skip postPermutationKey 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).


numCyclesOfPermutations

int numCyclesOfPermutations
input and output buffers are permuted numCyclesOfPermutations times

Constructor Detail

ZAC5

public ZAC5()
default constructor - does nothing.


ZAC5

public ZAC5(byte[] cryptBuf,
            byte[] fillBuf,
            byte[] controlBuf,
            byte[] prePermutationBuf,
            byte[] postPermutationBuf)
constructor with 5 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
prePermutationBuf - kept in obejct prePerbutation and init prePermutationKey
postPermutationBuf - kept in obejct postPerbutation and init postPermutationKey
Method Detail

setup

public void setup(byte[] cryptBuf,
                  byte[] fillBuf,
                  byte[] controlBuf,
                  byte[] prePermutationBuf,
                  byte[] postPermutationBuf)
setup 5 keys to ZAC5 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
prePermutationBuf - 256 bytes to prePermutationKey
postPermutationBuf - 256 bytes to postPermutationKey

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)

input buffer length must be >= 256 bytes.
prePermutation input buffer inplace.
caller gets back a clear input buffer (full of 0x00).

output buffer length must be twice the size/length of input buffer.

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,
                           byte[] prePermutationShaDigest,
                           byte[] postPermutationShaDigest)
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 in correctwe o not alert if filler byte

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.

getPostPermutationShaDigest

public byte[] getPostPermutationShaDigest()
Returns:
Returns the postPermutationShaDigest.

getPrePermutationShaDigest

public byte[] getPrePermutationShaDigest()
Returns:
Returns the prePermutationShaDigest.

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 ZAC5 object

Throws:
Throwable

main

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

Command line syntax:

java ZAC5 [options] [ops] [inputfilename outputfilename]

options:
-crf <cryptKeyFile.key>       default: cryptKeyFile.key
-fif <fillKeyFile.key>        default: fillKeyFile.key
-cof <controlKeyFile.key>     default: controlKeyFile.key
-prf <prePermutationKeyFile.key>     default: prePermutationKeyFile.key
-pof <postPermutationKeyFile.key>     default: postPermutationKeyFile.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.