Advertisement
markd315

Untitled

Mar 2nd, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.Arrays;
  6.  
  7. /**
  8.  * Handles basic caesar encyrpyion
  9.  *
  10.  * @author Mark Davis
  11.  * @version 2/10/2014
  12.  */
  13. public class Encryption
  14. {
  15.     private static int shift;
  16.     private static String message, newMessage;
  17.     static String newAlpha;
  18.     static int[] plainfrequencies = new int[26];
  19.     static int[] cipherfrequencies = new int[26];
  20.     static String encrypted, translated;
  21.     static final String alphabet = "abcdefghijklmnopqrstuvwxyz";
  22.     public Encryption()
  23.     {
  24.     }
  25.  
  26.     public static void readIn() throws IOException
  27.     {
  28.         plainfrequencies = getFrequency("plaintext");
  29.     }
  30.  
  31.     public static void encryptedIn() throws IOException
  32.     {  
  33.         cipherfrequencies = getFrequency("ciphertext");
  34.     }
  35.  
  36.     public static void translate() throws IOException
  37.     {
  38.         encrypted = readFromFile("secretmessage");
  39.         String plainalpha = generateStrings(plainfrequencies);
  40.         String cipheralpha = generateStrings(cipherfrequencies);
  41.         for (char c: encrypted.toCharArray())
  42.         {
  43.                 cipheralpha.indexOf(c) //START HERE
  44.         }
  45.     }
  46.     public static String generateStrings(int[] array)
  47.     {
  48.         char[] chararray = new char[26];
  49.         for (int i = 0; (i <= 25); i++)
  50.         {
  51.             int index = maxIndexOfArray(array);
  52.             chararray[i] = alphabet.charAt(index);
  53.             array[index] = 0;
  54.         }
  55.         return new String(chararray);
  56.     }
  57.     public static String getTranslated()
  58.     {
  59.         return translated;
  60.     }
  61.  
  62.     public static String readFromFile(String filename) throws IOException
  63.     {
  64.         File fileName = new File(filename + ".txt");
  65.         Scanner inFile = new Scanner(fileName);
  66.         Scanner in = new Scanner(System.in);
  67.         String text = "";
  68.         while(inFile.hasNextLine())
  69.         {
  70.             text += inFile.nextLine();
  71.         }
  72.         text = text.replace(" ", "");
  73.         return text;
  74.     }
  75.  
  76.     public static int[] getFrequency(String filename) throws IOException
  77.     {
  78.         String text = readFromFile(filename);
  79.         int[] frequencies = new int[26];
  80.         for(int i = 0; (i < text.length()); i++)
  81.         {
  82.             char temp = text.charAt(i);
  83.             if (canBeFoundIn(uncapitalize(temp), alphabet))
  84.                 frequencies[alphabet.indexOf(uncapitalize(temp))]++;
  85.         }
  86.         return frequencies;
  87.     }
  88.  
  89.     public static boolean canBeFoundIn(char c, String s)
  90.     {
  91.         boolean test = false;
  92.         char[] lowercases = s.toLowerCase().toCharArray();
  93.         for (char e: lowercases)
  94.         {
  95.             if (c == e)
  96.                 test = true;
  97.         }
  98.         return test;
  99.     }
  100.  
  101.     public static char uncapitalize(char d)
  102.     {
  103.         String dummy = "a" + d;
  104.         dummy = dummy.toLowerCase();
  105.         return dummy.charAt(1);
  106.     }
  107.  
  108.     public static int maxIndexOfArray(int[] a)
  109.     {
  110.         int e = Integer.MIN_VALUE;
  111.         int index = 0;
  112.         for (int i = 0; (i > a.length); i++)
  113.         {
  114.             if (a[i] > e)
  115.             {
  116.                 index = i;
  117.                 e = a[i];
  118.             }
  119.         }
  120.         return index;
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement