Vernam Encryption Write a program in C that will encrypt a file use XOR and a Ve
ID: 3673695 • Letter: V
Question
Vernam Encryption Write a program in C that will encrypt a file use XOR and a Vernam key. A Vernam key is a short string. (The real system used a much longer random sequence.) Read the input at low level, as binary data (hint unsigned char is useful here.) Then xor each character in the binary data with the character in the key. When you get to the end of the key reuse the key from the beginning. The program should take command line arguments for the key, input and output. ./vern abc input.clear output.encrypted Note that the cipher should decrypt its own output. The command: ./vern abc output.encrypted input.clear should recover the original input. If you use open to create the output file, you may want to also set O RDWR or O WRONLY as well as O CREAT. You could also use fread and fwrite for this problem. 2. Finding the period This cipher is vulnerable if the key repeats, and with a short key, like abc above, it will repeat many times for any reasonably sized input. The incidence of coincidence slides the cipher along itself and counts the number of times the same symbol is seen. ABCABCABC count is 9 ABCABCABC then shift 1 ABCABCABC count is 0 ABCABCABC then shift 2 ABCABCABC count is 0 ABCABCABC then shift 3 ABCABCABC count is 6 ABCABCABC Clearly the period is 3. Write the code to do that. The file classcipher.vrn is in my directory for this. 3. EXTRA CREDIT The character ’ ’ (space) is most common in English text. After finding the period count the most common character for each period and recover the key by XOR’ing it with ’ ’.
I know the assignment says C, but I'd like the program to be written in Java since I don't know C.
Explanation / Answer
import java.io.*;
import java.lang.*;
import java.util.*;
public class vernamIMP
{
public static String encryptVernamCipher(String k,String inputString)
{
int n=inputString.length();
char[] outputString=new char[n];
char[] in=inputString.toCharArray();
char[] ky=k.toCharArray();
int k1;
for( k1=0;k1<inputString.length();k1++)
{
outputString[k1]=(char)(in[k1]^ky[k1]);
}
return String.valueOf(outputString);
}
public static String decryptVernamCipher(String k,String outputString)
{
char[] inputString=new char[outputString.length()];
for(int k1=0;k1<outputString.length();k1++)
{
inputString[k1]=(char)(outputString.charAt(k1)^k.charAt(k1));
}
return new String(inputString);
}
public static int findPeriod(String key)
{
int k=1;
int periodCount=0;
while(true)
{
char[] newKey=new char[k];
for(int k1=0;k1<k;k1++)
newKey[k1]=' ';
String tKey=new String(newKey)+(key.substring(0,key.length()-1));
int minL=Math.min(key.length(),tKey.length());
periodCount=0;
for(int k3=0;k3<minL;k3++)
{
if(key.charAt(k3)==tKey.charAt(k3))
{
periodCount++;
}
}
if(periodCount>0)
break;
else
{
k++;
}
}
return k;
}
public static void main(String[] args)throws Exception
{
String key;
String inputString;
String outputString;
if(args.length<3)
{
System.out.println("ARGUMENTS MISSING");
System.exit(0);
}
key=args[0];
String a=args[1];
String[] t=a.split("\.");
if(t[0].equals("input"))
{
inputString=t[1];
System.out.println("input String:"+inputString);
if(key.length()!=inputString.length())
{
int k1=0;
char[] newKey=new char[inputString.length()];
for(int k=0;k<inputString.length();k++)
{
if(k1==key.length())
k1=0;
newKey[k]=(char)(key.charAt(k1));
k1++;
}
key=new String(newKey);
}
outputString=encryptVernamCipher(key,inputString);
System.out.println("Encrypted Text:"+outputString);
System.out.println("Period:"+findPeriod(key));
}
else
{
outputString=t[1];
System.out.println("output String:"+outputString);
if(key.length()!=outputString.length())
{
int k1=0;
char[] newKey=new char[outputString.length()];
for(int k=0;k<outputString.length();k++)
{
if(k1==key.length())
k1=0;
newKey[k]=(char)(key.charAt(k1));
k1++;
}
key=new String(newKey);
}
inputString=decryptVernamCipher(key,outputString);
System.out.println("Decrypted Text:"+inputString);
System.out.println("Period:"+findPeriod(key));
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.