// Aus UNIXMagazin Ausgabe 7/Juli 1992
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.BufferedReader;
// import java.io.BufferedInputStream;
// import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.DataInputStream;
import java.io.DataInput;
import java.io.EOFException;
import java.io.IOException;
// import java.io.FileReader; // Character Files
import java.io.FileInputStream; // Raw Bytes
import java.io.FileOutputStream;
public class fcrypt
{
byte[] inbuf = new byte[256];
byte[] outbuf = new byte[256];
byte[] groups = new byte[16];
byte[] inkey = new byte[256];
byte[] outkey = new byte[256];
String key = "h(eXrDVi!3uz9&2P3g#h2;y"; // 32 Zeichen
public fcrypt ()
{
System.out.println("Hello !");
outkey = make_key ();
// readBuffer ();
// encode ();
}
private byte[] make_key ()
{
int m;
byte n;
byte off;
int length = key.length();
for (int x = 0; x<length; ++x) inkey[x] = (byte) key.charAt(x);
System.out.println("key = "+key);
System.out.println("inkey = "+inkey);
for (m = 0; m < 256; ++m)
{
outkey [m] = 0;
}
System.out.println("outkey = "+outkey);
// System.out.println(inkey[length-1]);
// System.out.println(inkey[length-1]%length);
off = (inkey[inkey[length-1]%length]);
System.out.println("off = "+off);
m = 0;
for (n=0; n < 256; ++n)
{
m = m + inkey[off++];
// System.out.println("m = "+m);
off = (byte) (off%length);
// System.out.println("off = "+off);
while (outkey[++m & 0xff] > 0)
{
m = m & 0xff;
// System.out.println("n = "+n);
// System.out.println("m = "+m);
outkey[m]=n;
}
}
System.out.println("outkey = "+outkey);
return outkey;
}
private void readBuffer ()
{
int len, p;
FileInputStream in = null;
try
{
in = new FileInputStream("CrypTool-de.txt");
len = in.read (inbuf,0,256);
// Auffuellen mit 0
if (len<256)
{
for (p=len; p<256; ++p) inbuf[p] = 0;
}
} catch (IOException e)
{
System.out.println(e);
}
System.out.println("Input Buffer = "+inbuf);
}
private void write_buff ()
{
FileOutputStream out;
try
{
out = new FileOutputStream ("Ausgabe.txt",true);
out.write (outbuf);
} catch (IOException e)
{
System.out.println(e);
}
}
private void next_groups (boolean codeflag)
{
int n;
byte[] help = new byte[256];
byte[] key = new byte[256];
byte xor = 0;
byte gh;
for (n=0; n<256; ++n)
{
gh = codeflag ? outbuf[n] : inbuf[n];
xor = (byte) ((key[xor]+gh) & (byte)0xff);
}
for (n=0; n<256; ++n) help[n] = groups[key[n^xor]];
for (n=0; n<256; ++n) groups[n] = help[n];
}
private void encode ()
{
int n, sum, k;
for (n=0; n<256; ++n)
{
int a = inbuf[n]+groups[n];
outbuf[n] = (byte) (a & 0xff);
}
for (k=0; k<256; k+=16)
{
sum = 0;
for (n=k; n<k+16; ++n)
{
sum = sum + outbuf[groups[n]];
}
for (n=k; n<k+16; ++n)
{
int x = sum - outbuf[groups[n]];
outbuf[groups[n]] = (byte) (x & 0xff);
}
}
for (k=0; k<16; ++k)
{
sum = 0;
for (n=k; n<256; n+=16)
{
sum = sum + outbuf[groups[n]];
}
for (n=k; n<256; n+=16)
{
int y = sum - outbuf[groups[n]];
outbuf[groups[n]]= (byte) ((y & 0xff)^groups[groups[n]]);
}
}
} // encode
private void decode ()
{
byte[] mod = new byte[256];
int n, k;
int sum;
boolean flag = true;
/* compute field for fast solving the diophantic equation
s = 15*sum(mod 256) (s given, sum wanted) */
if (flag)
{
flag = false;
sum = 0;
for (n=0; n<256; ++n)
{
mod[sum & 0xff] = (byte) n;
sum = sum + 15;
}
}
for (n=0; n<256; ++n) outbuf[n] = (byte) (inbuf[n]^groups[n]);
for (k=0; k<16; ++k)
{
sum=0;
for (n=k; n<256; n+=16)
{
sum += outbuf[groups[n]];
}
sum = mod[sum & 0xff] + 256;
for (n=k; n<256; n+=16)
{
int x = sum-outbuf[groups[n]];
outbuf[groups[n]] = (byte) (x & 0xff);
}
}
for (k=0; k<256; k+=16)
{
sum = 0;
for (n=k; n<k+16; ++n)
{
sum += outbuf[groups[n]];
}
sum = mod[sum & 0xff] + 512;
for (n=k; n<k+16; ++n)
{
int y = sum-outbuf[groups[n]];
int z = y-groups[groups[n]];
outbuf[groups[n]] = (byte) (z & 0xff);
}
}
}// decode
}// fcrypt