You are here:

Java/Java program not overwriting binary file on 2nd run!

Advertisement


Question
Good evening,

A group and myself are working on a Java coding project in which the object is this: It is a program that uses simplified DES along with cipher block chaining to encrypt and decrypt a binary file. THe input includes a 10 bit key (0s and 1s) and an 8 bit "vector" (also 0s and 1s). THe program takes in a binary plaintext file and either "encrypts" or "decrypts" according to the entered keys following S-DES.
The program itself is calculating everything 100% correctly, and runs perfectly the first time. However if the program is rerun, The output binary file is NOT being overwriten, hence the result will reflect what was run the first time.
I have over the code, as well as our group, and we cannot find the problem. I hope that you might be able to figure out what the problem may be as it has been quite frustrating!

TO run, program takes arguments in the following format:
java mycipher -m mode -k 10bit_initial_key -i 8bit_initial_vector -p plaintext_file -c ciphertext_file

Example:
java mycipher -m ecrypt-k 0111111101 -i 10101010 -p file1 -c file2

with "file1" containing the bits "00000001 00010011" produces output "11110100 00001011"

The actual code is below (sorry for the length):


import java.io.*;

public class mycipher {//begin class mycipher
  
         //static permutation and function varibles
  public static int[] P4={2, 4, 3, 1};
       public static int[] P8={6, 3, 7, 4, 8, 5, 10, 9};
       public static int[] P10={3, 5, 2, 7, 4, 10, 1, 9, 8, 6};
       public static int[] IP={2, 6, 3, 1, 4, 8, 5, 7};
  public static int[] revIP={4, 1, 3, 5, 7, 2, 8, 6};   
  public static int[] E_P={4, 1, 2, 3, 2, 3, 4, 1};   
  public static int[][] sBox0={{1, 0, 3, 2},{3, 2, 1, 0},{0, 2, 1, 3},{3, 1, 3, 2}};  //mult dem array sBox1
  public static int[][] sBox1={{0, 1, 2, 3},{2, 0, 1, 3},{3, 0, 1, 0}, {2, 1, 0, 3}};  //mult dem array sBox2
       
       public mycipher() {
       }
       
      public static void main(String [] args)    //begin main
       {          
         String inputFile=null, outputFile;       //create strings needed for Input and output data
         
         mycipher ciph = new mycipher();  //ciph object
         
         try {//try catch for arguments
         if(args[1].equals("encrypt"))   
         {
         inputFile = "file1";
         outputFile = "file2";
         System.out.println("encrypt");
         }
         else
         {
         outputFile = "file1";
         inputFile = "file2";
         System.out.println("decrypt");
         }
         }catch (Exception ex) {ex.printStackTrace();}
         
         
         char [] kt = args[5].toCharArray();   //create keys
         int [] initialVector = new int [kt.length];
         for(int i = 0; i<kt.length; i++)
         {
         initialVector[i] = Integer.parseInt(String.valueOf(kt[i]));
         }
         kt = args[3].toCharArray();
         int [] initialKey = new int[kt.length];
         for(int i = 0; i<kt.length; i++)
         {
         initialKey[i] = Integer.parseInt(String.valueOf(kt[i]));
         }
         //get the appropriate keys and do conversions
         System.out.println("k1= "+ciph.outVector(ciph.key1(initialKey)));  
         System.out.println("k2= "+ciph.outVector(ciph.key2(initialKey)));
         int [] numbers = ciph.readFile(inputFile);
         String plainText = "plaintext = ";
         String cipherText = "ciphertext = ";
         int [] out = new int[numbers.length];  
         
         try {//try catch for arguments
         
         if(args[1].equals("encrypt")){      //do encryption
         for(int i = 0; i<numbers.length; i++)
         {
         int [] vector = new int [8];
         vector = ciph.decToBin(numbers[i], vector);
         for(int j = 0; j<vector.length; j++)
         {
         plainText += vector[j];
         vector[j] = ciph.XOR(vector[j], initialVector[j]);
         }
         plainText +=" ";
         vector = ciph.encryptor(vector, initialKey);
         for(int j = 0; j<vector.length; j++)
         {
         initialVector[j] = vector[j];
         cipherText += vector[j];
         }
         out[i] = ciph.binTodec(vector);
         cipherText += " ";
         }
         System.out.println(plainText);
         System.out.println(cipherText);
         }
         else
         {
         for(int i = 0; i<numbers.length; i++)
         {
         int [] vector = new int [8];
         int [] tempInit = vector;
         if(numbers[i]<0){ numbers[i]+=256;}
         vector = ciph.decToBin(numbers[i], vector);
         for(int j = 0; j<vector.length; j++)
         {
         cipherText += vector[j];          
         }
         cipherText +=" ";
         vector = ciph.decryptor(vector, initialKey);
         for(int j = 0; j<vector.length; j++)
         {
         vector[j] = ciph.XOR(vector[j], initialVector[j]);
         initialVector[j] = tempInit[j];
         plainText += vector[j];          
         }
         out[i] = ciph.binTodec(vector);
         plainText += " ";
         }
         System.out.println(cipherText);
         System.out.println(plainText);          
         }
         }catch (Exception ex) {ex.printStackTrace();}
         
       } //end main
        
        
       /**Functions and methods:**/
        
       public int [] encryptor(int [] vector, int [] key)
        {
         vector = ipBlock(vector, IP);
         vector = FK(vector, key1(key));
         vector = SW(vector);
         vector = FK(vector, key2(key));
         vector = ipBlock(vector, revIP);
         return vector;
        }
        
        public  int [] decryptor(int [] vector, int [] key)
        {
         vector = ipBlock(vector, IP);
         vector = FK(vector, key2(key));
         vector = SW(vector);
         vector = FK(vector, key1(key));
         vector = ipBlock(vector, revIP);
         return vector;
      
        }
        
       public int [] ipBlock(int [] vector, int [] ipVector)
       {
         int [] rVector = new int[vector.length];
         for(int i = 0; i<vector.length; i++)
         {
         rVector[i] = vector[ipVector[i]-1];
         }
         return rVector;
       }
       
       public int [] FK(int [] vector, int [] Key)
       {
         int [] vect41 = {vector[0], vector[1], vector[2], vector[3]};
         int [] vect42 = {vector[4], vector[5], vector[6], vector[7]};          
         int [] vect422 = {vector[4], vector[5], vector[6], vector[7]};          
         int [] vector2 = epBlock(vect42);          
         for(int i = 0; i<Key.length; i++)
         {
         vector2[i] = XOR(vector2[i], Key[i]);          
         }          
         int [] vector21 = {vector2[0], vector2[1], vector2[2], vector2[3]};
         int [] vector22 = {vector2[4], vector2[5], vector2[6], vector2[7]};
         int [] vectSBOX0 = sBlock(vector21, sBox0);
         int [] vectSBOX1 = sBlock(vector22, sBox1);
         for(int i = 0, j = vectSBOX0.length; i<vectSBOX0.length; i++, j++)
         {
         vect42[i] = vectSBOX0[i];
         vect42[j] = vectSBOX1[i];
         }          
         vect42 = P(vect42, P4);          
         for(int i = 0; i<vect42.length; i++)
         {
         vect41[i] = XOR(vect41[i], vect42[i]);
         }          
         for(int i = 0, j = vect41.length; i<vect41.length; i++, j++)
         {
         vector[i] = vect41[i];
         vector[j] = vect422[i];
         }          
         return vector;
       }
       
       public int [] epBlock(int [] vector4)
       {
         int [] rVector = new int [vector4.length*2];
         for(int i = 0; i<rVector.length; i++)
         {
         rVector[i] = vector4[E_P[i]-1];
         }
         return rVector;
       }
       
       public int XOR(int x1, int x2)//XOR formula
       {
         if(x1==0 && x2==0)
         {
         return 0;
         }else
         {
         if(x1==1 && x2==1)
         {
         return 0;
         }else
         {
         return 1;
         }
         }
       }
       
       public int [] sBlock(int [] vector4, int [][] S)
       {
         int [] rezVector = new int [vector4.length/2];
         int [] temp1Vec = {vector4[0],vector4[3]};
         int [] temp2Vec = {vector4[1],vector4[2]};
         int kt = S[binTodec(temp1Vec)][binTodec(temp2Vec)];
         rezVector = decToBin(kt, rezVector);
         return rezVector;          
       }
       
       public int binTodec(int [] vector)
       {
         int rez = 0;
         for(int i = vector.length-1, j = 0; i>=0; i--, j++)
         {
         rez += Math.pow(2, j)*vector[i];
         }
         return rez;
       }
       
       public int [] decToBin(int number, int [] rVector)
       {
         int k = 0;
         do {
         rVector[k] = number % 2;
         number = number / 2;
         k++;
      } while (number != 0);
      for(int i = 0, j = rVector.length-1; i<rVector.length/2; i++, j--){
         int kt = rVector[i];
         rVector[i] = rVector[j];
         rVector[j] = kt;          
         }
         return rVector;
       }
       
       public int [] P(int [] vector, int [] pVector)
       {
         int [] rVector = new int [pVector.length];
         for(int i = 0; i<pVector.length; i++)
         {
         rVector[i] = vector[pVector[i]-1];
         }
         return rVector;
       }
       
       public int [] SW(int [] vector)
       {
         for(int i = 0, j = vector.length/2; i<vector.length/2; i++, j++)
         {
         int kt = vector[i];
         vector[i] = vector[j];
         vector[j] = kt;
         }          
         return vector;
       }
       
       public int [] key1(int [] tenBitkey)
       {
         int [] rVector = new int [8];
         tenBitkey = P(tenBitkey, P10);
         int [] fiveBitkey1 = {tenBitkey[0], tenBitkey[1], tenBitkey[2], tenBitkey[3], tenBitkey[4]};
         int [] fiveBitkey2 = {tenBitkey[5], tenBitkey[6], tenBitkey[7], tenBitkey[8], tenBitkey[9]};
         fiveBitkey1 = lShift(fiveBitkey1, 1);
         fiveBitkey2 = lShift(fiveBitkey2, 1);
         for(int i = 0, j = fiveBitkey1.length; i<fiveBitkey1.length; i++, j++)
         {
         tenBitkey[i] = fiveBitkey1[i];
         tenBitkey[j] = fiveBitkey2[i];
         }
         rVector = P(tenBitkey, P8);
         return rVector;
       }
       
       public int [] key2(int [] tenBitkey)
       {
         int [] rVector = new int [8];
         tenBitkey = P(tenBitkey, P10);
         int [] fiveBitkey1 = {tenBitkey[0], tenBitkey[1], tenBitkey[2], tenBitkey[3], tenBitkey[4]};
         int [] fiveBitkey2 = {tenBitkey[5], tenBitkey[6], tenBitkey[7], tenBitkey[8], tenBitkey[9]};
         fiveBitkey1 = lShift(fiveBitkey1, 1);
         fiveBitkey2 = lShift(fiveBitkey2, 1);
         fiveBitkey1 = lShift(fiveBitkey1, 2);
         fiveBitkey2 = lShift(fiveBitkey2, 2);
         for(int i = 0, j = fiveBitkey1.length; i<fiveBitkey1.length; i++, j++)
         {
         tenBitkey[i] = fiveBitkey1[i];
         tenBitkey[j] = fiveBitkey2[i];
         }
         rVector = P(tenBitkey, P8);
         return rVector;
       }
       
       public int [] lShift(int [] vector, int count)
       {
         while(count != 0)
         {
         int kt = vector[0];
         for(int i = 0; i<vector.length-1; i++)
         {
         vector[i] = vector[i+1];
         }
         vector[vector.length-1] = kt;
         count--;
         }
         return vector;
       }
       

       
       public int [] readFile(String fileName)
       {
         byte[] buffer = null;
         File inputFile=null;
      try{   
         inputFile=new File(fileName);         
     FileInputStream data=new FileInputStream(inputFile);
     buffer=new byte[(int) inputFile.length()];         
     for (int i=0;i<inputFile.length();i++)          
     data.read(buffer,i,1);
     data.close();         
         }catch (Exception ex) {ex.printStackTrace();}          
         int [] rVector = new int [buffer.length];          
         for(int i = 0; i<buffer.length; i++)
         {
         rVector[i] = buffer[i] ;          
         }          
         return rVector;
       }
       
       public void writeFile(String fileName, int [] vector)
       {
         byte [] buffer = null;
         for(int i = 0; i<vector.length; i++)
         {
         buffer[i] = (byte)vector[i];
         }
         File outputFile=null;
         
         try{
         outputFile=new File(fileName);
         
     FileOutputStream data=new FileOutputStream(outputFile);
     for (int i=0;i<buffer.length;i++)
         data.write(buffer,i,1);
     data.close();         
     }catch (Exception ex) {ex.printStackTrace();}      
       }
       
       public String outVector(int [] vector)
       {
         String r = "";
         for(int i = 0; i<vector.length; i++)
         {
         r += vector[i];
         }
         return r;
       }    
}//end class mycipher


Again any help would be appreciated. If you need any other additional information, please let me know.
Thank you in advance,

Answer
The way your code currently is written, it should not write anything at all, since:
1. It doesn't actually call the writeFile method.
2. Even if it did, the writeFile method doesn't initialize the buffer array and will result in a NullPointerException

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.