Hi, my friend asked me to make him a very simple program that could generate an encryption substitution code, enter your own code, and encrypt and decrypt a text file. So, i started to make the programs... and eventually I got them all to work. But I had a few problems, and I wanted to know if there were any easier or more efficient ways for me to do some things I did. This is a low priority request, as the programs do work just fine. I just wish to better my habits if anything I did is a bad practice. Anyway, any advice is appreciated. Thanks. :)
CryptGen - Generates a random substitution code and saves to a file.
CryptMaker - Allows you to enter a substitution code and saves it to a file.Code:import java.util.Random;
import java.io.*;
public class CryptGen
{
public static void main(String[] args)throws IOException
{
Random gen = new Random();
PrintWriter write = new PrintWriter(new File("CryptCode.txt"));
char[] chars = new char[] {'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',
'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'};
char[] cryptList = new char[52];
int tracker = 0;
while(tracker < 52)
{
int num = gen.nextInt(52);
if(cryptList[num] == '\u0000')
{
cryptList[num] = chars[tracker];
tracker++;
}
}
for(int i = 0; i < 52; i++)
{
write.println(chars[i] + " " + cryptList[i]);
}
write.close();
}
}
Encrypt - Encrypts the plain text.Code:import java.util.Scanner;
import java.io.*;
public class CryptMaker
{
public static void main(String[] args)throws IOException
{
Scanner read = new Scanner(System.in);
PrintWriter write = new PrintWriter(new File("CryptCode.txt"));
char[] chars = new char[] {'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',
'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'};
char[] cryptList = new char[52];
for(int i = 0; i < 52; i++)
{
System.out.print(chars[i] + ": ");
cryptList[i] = read.nextLine().charAt(0);
}
for(int i = 0; i < 52; i++)
{
write.println(chars[i] + " " + cryptList[i]);
}
write.close();
}
}
Decrypt - Decrypts the cipher text.Code:import java.util.*;
import java.io.*;
public class Encrypt
{
public static void main(String[] args)throws IOException
{
Scanner read = new Scanner(new File("PlainText.txt"));
PrintWriter write = new PrintWriter(new File("CipherText.txt"));
String word;
char chars[];
while(read.hasNext())
{
word = read.next();
int length = word.length();
chars = new char[length];
for(int i = 0; i < length; i++)
{
chars[i] = word.charAt(i);
}
chars = Substitution(chars, length);
chars = Transposition(chars, length);
word = "";
for(int i = 0; i < length; i++)
{
word = word + chars[i];
}
write.print(word + " ");
//System.out.print(word + " ");
}
write.close();
}
public static char[] Transposition(char[] chars, int length)
{
char transposed[] = new char[length];
for(int i = 0; i < length && i+2 <= length; i+=2)
{
transposed[i] = chars[i+1];
transposed[i+1] = chars[i];
}
return transposed;
}
public static char[] Substitution(char[] chars, int length)throws IOException
{
Scanner getCode = new Scanner(new File("CryptCode.txt"));
char plainChars[] = new char[52];
char cipherChars[] = new char[52];
for(int i = 0; i < 52; i++)
{
String line = getCode.nextLine();
plainChars[i] = line.charAt(0);
cipherChars[i] = line.charAt(2);
}
for(int i = 0; i < length; i++)
{
for(int j = 0; j < 52; j++)
{
if(chars[i] == plainChars[j])
{
chars[i] = cipherChars[j];
break;
}
}
}
return chars;
}
}
Code:import java.util.*;
import java.io.*;
public class Decrypt
{
public static void main(String[] args)throws IOException
{
Scanner read = new Scanner(new File("CipherText.txt"));
PrintWriter write = new PrintWriter(new File("PlainText.txt"));
String word;
char chars[];
while(read.hasNext())
{
word = read.next();
int length = word.length();
chars = new char[length];
for(int i = 0; i < length; i++)
{
chars[i] = word.charAt(i);
}
chars = Substitution(chars, length);
chars = Transposition(chars, length);
word = "";
for(int i = 0; i < length; i++)
{
word = word + chars[i];
}
write.print(word + " ");
//System.out.print(word + " ");
}
write.close();
}
public static char[] Transposition(char[] chars, int length)
{
char transposed[] = new char[length];
for(int i = 0; i < length && i+2 <= length; i+=2)
{
transposed[i] = chars[i+1];
transposed[i+1] = chars[i];
}
return transposed;
}
public static char[] Substitution(char[] chars, int length)throws IOException
{
Scanner getCode = new Scanner(new File("CryptCode.txt"));
char plainChars[] = new char[52];
char cipherChars[] = new char[52];
for(int i = 0; i < 52; i++)
{
String line = getCode.nextLine();
plainChars[i] = line.charAt(0);
cipherChars[i] = line.charAt(2);
}
for(int i = 0; i < length; i++)
{
for(int j = 0; j < 52; j++)
{
if(chars[i] == cipherChars[j])
{
chars[i] = plainChars[j];
break;
}
}
}
return chars;
}
}

