Take the 2-minute tour ×
Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. It's 100% free, no registration required.

I have the following code that uses a CBC mode to encrypt a text by dividing it to 3-elements block. But it only works correctly on the first block which is xored with the IV.

import java.util.ArrayList;

public class Encryption {
    static int[][] key ={{167,8,48},
        {54,107,25},
        {170,184,107}};
    static int[][] keyRevers ={{119,152,136},
        {184,235,231},
        {174,120,59}};

    //static int[] IV = {17,5,87};
    static ArrayList<Integer> IV = new ArrayList<Integer>();

    public Encryption(){
        IV.add(167);
        IV.add(8);
        IV.add(48);    
    }

    public static String enc(String input){
        ArrayList<ArrayList<Integer>> encryptedBlocks = new ArrayList<ArrayList<Integer>>();

        ArrayList<Integer> textValue = new ArrayList<Integer>();
        ArrayList<ArrayList<Integer>> blocks = new ArrayList<ArrayList<Integer>>();

        // get characters ----------------------
        for(int i =0; i<input.length();i++){
            textValue.add((int)input.charAt(i)); 
        }

        // create blocks------------------------
        int noOfBlocks = (int) Math.ceil(textValue.size()/3.0);
        for(int j =0; j<noOfBlocks;j++){
            blocks.add(new ArrayList<Integer>());
        }

        // add values to blocks-----------------
        int counter=0;

        for(int j =0; j<textValue.size();j++){
            if(j%3==0 && j!=0){
                counter ++;
            }
            blocks.get(counter).add(textValue.get(j));
        }    

        // CBC ----------------- (xoring)
        for(int k=0;k<blocks.size();k++){
            ArrayList<Integer> list =null;
            if(k==0)
            {
                list = encryptBlock(xor(blocks.get(k),IV));
            }else
            {
                list = encryptBlock(xor(blocks.get(k),encryptedBlocks.get(k-1)));
            }
            encryptedBlocks.add(list);
        }

        String val="";

        for(int k=0;k<encryptedBlocks.size();k++){
            for(int h=0;h<encryptedBlocks.get(k).size();h++){
                val += String.valueOf((char)(encryptedBlocks.get(k).get(h)+0));
            }
        }
        return val;    
    }

    public static String dec(String input){
        ArrayList<ArrayList<Integer>> decryptedBlocks = new ArrayList<ArrayList<Integer>>();

        ArrayList<Integer> textValue = new ArrayList<Integer>();
        ArrayList<ArrayList<Integer>> blocks = new ArrayList<ArrayList<Integer>>();

        for(int i =0; i<input.length();i++){
            textValue.add((int)input.charAt(i)); 
        }

        int noOfBlocks = (int) Math.ceil(textValue.size()/3.0);
        for(int j =0; j<noOfBlocks;j++){
            blocks.add(new ArrayList<Integer>());
        }

        int counter=0;

        for(int j =0; j<textValue.size();j++){
            if(j%3==0 && j!=0){
                counter ++;
            }    
            blocks.get(counter).add(textValue.get(j));
        }
        for(int k=0;k<blocks.size();k++){
            ArrayList<Integer> list = null;
            if(k==0)
            {
                list = xor(decryptBlock(blocks.get(k)),IV);
            }else
            {
                list = xor(decryptBlock(blocks.get(k)),decryptedBlocks.get(k-1));
            }    
            decryptedBlocks.add(list);
        }
        String val="";

        for(int k=0;k<decryptedBlocks.size();k++){
            for(int h=0;h<decryptedBlocks.get(k).size();h++){
                val += String.valueOf((char)(decryptedBlocks.get(k).get(h)+0));
            }
        }    
        return val;
    }

    public static ArrayList<Integer> decryptBlock(ArrayList<Integer> block){    
        ArrayList<Integer> list =  new ArrayList<Integer>();
        for(int x=0;x<block.size();x++){
            int no=0;

            for(int y=0;y<block.size();y++){
                no += keyRevers[x][y]*block.get(y);
            }
            list.add(no%256);
        }
        return list;
    }

    public static ArrayList<Integer> encryptBlock(ArrayList<Integer> block){
        ArrayList<Integer> list =  new ArrayList<Integer>();
        for(int x=0;x<block.size();x++){
            int no=0;

            for(int y=0;y<block.size();y++){
                no += key[x][y]*block.get(y);
            }    
            list.add((no%256));
        }
        return list;
    }

    public static ArrayList<Integer> xor(ArrayList<Integer> plainText,ArrayList<Integer> encrypted){
        ArrayList<Integer> xoredBlock = new ArrayList<Integer>();
        for(int i=0;i<plainText.size();i++){
            xoredBlock.add((plainText.get(i)^(encrypted.get(i))));
        }
        return xoredBlock;
    }
}
share|improve this question

closed as off topic by Ninefingers May 24 '12 at 7:33

Questions on Cryptography Stack Exchange are expected to relate to cryptography within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Hello - this is really off topic here and belongs on Stack Overflow; however, before I migrate it there it could really do with an edit - have you nailed down exactly where the issue is? SO likely don't need all of the code you've posted. If you flag when you've had a look at some of SO's questions and edited yours, I'll be able to migrate it there. Please don't cross post :) –  Ninefingers May 24 '12 at 7:33
add comment

1 Answer 1

up vote 1 down vote accepted
list = xor(decryptBlock(blocks.get(k)), decryptedBlocks.get(k-1));

You need to xor with the same thing you xored with during encryption: the previous block of the ciphertext, not the previous block of the plaintext.

list = xor(decryptBlock(blocks.get(k)), blocks.get(k-1));

Check the following illustration from wikipedia:

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.