sam.cipher
Class Cipher

java.lang.Object
  |
  +--sam.cipher.Cipher
Direct Known Subclasses:
Deal, DES, Frog, Loki, Mars, RC6, Rijndael, Serpent, Twofish, WarFactory.War, XOR

public abstract class Cipher
extends java.lang.Object

General purpose ciphering class.


Nested Class Summary
 class Cipher.ByteArrayFilter
          byte[] array filter implementation based on this cipher.
static class Cipher.Error
          Exception class for ciphering functions.
 
Field Summary
static boolean DECRYPT
          Decryption specifier for instantiation
static sam.cipher.Cipher EMPTY
          Empty cipher implementation, does nothing.
static boolean ENCRYPT
          Encryption specifier for instantiation
static boolean INVERSE
          Decryption specifier for instantiation
protected  sam.cipher.Key key
          Actual key assigned to this cipher
static sam.cipher.Registry REGISTRY
          Central registry of all known cipher implementations.
 
Constructor Summary
protected Cipher(sam.cipher.Key key)
          Builds a new Cipher
 
Method Summary
protected  void cipher(byte[] data)
          Deprecated.  
protected  void cipher(byte[] data, int offset, sam.cipher.Key key)
          Either encrypts or decrypts data according to the key direction.
protected abstract  void decrypt(byte[] data, int offset, sam.cipher.Key key)
          Called in case of decrypting key
protected abstract  void encrypt(byte[] data, int offset, sam.cipher.Key key)
          Called in case of encrypting key
protected static void fill(byte[] data, int start, int end)
          Called whenever the whole buffer can not be filled up with fresh data.
protected  int getPreferredSize(int size)
          Specifiy preferred buffer size.
static void main(java.lang.String[] args)
          Tests all registered ciphers implementations.
protected static void store(byte[] data, int byteOffset, int a)
          Stores one int into the first 4 cells starting at byteOffset of a byte[] array.
protected static void store(byte[] data, int byteOffset, long a)
          Stores one int into the first 4 cells starting at byteOffset of a byte[] array.
protected static void storeReversed(byte[] data, int byteOffset, long a)
          Stores one long into the first 4 cells starting at byteOffset of a byte[] array.
static boolean streamTest(java.lang.String cipherName, int length, java.lang.String password)
          Tests a cipher through the complete stream-based mechanism.
protected  boolean test()
          Tests this cipher's implementation.
protected static int unstoreInt(byte[] data, int byteOffset)
          Rebuilds one int from specified byte[] array starting at byteOffset.
protected static long unstoreLong(byte[] data, int byteOffset)
          Rebuilds one long from specified byte[] array starting at byteOffset.
protected static long unstoreReversedLong(byte[] data, int byteOffset)
          Rebuilds one long from specified byte[] array starting at byteOffset.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENCRYPT

public static final boolean ENCRYPT
Encryption specifier for instantiation

See Also:
Constant Field Values

DECRYPT

public static final boolean DECRYPT
Decryption specifier for instantiation

See Also:
Constant Field Values

INVERSE

public static final boolean INVERSE
Decryption specifier for instantiation

See Also:
Constant Field Values

REGISTRY

public static final sam.cipher.Registry REGISTRY
Central registry of all known cipher implementations.


key

protected final transient sam.cipher.Key key
Actual key assigned to this cipher


EMPTY

public static final sam.cipher.Cipher EMPTY
Empty cipher implementation, does nothing. This is usefull for debugging purpose.

Constructor Detail

Cipher

protected Cipher(sam.cipher.Key key)
Builds a new Cipher

See Also:
Key
Method Detail

cipher

protected void cipher(byte[] data)
Deprecated.  

Main ciphering code.

See Also:
cipher

cipher

protected void cipher(byte[] data,
                      int offset,
                      sam.cipher.Key key)
Either encrypts or decrypts data according to the key direction.

See Also:
Cipher.Key.direction, Cipher.encrypt, Cipher.decrypt

encrypt

protected abstract void encrypt(byte[] data,
                                int offset,
                                sam.cipher.Key key)
Called in case of encrypting key


decrypt

protected abstract void decrypt(byte[] data,
                                int offset,
                                sam.cipher.Key key)
Called in case of decrypting key


test

protected boolean test()
Tests this cipher's implementation.

WarningNote that a cipher might be working even if this return false, in particular in case of block/key size inconsistencies or inner Key class. However the cipher is guaranteed to work properly if this returns true

Returns:
true if the cipher is working properly with current key.

streamTest

public static boolean streamTest(java.lang.String cipherName,
                                 int length,
                                 java.lang.String password)
                          throws java.io.IOException,
                                 java.lang.InterruptedException
Tests a cipher through the complete stream-based mechanism.

Parameters:
cipherName - the supposedly registered cipher to be tested
length - the sample data length
password - the password to be used
Returns:
wether the test was successfull or not
java.io.IOException
java.lang.InterruptedException

main

public static void main(java.lang.String[] args)
Tests all registered ciphers implementations. Syntax is java sam.cipher.Cipher [password] [cipher1 [cipher2 [...]]] If none specified then all registered ciphers are used.


fill

protected static void fill(byte[] data,
                           int start,
                           int end)
Called whenever the whole buffer can not be filled up with fresh data.

To be overriden as required.

This implementation fills up remaining bytes with random data.


getPreferredSize

protected int getPreferredSize(int size)
Specifiy preferred buffer size.

May return a fixed value, ignoring suggested size (e.g. return 16;).

By default, this implementation simply returns its argument unchanged.

Parameters:
size - proposed buffer size (in bytes)
Returns:
adjusted buffer size (in bytes)

store

protected static void store(byte[] data,
                            int byteOffset,
                            int a)
Stores one int into the first 4 cells starting at byteOffset of a byte[] array.


store

protected static void store(byte[] data,
                            int byteOffset,
                            long a)
Stores one int into the first 4 cells starting at byteOffset of a byte[] array.


unstoreInt

protected static int unstoreInt(byte[] data,
                                int byteOffset)
Rebuilds one int from specified byte[] array starting at byteOffset.


unstoreLong

protected static long unstoreLong(byte[] data,
                                  int byteOffset)
Rebuilds one long from specified byte[] array starting at byteOffset.


unstoreReversedLong

protected static long unstoreReversedLong(byte[] data,
                                          int byteOffset)
Rebuilds one long from specified byte[] array starting at byteOffset.


storeReversed

protected static void storeReversed(byte[] data,
                                    int byteOffset,
                                    long a)
Stores one long into the first 4 cells starting at byteOffset of a byte[] array.