cz.cuni.jagrlib.obscure
Class Enigma

java.lang.Object
  extended by cz.cuni.jagrlib.obscure.Enigma

public class Enigma
extends Object

Text file scrambling algorithm. Preserves original line splitting, so VCS systems won't get overburdened. Able to skip over initial Javadoc comment.

In honour of Bletchley Park codebreakers..

Since:
0.24
Version:
0.26 $Rev: 713 $ $Date: 2008-02-11 18:57:32 +0100 (po, 11 II 2008) $ $Author: pepca $
See Also:
Enigma.java

Field Summary
protected  int CHAR_MASK
           
protected  long INIT_SEED
           
protected  int[] inv
          Inverted "perm".
protected  String key
          Universal key = arbitrary string (only low-order bits of UNICODE are used).
protected  int MUL1
           
protected  int[] perm
          Pseudo-random permutation used for seed iteration.
protected  int PRIME1
           
protected  long seed
          Encryption/decryption seed.
protected  int SUM0
           
protected static String table6
          Random permutation of "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-+".
 
Constructor Summary
Enigma()
          Have to enter encryption key later (setKey(java.lang.String)).
Enigma(String k)
          Should enter encryption key here or call setKey(java.lang.String) later.
 
Method Summary
protected  void advance(int val)
          Advances the seed over one 16-bit integer value.
protected  void advanceLine(String line)
          Advances the seed over the line of text.
protected  void buildPermutation(int prime1, int mul1, int[] forward, int[] inverse)
          Builds key-based (scrambled) permutation of integers [ 0, forward.length - 1 ].
protected  char decode(int code)
          Decodes one 18-bit code number into 16-bit character, advances the seed..
protected static int decode18(String line, int i)
           
protected static int decode6(char c)
           
 int decodeFile(String inFileName, String outFileName)
          Decodes the whole text file.
 int decodeJavaSource(String inFileName, String outFileName)
          Encodes Java source file with leading Javadoc comment.
 String decodeLine(String[] prefix, int gap, String code)
          Decrypt one line of text.
protected  int encode(char ch)
          Encodes one 16-bit character, returns result in 18 bits, advances the seed..
protected static void encode18(StringBuffer buf, int value)
           
protected static char encode6(int val)
           
 int encodeFile(String inFileName, String outFileName)
          Encodes the whole text file.
 int encodeJavaSource(String inFileName, String outFileName)
          Encodes Java source file with leading Javadoc comment.
 String encodeLine(String[] prefix, int gap, String line)
          Encrypt one line of text.
protected  void init()
          Initializes the encoding/decoding sequence.
static void main(String[] args)
          Command-line encryptin/decrypting utility.
 void setKey(String k)
          Empty keys are replaced by default (11-letter) key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PRIME1

protected final int PRIME1
See Also:
Constant Field Values

MUL1

protected final int MUL1
See Also:
Constant Field Values

CHAR_MASK

protected final int CHAR_MASK
See Also:
Constant Field Values

INIT_SEED

protected final long INIT_SEED
See Also:
Constant Field Values

SUM0

protected final int SUM0
See Also:
Constant Field Values

key

protected String key
Universal key = arbitrary string (only low-order bits of UNICODE are used).


perm

protected int[] perm
Pseudo-random permutation used for seed iteration.


inv

protected int[] inv
Inverted "perm".


seed

protected long seed
Encryption/decryption seed. Only one instance of coding/decoding should run at the same time..


table6

protected static final String table6
Random permutation of "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-+".

See Also:
Constant Field Values
Constructor Detail

Enigma

public Enigma()
Have to enter encryption key later (setKey(java.lang.String)).


Enigma

public Enigma(String k)
Should enter encryption key here or call setKey(java.lang.String) later.

Method Detail

setKey

public void setKey(String k)
Empty keys are replaced by default (11-letter) key.


buildPermutation

protected void buildPermutation(int prime1,
                                int mul1,
                                int[] forward,
                                int[] inverse)
Builds key-based (scrambled) permutation of integers [ 0, forward.length - 1 ].


init

protected void init()
Initializes the encoding/decoding sequence.


encode

protected int encode(char ch)
Encodes one 16-bit character, returns result in 18 bits, advances the seed..


advance

protected void advance(int val)
Advances the seed over one 16-bit integer value.


decode

protected char decode(int code)
Decodes one 18-bit code number into 16-bit character, advances the seed..


advanceLine

protected void advanceLine(String line)
Advances the seed over the line of text.


encode6

protected static final char encode6(int val)

decode6

protected static final int decode6(char c)

encode18

protected static final void encode18(StringBuffer buf,
                                     int value)

decode18

protected static final int decode18(String line,
                                    int i)

encodeLine

public String encodeLine(String[] prefix,
                         int gap,
                         String line)
Encrypt one line of text. Key, previous nonempty line[s] (together in "prefix"), number of preceding empty lines are used to initialize seed..


decodeLine

public String decodeLine(String[] prefix,
                         int gap,
                         String code)
Decrypt one line of text. Context ("prefix" and "gap") must exactly match the one used in encryption.


encodeFile

public int encodeFile(String inFileName,
                      String outFileName)
               throws IOException
Encodes the whole text file.

Returns:
Number of output lines.
Throws:
IOException
See Also:
FileNotFoundException

decodeFile

public int decodeFile(String inFileName,
                      String outFileName)
               throws IOException
Decodes the whole text file.

Returns:
Number of output lines.
Throws:
IOException
See Also:
FileNotFoundException

encodeJavaSource

public int encodeJavaSource(String inFileName,
                            String outFileName)
                     throws IOException
Encodes Java source file with leading Javadoc comment. Preserves compileability of the source.

Returns:
Number of output lines.
Throws:
IOException
See Also:
FileNotFoundException

decodeJavaSource

public int decodeJavaSource(String inFileName,
                            String outFileName)
                     throws IOException
Encodes Java source file with leading Javadoc comment.

Returns:
Number of output lines.
Throws:
IOException
See Also:
FileNotFoundException

main

public static void main(String[] args)
Command-line encryptin/decrypting utility.

Usage: Enigma {-e|-d|-je|-jd} [-k <key>] <in-file> <out-file>

Must enter one of commands: -e, -d, -je, -jd. Also the "-k" (key) parameter is higly recommended.

-e: regular text-file encryption.
-d: regular text-file decryption (inverse to "-e").
-je: Java source file encryption.
-jd: Java source file decryption (inverse to "-je").