sam.cipher
Class XOR

java.lang.Object
  |
  +--sam.cipher.Cipher
        |
        +--sam.cipher.XOR

public class XOR
extends Cipher

Basic XOR cipher.

This cipher is inherently unsafe: given its key size n, well-known bytes of unciphered data might be found at position j modulo n for all j less than n with a dramatically high probability. Because the XOR function is its self-inverse the full key can then be obtained for the full data. This is especially true with non-binary files (like Microsoft Word or HTML documents).

Thus it is not intended to be used solely, but rather as a masking step in the ciphering stack. It is especially usefull in top of the huffman compression algorithm.


Nested Class Summary
 
Nested classes inherited from class sam.cipher.Cipher
Cipher.ByteArrayFilter, Cipher.Error
 
Field Summary
 
Fields inherited from class sam.cipher.Cipher
DECRYPT, EMPTY, ENCRYPT, INVERSE, key, REGISTRY
 
Constructor Summary
XOR(sam.cipher.Key key)
           
 
Method Summary
protected  void decrypt(byte[] data, int offset, sam.cipher.Key key)
          Same as encrypt since the XOR function is involutive
protected  void encrypt(byte[] data, int offset, sam.cipher.Key key)
          Cyclic exclusive-or of the bytes of data.
protected  int getPreferredSize(int size)
          Specifiy preferred buffer size.
 
Methods inherited from class sam.cipher.Cipher
cipher, cipher, fill, main, store, store, storeReversed, streamTest, test, unstoreInt, unstoreLong, unstoreReversedLong
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XOR

public XOR(sam.cipher.Key key)
Method Detail

encrypt

protected void encrypt(byte[] data,
                       int offset,
                       sam.cipher.Key key)
Cyclic exclusive-or of the bytes of data.

Specified by:
encrypt in class Cipher

decrypt

protected void decrypt(byte[] data,
                       int offset,
                       sam.cipher.Key key)
Same as encrypt since the XOR function is involutive

Specified by:
decrypt in class Cipher

getPreferredSize

protected int getPreferredSize(int size)
Description copied from class: Cipher
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.

Overrides:
getPreferredSize in class Cipher
Parameters:
size - proposed buffer size (in bytes)
Returns:
adjusted buffer size (in bytes)