Block Cipher in Java

From: Sigrid Schefer (sigridschefer_at_gmx.at)
Date: 05/13/04


Date: 13 May 2004 09:45:02 -0700

Well, I have a problem with my java-program, which should cipher any
input-file by means of the Block Cipher method. However, it doesn't
cipher the whole input-file and I simply can't understand that.
Many thanx for your help!

Here's the code:

import java.io.*;

public class Cipher {
   public static void main(String[] args) {
      String key = args[0];
      String fileName = args[1];
      codierungXor(key, fileName);
   }
   public static void codierungXor(String key, String fileName) {
      try {
         FileInputStream reader = new FileInputStream(fileName);
         byte[] keyBuffer = key.getBytes();
         int len = keyBuffer.length;
         byte[] messageBuffer = new byte[len];
         int bytesRead;
         
         while((bytesRead = reader.read(messageBuffer)) != -1) {
            for(int i = 0; i < bytesRead; i++) {
               System.out.write(messageBuffer[i] ^ keyBuffer[i]);
            }
         }
      }
      catch (Exception e) {
         System.out.println("Fehler beim Lesen: " + e);
      }
   }
}