I need to create a program that accepts a keyword from a .txt and then uses the keyword to create a cryptic alphabet. The alphabet I need takes the non-redundant letters of the key and places them at the front of the alphabet, then reverses the remaining alphabet. So if my keyword is "KEY", the new alphabet would be KEYZXWVUTSRQPONMLJIHGFDCBA. I then have to use the cipher alphabet to encrypt a sentence from another .txt.
I'm guessing I would need to use a bufferedReader instead of FileReader? Also, I hard coded the cipher alphabet but can't figure out how to accept a key from .txt to determine the cipher (like if the key changes, for example).
If someone could just give me a pointer or two on where to start, it would be greatly appreciated.
Here is some hard code I used:
package com.encryption.encryptionstring;
import java.util.*;
import java.io.*;
import java.nio.*;
public class MonoalphabeticEncryption
{
public static char p[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z' };
public static char ch[] = { 'T', 'R', 'O', 'Y', 'N', 'L', 'I', 'E', 'Z',
'X', 'W', 'V', 'U', 'S', 'Q', 'P', 'M', 'K', 'J', 'H', 'G', 'F',
'D', 'C', 'B', 'A' };
public static String doEncryption(String s)
{
char c[] = new char[(s.length())];
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j < 26; j++)
{
if (p[j] == s.charAt(i))
{
c[i] = ch[j];
break;
}
}
}
return (new String(c));
}
public static String doDecryption(String s)
{
char p1[] = new char[(s.length())];
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j < 26; j++)
{
if (ch[j] == s.charAt(i))
{
p1[i] = p[j];
break;
}
}
}
return (new String(p1));
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the message: ");
String en = doEncryption(sc.next().toLowerCase());
System.out.println("Encrypted message: " + en);
System.out.println("Decrypted message: " + doDecryption(en));
sc.close();
}
}
EDIT: I started making updates per Z. Bagley's suggestion. Here is what I have so far, an alphabet array, accepted my input.txt and keyword.txt to arrays:
package com.encryption.encryptionstring;
import java.util.*;
import java.io.*;
import java.nio.*;
public class MonoalphabeticEncryption
{
char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); //is this a good alphabet array? How do I compare one array to another?
private String readFileKeyword(String file) throws IOException
{
FileWriter fileOutput= new FileWriter("output.txt");
BufferedReader readerKeyword = new BufferedReader(new FileReader ("keyword.txt"));
String lineKeyword = null;
StringBuilder stringBuilderKeyword = new StringBuilder();
String lsKeyword = System.getProperty("line.separator");
try
{
while((lineKeyword = readerKeyword.readLine()) != null)
{
stringBuilderKeyword.append(lineKeyword);
stringBuilderKeyword.append(lsKeyword);
System.out.println(fileOutput);//will this print text from keyword.txt to output.txt?
}
return stringBuilderKeyword.toString();
}
finally
{
readerKeyword.close();
}
}
private String readFileInput(String file) throws IOException
{
BufferedReader readerInput = new BufferedReader(new FileReader ("input.txt"));
String lineInput = null;
StringBuilder stringBuilderInput = new StringBuilder();
String lsInput = System.getProperty("line.separator");
try
{
while((lineInput = readerInput.readLine()) != null)
{
stringBuilderInput.append(lineInput);
stringBuilderInput.append(lsInput);
System.out.println(stringBuilderInput); //will this print text from input.txt?
}
return stringBuilderInput.toString();
}
finally
{
readerInput.close();
}
}
}