Question
Modes of operation for DES-like cipher Expand the program you wrote for the previous assignment to let it support ECB and CBC modes of operation.
Modes of operation for DES-like cipher
Expand the program you wrote for the previous assignment to let it support ECB and CBC modes of operation. The input of your program must be a string. The output must be two hexadecimal strings.
Input: World! (note: the trailing exclamation mark is part of the input)
Key: 421 (note: numerical value in base-10)
IV (if needed): 124 (note: numerical value in base-10)
Output:
ECB: 0x9f279835b7b4 (maybe) CBC: 0x4f3c5e86f7bd (maybe)
Previous Program
public class DESCypher { private static int sbox1[][]={ {0b101,0b010,0b001,0b110,0b011,0b100,0b111,0b000}, {0b001,0b100,0b110,0b010,0b000,0b111,0b101,0b011} }; private static int sbox2[][]={{0b100,0b000,0b110,0b101,0b111,0b001,0b011,0b010}, {0b101,0b011,0b000,0b111,0b110,0b010,0b001,0b100}}; //[1, 2, 4, 3, 4, 3, 5, 6] private String expander(String bytes){ StringBuffer sb=new StringBuffer(); sb.append(bytes.charAt(0)); sb.append(bytes.charAt(1)); sb.append(bytes.charAt(3)); sb.append(bytes.charAt(2)); sb.append(bytes.charAt(3)); sb.append(bytes.charAt(2)); sb.append(bytes.charAt(4)); sb.append(bytes.charAt(5)); return sb.toString(); } private String keySchedule(String bytes,String MasterKey,int Round){ StringBuffer sb = new StringBuffer(); //generating roundKey String roundKey=MasterKey.substring(Round-1); if(Round>1){ roundKey+=MasterKey.substring(0,Round-2); } //bytes XOR roundKey for (int i = 0; i < bytes.length(); i++) { sb.append(bytes.charAt(i)^roundKey.charAt(i)); } String str1=sb.substring(0,4); String str2=sb.substring(4,8); int row=str1.charAt(0)-48; int col=Integer.parseInt(str1.substring(1,4),2); String result=Integer.toBinaryString(sbox1[row][col]); while (result.length()<3){ result="0"+result; } row=str2.charAt(0)-48; col=Integer.parseInt(str2.substring(1,4),2); String res2=Integer.toBinaryString(sbox2[row][col]); while (res2.length()<3){ res2="0"+res2; } result+=res2; return result; } private String f_function(String bytes,String MasterKey,int Round){ bytes=expander(bytes); bytes=keySchedule(bytes,MasterKey,Round); return bytes; } public String cipher(String bytes,String MasterKey,int Round){ String L0=bytes.substring(0,6); String R0=bytes.substring(6,12); String res=f_function(R0,MasterKey,Round); StringBuffer sb = new StringBuffer(); for (int i = 0; i < L0.length(); i++) { sb.append(L0.charAt(i)^res.charAt(i)); } R0+=sb.toString(); return R0; } //100110 011000 public static void main(String[] args) { String s1="011100100110"; String mKey="010011001"; DESCypher desCypher=new DESCypher(); String res=desCypher.cipher(s1,mKey,4); System.out.println(res); } }
SAMPLE CODE FOR MODE OF OPERATION
public class DESLikeECB extends DESLike { public long encrypt(long x, int masterkey) { int block=0, // contain each 12-bit block of plaintext cipherblock=0, // contains the encrypted 12-bit block blockcount=0; // is the number of blocks we have already encrypted long ciphertext=0; // output of our function // ensure that the masterkey is indeed no more than 9 bits long assert masterkey >= 0 && masterkey <= 0x111111111 : "Invalid masterkey in encrypt"; /* while loop "breaks down" the overall plaintext (x) into smaller 12-bit * blocks that can be provided to the encryption function * * isolate and remove the 12 least significant bits from the ciphertext, * until no bits are left */ while (x > 0) { block = (int)(x & (long)0xFFF); // isolate 12 bits (xFFF) x = x >> 12; // remove 12 bits cipherblock=super.encrypt(block, masterkey); // encrypt block // places cipherblock in the right position and adds it to the ciphertext ciphertext = cipherblock<<(12*blockcount) | ciphertext; } return ciphertext; } public static void main(String[] args) { DESLikeECB ecb = new DESLikeECB(); ecb.encrypt(Long.parseLong("576f726c6421", 16), 421); } } /*ECB / CBC Electronic Code Books y1 = e(x1) y2 = e(x2) y3 = e(x3) y4 = e(x4) W o r l d ! 0x576f726c6421 = 48 bit (long) 0x 576 f72 6c6 421*/ /* Cipher Block Chaining */
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started