RC6 Sample
RC6 Sample
RC6 Sample
//sample//sample//sample//sample//sample
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
int A,B,i,j;
A=B=i=j=0;
return S;
}
// ENCRYPTION ALGORITHM
public byte[] encryption(byte[] keySchArray){
int temp,t,u;
for(int i =0;i<temp_data.length;i++)
temp_data[i] = 0;
temp_data=convertBytetoInt(keySchArray,temp_data.length);
int A,B,C,D;
A=B=C=D=0;
A = temp_data[0];
B = temp_data[1];
C = temp_data[2];
D = temp_data[3];
B = B + S[0];
D = D + S[1];
int lgw=5;
t = rotateLeft(B*(2*B+1),lgw);
u = rotateLeft(D*(2*D+1),lgw);
A = rotateLeft(A^t,u)+S[2*i];
C = rotateLeft(C^u,t)+S[2*i+1];
temp = A;
A = B;
B = C;
C = D;
D = temp;
}
A = A + S[2*r+2];
C = C + S[2*r+3];
temp_data[0] = A;
temp_data[1] = B;
temp_data[2] = C;
temp_data[3] = D;
outputArr = convertIntToByte(temp_data,keySchArray.length);
return outputArr;
}
//DECRYPTION ALGORITHM
public byte[] decryption(byte[] keySchArray){
int temp,t,u;
int A,B,C,D;
A=B=C=D=0;
int[] temp_data_decryption = new int[keySchArray.length/4];
for(int i =0;i<temp_data_decryption.length;i++)
temp_data_decryption[i] = 0;
temp_data_decryption=convertBytetoInt(keySchArray,temp_data_decryption.length);
A = temp_data_decryption[0];
B = temp_data_decryption[1];
C = temp_data_decryption[2];
D = temp_data_decryption[3];
C = C - S[2*r+3];
A = A - S[2*r+2];
int lgw=5;
u = rotateLeft(D*(2*D+1),lgw);
t = rotateLeft(B*(2*B+1),lgw);
C= rotateRight(C-S[2*i+1],t)^u;
A= rotateRight(A-S[2*i], u)^t;
}
D=D-S[1];
B=B-S[0];
temp_data_decryption[0] = A;
temp_data_decryption[1] = B;
temp_data_decryption[2] = C;
temp_data_decryption[3] = D;
outputArr =
convertIntToByte(temp_data_decryption,keySchArray.length);
return outputArr;
}
return int_to_byte;
}
int counter = 0;
for(int i=0;i<byte_to_int.length;i++){
byte_to_int[i] = ((arr[counter++]&0xff))|
((arr[counter++]&0xff) << 8) |
((arr[counter++]&0xff) << 16) |
((arr[counter++]&0xff) << 24);
}
return byte_to_int;
return bytes_to_words;
}
// MAIN
public static void main(String[] args) {
String tempString;
String given_text;
String text_data;
String key_value;
String []input_text_val;
int total_lines;
BufferedWriter output_to_text_file=null;
Scanner sc=new Scanner(System.in);
try {
FileReader input_file=new FileReader("input.txt");
FileWriter output_file=new FileWriter("output.txt",false);
BufferedReader bf=new BufferedReader(input_file);
String choice=bf.readLine();
if (choice.equals("Encryption")){
given_text=bf.readLine();
input_text_val=given_text.split(":");
text_data=input_text_val[1];
key_value=bf.readLine();
String []input_key_val=key_value.split(":");
tempString=input_key_val[1];
byte[] key2=hexStringToByteArray(tempString);
byte[] X=hexStringToByteArray(text_data);
S = new AlgorithmRC6().KeySchedule(key2);
byte[] decrypt=new AlgorithmRC6().decryption(X);
String decrypted_text=byteArrayToHex(decrypt);
decrypted_text = decrypted_text.replaceAll("..", "$0
");
output_to_text_file=new BufferedWriter(output_file);
output_to_text_file.write("plaintext: "+
decrypted_text);
}
else {
System.out.println("Invalid option");
}
} catch (FileNotFoundException e) {
System.out.println("File not found exception");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IO exception");
e.printStackTrace();
}
finally
{
if(output_to_text_file != null) {
try {
output_to_text_file.close();
} catch (IOException e) {
System.out.println("File cannot be closed");
e.printStackTrace();
}
}
}
}
}