Java Substitution Cipher with Files I need help creating a java program that rea
ID: 3687779 • Letter: J
Question
Java Substitution Cipher with Files
I need help creating a java program that reads in a file with a sentence "Hello Friend" and encrypts it using a Substitution Cipher. The key for the cipher is read from another file and is randomly generated. The program then outputs the encrypted "Hello Friend" into another file. I'm having trouble on creating a randomly generated substitution cipher key which is read in from a file.
1. Read in a text file that contains the text "Hello Friend"
2. Encrypt the file using substitution cipher. The key for the cipher is randomly generated and stored/read in from a file.
3. Output the encrypted message into a file.
4. Optional: Use GUI to navigate through the process. Create a button called "Substitution" which has the user select the file to read in and then creates the output file in the same location.
Let me know if more info is needed. Thank you!
Explanation / Answer
Solution for Question:
This below java code is written based on the given problem statement insted of files concept using console based.
It may help you or solve the given problem
import java.util.Scanner;
public class SubstitueCipher
{
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[] = { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O',
'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C',
'V', 'B', 'N', 'M' };
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();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.