Von C nach Java, Teil 4: Datenkompression und Verschlüsselung

Seite 6: Listing 2

Inhaltsverzeichnis
import java.io.*;
import java.util.*;

public class MiniCrypt {
final static int BUFLEN=1024*1024;

public MiniCrypt(String[] args) throws Exception {
Random r=null;
byte[] buffer=new byte[BUFLEN];

for (String s: args) {
if (r==null) {
int seed=s.hashCode();
r=new Random(seed);
System.out.printf("Password \"%s\" has hashCode:
%d\n", s, seed);
continue;
}
FileInputStream fis=new FileInputStream(s);
FileOutputStream fos=new FileOutputStream(s+".crypt");
int rb;

while ((rb=fis.read(buffer, 0, BUFLEN))>0) {
for (int i=0;i<rb;i++) {
int code=buffer[i] & 0xff;
code ^= ~r.nextInt();
buffer[i] = (byte)(code & 0xff);
}
fos.write(buffer, 0, rb);
}
fis.close();
fos.close();
}
}

public static void main(String[] args) {
if (args.length>1) {
try {
new MiniCrypt(args);
} catch(Exception e) {
System.err.println("Exception caught: "+e.getMessage());
System.exit(1);
}
} else {
System.err.println("usage: java "+MiniCrypt.class.getName()+"
password file ...");
}
}
}