Results 1 to 2 of 2
  1. #1
    azem1687 is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default I have done a project in Simplified DES with cbc but i am getting an error

    here is the error
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at mycipher.Main.main(Main.java:24)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)


    here is my main.java and DES_M.java ...Please help out as i dont have any issues in the size of string=modes.

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package mycipher;
    import java.io.*;
    import java.util.Arrays;
    /**
    *
    * @author Administrator
    */
    public class Main {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) throws IOException {
    // TODO code application logic here
    char[] i_key_Ar=new char[10];
    char[] i_vector_Ar=new char[8];
    String modes,initial_key,initial_vector,plaintext,ciphert ext;
    //encrypt or decrypt mode
    modes = args[1];
    int i=0;
    initial_key=args[3];
    initial_vector=args[5];
    plaintext=args[7];
    ciphertext=args[9];
    i_key_Ar=initial_key.toCharArray();
    i_vector_Ar=initial_vector.toCharArray();

    int mode=0;
    if(modes.equals("encrypt"))
    {
    mode=1;
    }
    else if(modes.equals("decrypt"))
    {
    mode=2;
    }
    else
    {
    System.out.println("enter correct mode");
    }

    // int [] plain_data = new int[16]; //plaintext data
    int [] cipher_data = new int[8];
    int []i_key= new int[10]; //initial key
    int []subkey1= new int[8];
    int []subkey2= new int[8];

    i_key[0] = Integer.parseInt(Character.toString(i_key_Ar[0]));
    i_key[1] = Integer.parseInt(Character.toString(i_key_Ar[1]));
    i_key[2] = Integer.parseInt(Character.toString(i_key_Ar[2]));
    i_key[3] = Integer.parseInt(Character.toString(i_key_Ar[3]));
    i_key[4] = Integer.parseInt(Character.toString(i_key_Ar[4]));
    i_key[5] = Integer.parseInt(Character.toString(i_key_Ar[5]));
    i_key[6] = Integer.parseInt(Character.toString(i_key_Ar[6]));
    i_key[7] = Integer.parseInt(Character.toString(i_key_Ar[7]));
    i_key[8] = Integer.parseInt(Character.toString(i_key_Ar[8]));
    i_key[9] = Integer.parseInt(Character.toString(i_key_Ar[9]));

    int []i_vector= new int[8]; //initial vector

    i_vector[0] = Integer.parseInt(Character.toString(i_vector_Ar[0]));
    i_vector[1] = Integer.parseInt(Character.toString(i_vector_Ar[1]));
    i_vector[2] = Integer.parseInt(Character.toString(i_vector_Ar[2]));
    i_vector[3] = Integer.parseInt(Character.toString(i_vector_Ar[3]));
    i_vector[4] = Integer.parseInt(Character.toString(i_vector_Ar[4]));
    i_vector[5] = Integer.parseInt(Character.toString(i_vector_Ar[5]));
    i_vector[6] = Integer.parseInt(Character.toString(i_vector_Ar[6]));
    i_vector[7] = Integer.parseInt(Character.toString(i_vector_Ar[7]));

    //class file for all called functions
    DES_M a1=new DES_M();

    int [] temp_key=new int[10];
    for (i=0; i < i_key.length; i++) {
    temp_key[i]=i_key[i];
    }
    //***********************************SUBKEY1******** ************************
    subkey1=a1.key1_gen(i_key);
    System.out.print("K1= ");
    for (i=0; i < subkey1.length; i++) {
    System.out.print(subkey1[i]);
    }

    //***********************************SUBKEY2******** **************************
    subkey2=a1.key2_gen(temp_key);
    System.out.print("K2= ");
    for (i=0; i < subkey2.length; i++) {
    System.out.print(subkey2[i]);
    }

    File file1 = new File (plaintext);
    File file2 = new File (ciphertext);

    //Read Input file of plaintext***************************************** *******************
    FileInputStream file_input = null ;
    DataInputStream data_in = null ;
    FileOutputStream file_output= null;

    try {
    // Wrap the FileInputStream with a DataInputStream
    if(mode==1)
    {
    file_input = new FileInputStream (file1);
    }
    else if(mode==2)
    {
    file_input = new FileInputStream (file2);
    }
    data_in = new DataInputStream (file_input);

    System.out.println("\n");
    if(mode==1)
    {
    System.out.print("Plaintext= ");
    }
    else if(mode==2)
    {
    System.out.print("Ciphertext= ");
    }

    int [] bb1=new int[100];

    // Now write the data array to the byte array according to the file********************************************** *
    i=0;
    while (true) {
    try {

    bb1[i]=data_in.readUnsignedByte();
    i++;
    }
    catch (IOException e) {
    break;
    }
    }
    int [] bb=new int[i];
    String s="",tt="";
    for(int j=0;j<i;j++)
    {
    bb[j]=bb1[j];
    tt=Integer.toBinaryString(bb[j]);
    while (tt.length() % 8 != 0) {
    // Pad with 0
    tt = "0"+tt;
    }
    s=s.concat(tt);
    }

    //BigInteger bi = new BigInteger(bb);
    // Format to binary
    //String s = bi.toString(2);

    int [] temp_plain = new int[s.length()] ;
    for(i=0;i<s.length();i++)
    {
    String ss=s.substring(i, i+1);
    //System.out.print(ss);
    if(ss.equals("1")||(ss.equals("0")))
    {
    temp_plain[i]=Integer.parseInt(ss);
    }
    }
    for(i=0;i<temp_plain.length;i++)
    {
    if(i%8==0)
    {
    System.out.print(" ");
    }
    System.out.print(temp_plain[i]);
    }

    System.out.println("\n");
    if(mode==1)
    {
    System.out.print("Ciphertext= ");
    cipher_data=a1.encrypt_CBC(temp_plain,subkey1,subk ey2,i_vector);
    }
    else if(mode==2)
    {
    System.out.print("Plaintext= ");
    cipher_data=a1.decrypt_CBC(temp_plain,subkey1,subk ey2,i_vector);
    }

    file_input.close ();
    //data_in.close ();
    }

    catch (IOException e) {
    System.out.println ( "IO Exception =: " + e );
    }
    //System.out.println("\n");

    try {
    // Create an output stream to the file.
    if(mode==1)
    {
    file_output = new FileOutputStream (file2);
    }
    else if(mode==2)
    {
    file_output = new FileOutputStream (file1);
    }
    DataOutputStream data_out = new DataOutputStream (file_output);
    for (i=0; i < cipher_data.length; i++) {
    if(i%8==0)
    {
    System.out.print(" ");
    }
    System.out.print(cipher_data[i]);
    }
    //Replacing all unwanted characters from string and
    //converting string to int array
    String ss=null;
    String temp=null;
    ss=Arrays.toString(cipher_data).replace(", ", "");
    ss=ss.replace("[", "");
    ss=ss.replace("]", "");

    int counter=ss.length()/8;
    i=0;
    int[] val=new int[counter];
    for(;i<counter;i++)
    {
    temp=ss.substring(8*i,8*i+8 );
    val[i]=Integer.parseInt(temp,2);
    }

    // Write the data to the file in an integer/double pair
    for (i=0; i < val.length; i++) {
    data_out.writeByte(val[i]);
    }

    file_output.close ();
    }
    catch (IOException e) {
    System.out.println ("IO exception = " + e );
    }
    }

    }
    -------------------------------------------
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package mycipher;

    /**
    *
    * @author Administrator
    */
    public class DES_M {

    int temp,temp1,temp2=0;
    int[] in_key=new int[10];
    //****************Subkey 1 generation func*******************************

    public int [] key1_gen(int[] in_key)
    {
    int[] in_key1=new int[8];

    int [] temp_key=new int[10];
    for (int i=0; i < in_key.length; i++) {
    //System.out.print(temp_key[i]);
    temp_key[i]=in_key[i];
    }
    //Apply P10
    in_key[0] = temp_key[2]; in_key[1] = temp_key[4]; in_key[2] = temp_key[1]; in_key[3] = temp_key[6];
    in_key[4] = temp_key[3]; in_key[5] = temp_key[9]; in_key[6] = temp_key[0]; in_key[7] = temp_key[8];
    in_key[8] = temp_key[7]; in_key[9] = temp_key[5];

    //Circular left shift or rotation
    temp=in_key[0];

    in_key[0] = in_key[1]; in_key[1] = in_key[2]; in_key[2] = in_key[3]; in_key[3] = in_key[4];
    in_key[4] = temp;

    temp=in_key[5];
    in_key[5] = in_key[6]; in_key[6] = in_key[7]; in_key[7] = in_key[8]; in_key[8] = in_key[9];
    in_key[9] = temp;

    in_key1[0] = in_key[5]; in_key1[1] = in_key[2]; in_key1[2] = in_key[6]; in_key1[3] = in_key[3];
    in_key1[4] = in_key[7]; in_key1[5] = in_key[4]; in_key1[6] = in_key[9]; in_key1[7] = in_key[8];

    return in_key1;
    }

    //********************subkey 2 generation function*******************************

    public int [] key2_gen(int[] in_keyk)
    {
    int[] in_key2=new int[8];
    int [] temp_key=new int[10];
    for (int i=0; i < in_keyk.length; i++) {
    temp_key[i]=in_keyk[i];
    //System.out.print(temp_key[i]);
    }
    //Apply P10
    in_keyk[0] = temp_key[2]; in_keyk[1] = temp_key[4]; in_keyk[2] = temp_key[1]; in_keyk[3] = temp_key[6];
    in_keyk[4] = temp_key[3]; in_keyk[5] = temp_key[9]; in_keyk[6] = temp_key[0]; in_keyk[7] = temp_key[8];
    in_keyk[8] = temp_key[7]; in_keyk[9] = temp_key[5];

    for (int i=0; i < in_keyk.length; i++) {
    //System.out.print(in_keyk[i]);
    }
    System.out.println("\n");
    //Circular left shift or rotation for three positions
    temp=in_keyk[0];
    temp1=in_keyk[1];
    temp2=in_keyk[2];

    in_keyk[0] = in_keyk[3]; in_keyk[1] = in_keyk[4]; in_keyk[2] = temp; in_keyk[3] = temp1;
    in_keyk[4] = temp2;

    temp=in_keyk[5];
    temp1=in_keyk[6];
    temp2=in_keyk[7];

    in_keyk[5] = in_keyk[8]; in_keyk[6] = in_keyk[9]; in_keyk[7] = temp; in_keyk[8] = temp1;
    in_keyk[9] = temp2;
    ///////////////////////////////////////////////////////
    //Apply P8
    in_key2[0] = in_keyk[5]; in_key2[1] = in_keyk[2]; in_key2[2] = in_keyk[6]; in_key2[3] = in_keyk[3];
    in_key2[4] = in_keyk[7]; in_key2[5] = in_keyk[4]; in_key2[6] = in_keyk[9]; in_key2[7] = in_keyk[8];

    return in_key2;

    }

    //XOR function
    public int XOR(int x,int y)
    {
    if(x==y)
    return 0;
    else
    return 1;
    }

    //SWITCH Function
    public int[] switch_func(int[] fdata)
    {
    int []tdata=new int[4];
    int []mdata=new int[8];
    for (int i=0; i < 4; i++) {
    tdata[i]=fdata[i];
    }
    for (int i=0; i < 4; i++) {
    mdata[i]=fdata[i+4];
    }
    for (int i=0; i < 4; i++) {
    mdata[i+4]=tdata[i];
    }
    return mdata;
    }

    // The function Fk ************************************
    public int [] fk_phase(int[] pdata,int [] subkey)
    {
    int [] temp_data=new int[8];
    int [] RN=new int[4];
    int [] LF=new int[4];
    int [] fk=new int[4];
    int [] fk_temp=new int[4];
    int s0row=0;
    int s0colum=0;
    int s1row=0;
    int s1colum=0;
    int [][] PM=new int[2][4];
    int [][] S0=new int[4][4];
    int [][] S1=new int[4][4];
    //S0 BOX
    S0[0][0]=1; S0[0][1]=0; S0[0][2]=3; S0[0][3]=2;
    S0[1][0]=3; S0[1][1]=2; S0[1][2]=1; S0[1][3]=0;
    S0[2][0]=0; S0[2][1]=2; S0[2][2]=1; S0[2][3]=3;
    S0[3][0]=3; S0[3][1]=1; S0[3][2]=3; S0[3][3]=2;
    //S1 BOX
    S1[0][0]=0; S1[0][1]=1; S1[0][2]=2; S1[0][3]=3;
    S1[1][0]=2; S1[1][1]=0; S1[1][2]=1; S1[1][3]=3;
    S1[2][0]=3; S1[2][1]=0; S1[2][2]=1; S1[2][3]=0;
    S1[3][0]=2; S1[3][1]=1; S1[3][2]=0; S1[3][3]=3;

    for (int i=0; i < pdata.length; i++) {
    temp_data[i]=pdata[i];
    }
    ///RH side 4 bits
    for (int i=0; i < 4; i++) {
    RN[i]=pdata[i+4];
    }
    //LH side 4 bits
    for (int i=0; i < 4; i++) {
    LF[i]=pdata[i];
    }
    //Generating FK[0] and FK[1]
    PM[0][0]= XOR(RN[3],subkey[0]); PM[0][1]= XOR(RN[0],subkey[1]); PM[0][2]= XOR(RN[1],subkey[2]);
    PM[0][3]= XOR(RN[2],subkey[3]);

    if(PM[0][0]==0)
    {
    if(PM[0][3]==0)
    {
    s0row=0;
    }
    else if(PM[0][3]==1)
    {
    s0row=1;
    }
    }
    else if(PM[0][0]==1)
    {
    if(PM[0][3]==0)
    {
    s0row=2;
    }
    else if(PM[0][3]==1)
    {
    s0row=3;
    }
    }

    if(PM[0][1]==0)
    {
    if(PM[0][2]==0)
    {
    s0colum=0;
    }
    else if(PM[0][2]==1)
    {
    s0colum=1;
    }
    }
    else if(PM[0][1]==1)
    {
    if(PM[0][2]==0)
    {
    s0colum=2;
    }
    else if(PM[0][2]==1)
    {
    s0colum=3;
    }
    }
    int fk01=S0[s0row][s0colum];

    if(fk01==0)
    {
    fk[0]=0; fk[1]=0;
    }
    else if(fk01==1)
    {
    fk[0]=0; fk[1]=1;
    }
    else if(fk01 == 2)
    {
    fk[0]=1; fk[1]=0;
    }
    else if(fk01 == 3)
    {
    fk[0]=1; fk[1]=1;
    }
    ////////////////////////////Generating FK[2] and fk[3]/////////////////////////////////////////

    PM[1][0]= XOR(RN[1],subkey[4]); PM[1][1]= XOR(RN[2],subkey[5]); PM[1][2]= XOR(RN[3],subkey[6]);
    PM[1][3]= XOR(RN[0],subkey[7]);

    if(PM[1][0]==0)
    {
    if(PM[1][3]==0)
    {
    s1row=0;
    }
    else if(PM[1][3]==1)
    {
    s1row=1;
    }
    }
    else if(PM[1][0]==1)
    {
    if(PM[1][3]==0)
    {
    s1row=2;
    }
    else if(PM[1][3]==1)
    {
    s1row=3;
    }
    }

    if(PM[1][1]==0)
    {
    if(PM[1][2]==0)
    {
    s1colum=0;
    }
    else if(PM[1][2]==1)
    {
    s1colum=1;
    }
    }
    else if(PM[1][1]==1)
    {
    if(PM[1][2]==0)
    {
    s1colum=2;
    }
    else if(PM[1][2]==1)
    {
    s1colum=3;
    }
    }
    int fk23=S1[s1row][s1colum];

    if(fk23==0)
    {
    fk[2]=0; fk[3]=0;
    }
    else if(fk23==1)
    {
    fk[2]=0; fk[3]=1;
    }
    else if(fk23 == 2)
    {
    fk[2]=1; fk[3]=0;
    }
    else if(fk23 == 3)
    {
    fk[2]=1; fk[3]=1;
    }
    /////////////////////////////////////////////////////////////////////
    //APPLY PURMUTATION P4
    for (int i=0; i < fk.length; i++) {
    fk_temp[i]=fk[i];
    }
    fk[0]=fk_temp[1]; fk[1]=fk_temp[3]; fk[2]=fk_temp[2]; fk[3]=fk_temp[0];

    fk_temp[0]=XOR(fk[0],LF[0]); fk_temp[1]=XOR(fk[1],LF[1]);
    fk_temp[2]=XOR(fk[2],LF[2]); fk_temp[3]=XOR(fk[3],LF[3]);
    //************************************************** *****

    for (int i=0; i < fk_temp.length; i++) {
    temp_data[i]=fk_temp[i];
    }
    for (int i=0; i < RN.length; i++) {
    temp_data[i+4]=RN[i];
    }
    //temp_data contains total fk data

    return temp_data;
    }

    //Initial Purmutation Function****************************************** ******
    public int[] init_PM(int [] pdata)
    {
    int [] temp_data=new int[8];
    temp_data[0]=pdata[1]; temp_data[1]=pdata[5]; temp_data[2]=pdata[2]; temp_data[3]=pdata[0];
    temp_data[4]=pdata[3]; temp_data[5]=pdata[7]; temp_data[6]=pdata[4]; temp_data[7]=pdata[6];

    return temp_data;
    }

    //Inverse Purmutation Function****************************************** ******
    public int[] reverse_PM(int [] pdata)
    {
    int [] temp_data=new int[8];
    temp_data[0]=pdata[3]; temp_data[1]=pdata[0]; temp_data[2]=pdata[2]; temp_data[3]=pdata[4];
    temp_data[4]=pdata[6]; temp_data[5]=pdata[1]; temp_data[6]=pdata[7]; temp_data[7]=pdata[5];

    return temp_data;
    }

    //*****Normal Simplified DES encryption Function with 5 steps******************
    public int[] encrypt(int[] plain_data,int[] subkey1,int[] subkey2)
    {
    int[] cipher_data=new int[8];

    cipher_data=init_PM(plain_data); //1st Step

    cipher_data=fk_phase(cipher_data,subkey1); //2nd step

    cipher_data=switch_func(cipher_data); //3rd Step

    cipher_data=fk_phase(cipher_data,subkey2); //4th step

    cipher_data=reverse_PM(cipher_data); //5th Step

    return cipher_data;
    }

    public String HextoBinary(String hx)
    {

    int i = Integer.parseInt(hx);
    String by = Integer.toBinaryString(i);
    return by;
    }

    //****************The main encryption Function with CBC mode********************
    public int[] encrypt_CBC(int[] plain_data,int[] subkey1,int[] subkey2,int [] init_vector)
    {
    //int [] cipher_data=new int[plain_data.length];
    int [] temp_data=new int[8];
    int [] used_data=new int[plain_data.length];

    int c=plain_data.length/8;
    if(c==1) //CBC Mode single Block***************************
    {
    for(int i=0;i<8;i++)
    {
    temp_data[i]=XOR(plain_data[i],init_vector[i]);
    //System.out.print(temp_data[i]);
    }
    used_data=encrypt(temp_data,subkey1,subkey2);
    }
    else if(c>1) //CBC MODE MULTIPLE BLOCKS***********************
    {
    int cc=1;
    for(int i=0;i<8;i++)
    {
    temp_data[i]=XOR(init_vector[i],plain_data[i]);
    }

    temp_data=encrypt(temp_data,subkey1,subkey2);
    for(int i=0;i<8;i++)
    {
    used_data[i]=temp_data[i];
    }
    while(cc<c){

    for(int i=0;i<8;i++)
    {
    temp_data[i]=XOR(temp_data[i],plain_data[cc*8+i]);
    }
    temp_data=encrypt(temp_data,subkey1,subkey2);
    for(int i=0;i<8;i++)
    {
    used_data[cc*8+i]=temp_data[i];
    }
    cc++;
    }
    }
    return used_data;
    }


    public int[] decrypt(int[] cipher_data,int[] subkey1,int[] subkey2)
    {
    int[] plain_data=new int[8];

    plain_data=init_PM(cipher_data); //1st Step
    plain_data=fk_phase(plain_data,subkey2); //2nd step
    plain_data=switch_func(plain_data); //3rd Step
    plain_data=fk_phase(plain_data,subkey1); //4th step
    plain_data=reverse_PM(plain_data); //5th Step

    return plain_data;
    }
    //****The main Decryption Function with CBC mode
    public int[] decrypt_CBC(int[] cipher_data,int[] subkey1,int[] subkey2,int [] init_vector)
    {
    //int [] cipher_data=new int[plain_data.length];
    int [] temp_data=new int[8];
    int [] temp1_data=new int[8];
    int [] temp2_data=new int[8];
    int [] used_data=new int[cipher_data.length];

    int c=cipher_data.length/8;
    if(c==1) //CBC Mode single Block***************************
    {
    temp_data=decrypt(cipher_data,subkey1,subkey2);
    for(int i=0;i<8;i++)
    {
    used_data[i]=XOR(temp_data[i],init_vector[i]);
    }

    }
    else if(c>1) //CBC MODE MULTIPLE BLOCKS***********************
    {
    int cc=1;
    for(int i=0;i<8;i++)
    {
    temp1_data[i]=cipher_data[i];
    }
    temp_data=decrypt(temp1_data,subkey1,subkey2);
    for(int i=0;i<8;i++)
    {
    temp_data[i]=XOR(init_vector[i],temp_data[i]);
    }

    for(int i=0;i<8;i++)
    {
    used_data[i]=temp_data[i];
    }
    while(cc<c){

    for(int i=0;i<8;i++)
    {
    temp2_data[i]=cipher_data[cc*8+i];
    }

    temp_data=decrypt(temp2_data,subkey1,subkey2);

    for(int i=0;i<8;i++)
    {
    temp_data[i]=XOR(temp_data[i],cipher_data[(cc-1)*8+i]);
    }

    for(int i=0;i<8;i++)
    {
    used_data[cc*8+i]=temp_data[i];
    }
    cc++;
    }
    }
    return used_data;
    }

    }

  2. #2
    couling is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    Hi

    Firstly I'm guessing you didn't write this code.

    Secondly when posting on any forum, use code tags for code. It's not just for show, they really make code a lot more readable.

    Thirdly learn the meaning of SSCCE. It helps you answer questions yourself and helps us give quicker answers.

    Fourthly you need to look up what "args" is used for in a java main method. Hint: its an array but because of the way you are running the program it only has one element.
    ----Signature ----
    Please use [CODE] tags and indent correctly. It really helps when reading your code.

Similar Threads

  1. Eclipse project error
    By bhanu in forum Eclipse
    Replies: 8
    Last Post: 09-07-2011, 02:56 PM
  2. when i build my project it give following error
    By enggvijaysingh@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 11-18-2010, 11:02 AM
  3. Replies: 0
    Last Post: 08-13-2009, 04:18 AM
  4. Simplified explanation of Mnemonic
    By dbashby in forum New To Java
    Replies: 4
    Last Post: 04-10-2009, 09:56 PM
  5. Eclipse - Error Delete Project
    By giugy in forum Eclipse
    Replies: 1
    Last Post: 04-24-2008, 02:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •