Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.io.File;
- import java.io.IOException;
- import java.util.Arrays;
- /**
- * Handles basic caesar encyrpyion
- *
- * @author Mark Davis
- * @version 2/10/2014
- */
- public class Encryption
- {
- private static int shift;
- private static String message, newMessage;
- static String newAlpha;
- static int[] plainfrequencies = new int[26];
- static int[] cipherfrequencies = new int[26];
- static String encrypted, translated;
- static final String alphabet = "abcdefghijklmnopqrstuvwxyz";
- public Encryption()
- {
- }
- public static void readIn() throws IOException
- {
- plainfrequencies = getFrequency("plaintext");
- }
- public static void encryptedIn() throws IOException
- {
- cipherfrequencies = getFrequency("ciphertext");
- }
- public static void translate() throws IOException
- {
- encrypted = readFromFile("secretmessage");
- String plainalpha = generateStrings(plainfrequencies);
- String cipheralpha = generateStrings(cipherfrequencies);
- for (char c: encrypted.toCharArray())
- {
- cipheralpha.indexOf(c) //START HERE
- }
- }
- public static String generateStrings(int[] array)
- {
- char[] chararray = new char[26];
- for (int i = 0; (i <= 25); i++)
- {
- int index = maxIndexOfArray(array);
- chararray[i] = alphabet.charAt(index);
- array[index] = 0;
- }
- return new String(chararray);
- }
- public static String getTranslated()
- {
- return translated;
- }
- public static String readFromFile(String filename) throws IOException
- {
- File fileName = new File(filename + ".txt");
- Scanner inFile = new Scanner(fileName);
- Scanner in = new Scanner(System.in);
- String text = "";
- while(inFile.hasNextLine())
- {
- text += inFile.nextLine();
- }
- text = text.replace(" ", "");
- return text;
- }
- public static int[] getFrequency(String filename) throws IOException
- {
- String text = readFromFile(filename);
- int[] frequencies = new int[26];
- for(int i = 0; (i < text.length()); i++)
- {
- char temp = text.charAt(i);
- if (canBeFoundIn(uncapitalize(temp), alphabet))
- frequencies[alphabet.indexOf(uncapitalize(temp))]++;
- }
- return frequencies;
- }
- public static boolean canBeFoundIn(char c, String s)
- {
- boolean test = false;
- char[] lowercases = s.toLowerCase().toCharArray();
- for (char e: lowercases)
- {
- if (c == e)
- test = true;
- }
- return test;
- }
- public static char uncapitalize(char d)
- {
- String dummy = "a" + d;
- dummy = dummy.toLowerCase();
- return dummy.charAt(1);
- }
- public static int maxIndexOfArray(int[] a)
- {
- int e = Integer.MIN_VALUE;
- int index = 0;
- for (int i = 0; (i > a.length); i++)
- {
- if (a[i] > e)
- {
- index = i;
- e = a[i];
- }
- }
- return index;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement