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;
}
}
