All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object | +----cryptix.security.BlockCipher
streamCipher
).
Usage of Extended Classes
General usage of extended classes is to construct the object, providing
the secret key and any other details required.
The key is generally fixed for any given object.
Then, methods encrypt
and decrypt
are called with data for processing, always using the original key:
String plain = "Et tu, Brute?"; byte plainText[] = new byte[cryptor.blockLength]; plain.getBytes(0, cryptor.blockLength, plainText, 0);In general, method
byte cipherText[] = new byte[cryptor.blockLength]; byte decrypText[] = new byte[cryptor.blockLength];
Caeser cryptor = new Caeser( 3 ); // 'a' becomes 'd', etc cryptor.encrypt( plainText, cipherText ); cryptor.decrypt( cipherText, decrypText );
String decryp = new String( decrypText, 0 ); String cipher = new String( cipherText, 0 );
if ( decryp.equals( plain ) && !cipher.equals( plain ) ) out.println( "Caeser is good for protecting secrets of state" ); else out.println( "Caeser has failed to keep secrets" );
encrypt
takes plaintext
and returns ciphertext,
whilst method decrypt
takes ciphertext
and returns plaintext.
Adding New Ciphers
Extended classes should provide constructors that initialise the key and any other details:
public final class Caeser extends BlockCipher { private static final String LIBRARY_NAME = "caeser"; private int rotate = 3; // 3 is caeser, 13 is "rot13" // public Caeser() { } // default behaviourWhilst Caeser ciphers didn't deserve a real key, the amount of rotation for each character is almost as good. Note that the 'key' is fixed for the object; this is expected behaviour for extended classes, but not mandated.
public int blockLength() { return 8; }
/** * Create a Caeser object with a different rotation. * @param rotate number of positions to the right to adjust * / public Caeser( int rotate ) { this.rotate = rotate } ....
Extended classes should provide methods blockEncrypt
and blockDecrypt
that operate on the passed data.
The arrays in
and out
may be the same array.
Methods encrypt
and decrypt
do not need
to be provided as they call the former from within this super class:
protected void blockEncrypt( byte in[], int in_offset, byte out[], int out_offset ) { for (int i = 0; i < rotate; i++) { out[out_offset + i] = in[in_offset + i] + rotate; if ( out[out_offset + i] > (int)'Z' ) // modulo 26 out[out_offset + i] -= 26; // only works on [A-Z] } }Extended classes should document the algorithm, constructors, any special calls and any deviations from standard behaviour. Users can refer to this superclass for standard behavior.
/** * Caeser is a substitution cipher recommended for Despots and jokes. * Warning: foreign characters will be thrown to the lions. * / public void Caeser ...
References
See also: Shakespeare, W., Julius Caeser, The Globe, and Schneier, B., Applied Cryptography, Wiley, 1996, 2nd Ed.
public BlockCipher()
public final void encrypt(byte text[])
text
is encrypted
and written back as ciphertext.
The length of text
must be the
block length as returned by blockLength
.
public final void decrypt(byte text[])
text
is decrypted
and written back as plaintext.
The length of text
must be the
block length as returned by blockLength
.
public final void encrypt(byte in[], byte out[])
in
is encrypted and the ciphertext
is written into out
.
Note that in
and out
can be the same array.
The length of both in
and out
must be the
block length as returned by blockLength
.
public final void decrypt(byte in[], byte out[])
in
is decrypted and
the plaintext is written into out
.
Note that in
and out
can be the same array.
The length of both in
and out
must be the
block length as returned by blockLength
.
public final void encrypt(byte in[], int in_offset, byte out[], int out_offset)
in
is encrypted
from in_offset
to (in_offset + blockLength - 1)
and the ciphertext
is written into out
from out_offset
to (out_offset + blockLength - 1)
Note that there must be at least blockLength bytes left in each
array at the supplied offsets.
in
out
public final void decrypt(byte in[], int in_offset, byte out[], int out_offset)
in
is decrypted
from in_offset
to (in_offset + blockLength - 1)
and the plaintext
is written into out
from out_offset
to (out_offset + blockLength - 1)
Note that there must be at least blockLength bytes left in each
array at the supplied offsets.
in
out
protected abstract void blockEncrypt(byte in[], int in_offset, byte out[], int out_offset)
in
is encrypted
from in_offset
to (in_offset + blockLength - 1)
and the ciphertext
is written into out
from out_offset
to (out_offset + blockLength - 1)
Note that there will be at least blockLength bytes left in each
array at the supplied offsets, this is checked in the superclass.
The in
and out
buffers can be the same.
in
out
protected abstract void blockDecrypt(byte in[], int in_offset, byte out[], int out_offset)
in
is decrypted
from in_offset
to (in_offset + blockLength - 1)
and the plaintext
is written into out
from out_offset
to (out_offset + blockLength - 1)
Note that there will be at least blockLength bytes left in each
array at the supplied offsets, this is checked in the superclass.
The in
and out
buffers can be the same.
in
out
public abstract int blockLength()
public abstract int keyLength()
All Packages Class Hierarchy This Package Previous Next Index