This is code I\'m currently working on learning about ciphers. During/after encr
ID: 3722998 • Letter: T
Question
This is code I'm currently working on learning about ciphers. During/after encryption of the plain text, how would I save the cipher text in hexadecimal? Example and explanation woul be appreciated thank you.
package rccipher;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class RCCipher
{
static void output(int disp[])
{
char con[]=new char[disp.length];
for(int l=0;l<disp.length;l++)
{
con[l]=(char)disp[l];
System.out.print(con[l]);
}
}
public static void main(String args[])throws IOException
{
String text = "In cryptography, RC4 (Rivest Cipher 4 also known as ARC4 or "
+ " ARCFOUR meaning Alleged RC4) is a stream cipher. While remarkable"
+ " for its simplicity and speed in software, multiple vulnerabilities"
+ " have been discovered in RC4, rendering it insecure. It is especially"
+ " vulnerable when the beginning of the output keystream is not discarded,"
+ " or when nonrandom or related keys are used. Particularly problematic "
+ " uses of RC4 have led to very insecure protocols such as WEP.";
String key;
int sbox[] = new int[256];
int kgen[] = new int[256];
Scanner scan = new Scanner(System.in);
int temp=0;
System.out.println("ORIGINAL PLAIN TEXT:");
System.out.println(text);
System.out.println(" ENTER KEY:");
key=scan.nextLine();
char ptextc[]=text.toCharArray();
char keyc[]=key.toCharArray();
int cipher[]=new int[text.length()];
int decrypt[]=new int[text.length()];
int plainTexti[]=new int[text.length()];
int keyi[]=new int[key.length()];
for(int i=0;i<text.length();i++)
{
plainTexti[i]=(int)ptextc[i];
}
for(int i=0;i<key.length();i++)
{
keyi[i]=(int)keyc[i];
}
for(int i=0;i<255;i++)
{
sbox[i]=i; kgen[i]=keyi[i%key.length()];
}
int j=0;
for(int i=0;i<255;i++)
{
j=(j+sbox[i]+kgen[i])%256;
temp=sbox[i];
sbox[i]=sbox[j];
sbox[j]=temp;
}
int i=0;
j=0;
int z=0;
for(int l=0;l<text.length();l++)
{
i = (l+1)%256;
j = (j+sbox[i])%256;
temp = sbox[i];
sbox[i] = sbox[j];
sbox[j] = temp;
z = sbox[(sbox[i]+sbox[j])%256];
cipher[l] = z^plainTexti[l];
decrypt[l] = z^cipher[l];
}
System.out.println(" CIPHER TEXT:");
output(cipher);
System.out.println(" PLAIN TEXT:");
output(decrypt);
System.out.println(" ");
}
}
Explanation / Answer
CIpher text each and every character takes maximum 1 byte i.e., 0 to 255, So it can take max 2 characters(FF). so out cipher text in hexa decimal takes 255*2 characters so 512 bytes...
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class RCCipher {
static void output(int disp[]) {
char con[] = new char[disp.length];
for (int l = 0; l < disp.length; l++) {
con[l] = (char) disp[l];
System.out.print(con[l]);
}
}
public static void main(String args[]) throws IOException {
String text = "In cryptography, RC4 (Rivest Cipher 4 also known as ARC4 or "
+ " ARCFOUR meaning Alleged RC4) is a stream cipher. While remarkable"
+ " for its simplicity and speed in software, multiple vulnerabilities"
+ " have been discovered in RC4, rendering it insecure. It is especially"
+ " vulnerable when the beginning of the output keystream is not discarded,"
+ " or when nonrandom or related keys are used. Particularly problematic "
+ " uses of RC4 have led to very insecure protocols such as WEP.";
String key;
int sbox[] = new int[256];
int kgen[] = new int[256];
Scanner scan = new Scanner(System.in);
int temp = 0;
System.out.println("ORIGINAL PLAIN TEXT:");
System.out.println(text);
System.out.println(" ENTER KEY:");
key = scan.nextLine();
char ptextc[] = text.toCharArray();
char keyc[] = key.toCharArray();
int cipher[] = new int[text.length()];
int decrypt[] = new int[text.length()];
int plainTexti[] = new int[text.length()];
int keyi[] = new int[key.length()];
for (int i = 0; i < text.length(); i++) {
plainTexti[i] = (int) ptextc[i];
}
for (int i = 0; i < key.length(); i++) {
keyi[i] = (int) keyc[i];
}
for (int i = 0; i < 255; i++) {
sbox[i] = i;
kgen[i] = keyi[i % key.length()];
}
int j = 0;
for (int i = 0; i < 255; i++) {
j = (j + sbox[i] + kgen[i]) % 256;
temp = sbox[i];
sbox[i] = sbox[j];
sbox[j] = temp;
}
int i = 0;
j = 0;
int z = 0;
for (int l = 0; l < text.length(); l++) {
i = (l + 1) % 256;
j = (j + sbox[i]) % 256;
temp = sbox[i];
sbox[i] = sbox[j];
sbox[j] = temp;
z = sbox[(sbox[i] + sbox[j]) % 256];
cipher[l] = z ^ plainTexti[l];
convertHex(cipher);
decrypt[l] = z ^ cipher[l];
}
System.out.println(" CIPHER TEXT:");
output(cipher);
System.out.println(" PLAIN TEXT:");
output(decrypt);
System.out.println(" IN CIPHER HEXADECIMAL: "+convertHex(cipher));
System.out.println(" ");
}
public static String convertHex(int cipher[]){
String enc = "";
int i=0, len = cipher.length;
for(i=0; i<len; i++){
int n = cipher[i], j=1;
char h[] = new char[2];
h[0] = '0';
h[1] = '0';
while(n!=0){
int rem = n%16;
if(rem<10)
h[j] = (char)(rem+'0');
else
h[j] = (char)(rem+55);
n = n/16;
j--;
}
enc += h[0]+""+h[1];
}
return enc;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.