We Discussed about using 4 bytes state and 2 bytes key for stream generation. As
ID: 3753728 • Letter: W
Question
We Discussed about using 4 bytes state and 2 bytes key for stream generation. Assuming that K = [2,1] and the permutation S = [1,2,3,0] has been obtained after Step 2 (initial permutation of S). Write a Program to complete the followings:
1. Complete the Stream generation step (step 3) to obtain streams.
2. USing the streams to encrypt the message: "HI" (H = 01001000 and I = 01001001)
Your output should look like the following screenshot.
String res="";
// xor
int c = p ^ k;
// Binary representation
String b4p = String.format("%8s", Integer.toBinaryString((int)p)).replace(' ', '0');
String b4k = String.format("%8s", Integer.toBinaryString(k)).replace(' ', '0');
String b4c = String.format("%8s", Integer.toBinaryString(c)).replace(' ', '0');
// print out
System.out.printf("%s (%2c) ", b4p,p);
System.out.printf("%s (%2d) ", b4k, k);
System.out.printf("-------------- ");
System.out.printf("%s (%c^%d) ", b4c, p,k);
// print out the corresponding letter
res += (char) c;
return res;
}
public static int [] streamGeneration (int keylen) {
int key [] = new int[keylen];
int S[] = {1,2,3,0};
int i = 0;
int j = 0;
int count = 0;
int temp, t;
// ......
return key;
}
public static void main () {
// key generation
int [] key = streamGeneration (2);
System.out.print("Key: ");
for (int k=0; k System.out.print(key[k] + " ");
}
System.out.println(" ");
// encryption
String cipher = encryption ('A', 3);
System.out.printf("Cipher text: %s ", cipher);
}
}
Explanation / Answer
Hi I have implemented how to find the next permutation and then take number of key len elements from S[] array.
after that we need to call the encryption method for H and I as well.
Please find the below code and output after running it in any java compiler.
/*package whatever //do not write package name here */
import java.io.*;
public class StreamCipher
{
public static String encryption (char p, int k) {
String res="";
// xor
int c = p ^ k;
// Binary representation
String b4p = String.format("%8s", Integer.toBinaryString((int)p)).replace(' ', '0');
String b4k = String.format("%8s", Integer.toBinaryString(k)).replace(' ', '0');
String b4c = String.format("%8s", Integer.toBinaryString(c)).replace(' ', '0');
// print out
System.out.printf("%s (%2c) ", b4p,p);
System.out.printf("%s (%2d) ", b4k, k);
System.out.printf("-------------- ");
System.out.printf("%s (%c^%d) ", b4c, p,k);
// print out the corresponding letter
res += (char) c;
return res;
}
public static int [] streamGeneration (int keylen) {
int key [] = new int[keylen];
int S[] = {1,2,3,0};
int i = 0;
int j = 0;
int count = 0;
int temp, t;
// ......
//this piece of code will find the next permutation
i = S.length - 1;
while (i > 0 && S[i - 1] >= S[i]) {
i--;
}
// if (i <= 0) {
// return false;
// }
j = S.length - 1;
while (S[j] <= S[i - 1]) {
j--;
}
temp = S[i - 1];
S[i - 1] = S[j];
S[j] = temp;
j = S.length - 1;
while (i < j) {
temp = S[i];
S[i] = S[j];
S[j] = temp;
i++;
j--;
}
for(i=0;i<4;i++){
//System.out.print(" "+S[i]);
if(count<keylen){
key[count] = S[keylen+i-1];
}
count++;
}
return key;
}
public static void main (String[] args) {
// key generation
int [] key = streamGeneration (2);
System.out.print("Key: ");
for (int k=0; k<2;k++){
System.out.print(key[k] + " ");
}
System.out.println(" ");
// encryption
String cipher = encryption ('H', 3);
//System.out.printf("Cipher text: %s ", cipher);
String encrypted_cipher = cipher;
cipher = encryption ('I', 0);
encrypted_cipher+=cipher;
System.out.printf("Cipher text: %s ", encrypted_cipher);
}
}
Output :
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.