Von C nach Java

HansWM

Grünschnabel
Habe ein altes Verschlüsselungsprogramm in C ausgegraben
Die nachfolgende Routine soll ein benutzerdefiniertes Passwort auf 256 Byte "verlängern".
Irgendwie gerate ich bei der Umsetzung in Java in eine Endlosschleife.
Wie setze ich das in Java um ?
Denke mal, inkey und outkey sind dann byte[256];

input: inkey - input key
lg - it´s length
outout: outkey - result key

Code:
void make_key (inkey, outkey,lg)
   int inkey[256], outkey[256];
{
   register m,n;
   int off;

   for (m=0; m<256; ++m) outley[m] =0;
   off=inkey[inkey[lg-1]%lg];

   for (n=m=0; n<256; ++n)
   {
       m+=inkey[off++];
       off %= lg;
       while(outkey[++m & 0xff]);
       m &= 0xff;
       outkey[m] = n;
  }
}
 
Wie sieht denn das aus was du bisher in Java hinbekommen hast?

Ich würde halt einfach den C-Code nach Java kopieren und solange ändern bis der Compiler nicht mehr meckert.
 
Ist halt auch die Frage, wie groß das int sein soll...1/2/4 oder 8 Byte?...also byte/short/int/long

(weil das ist auf C ebene nicht immer klar)
 
Code:
// 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
 
Zurück