NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

/**
* Name: Leo Cohen
* Pennkey: leocohen
*
* Execution:
* java Caesar encrypt <filename> <key>
* java Caesar decrypt <filename> <key>
* java Caesar crack <encrypted_filename> <languageFrequency_filename>
*
* Program Description:
* Program can encrypt text files by shifting each letter a certain amount forward
* based on a specified key (capital letter A-Z). Program can decrypt text files by
* shifting each letter back by a specified key. Program can crack encrypted text
* files without the key when given a file with data on the letter frequency of the
* encoded language.
*
*/

public class Caesar {
public static void main(String[] args) {

// Records command line argumants and specified file
String command = args[0];
String filename = args[1];
In inStream = new In(filename);
String originalMessage = inStream.readAll();

// Executes encrypt function and prints output
if (command.equals("encrypt")) {
String charKey = args[2];
int key = (int) (charKey.charAt(0) - 'A');
String encryptedMessage = encrypt(originalMessage, key);
System.out.println(encryptedMessage);

// Executes decrypt function and prints output
} else if (command.equals("decrypt")) {
String charKey = args[2];
int key = (int) (charKey.charAt(0) - 'A');
String decryptedMessage = decrypt(originalMessage, key);
System.out.println(decryptedMessage);

// Executes crack function and prints output
} else if (command.equals("crack")) {
String letterFrequencies = args[2];
char foundKey = crack(originalMessage, letterFrequencies);
int key = (int) foundKey;

// Decrypts file with key found from crack function
String decryptedMessage = decrypt(originalMessage, key);
System.out.println(decryptedMessage);
}
}

// Takes in string and converts characters into an array of integers
public static int[] stringToSymbolArray(String str) {
str = str.toUpperCase();
int[] symbolArray = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
int letter = (int) str.charAt(i);
symbolArray[i] = letter - 'A';
}
return symbolArray;
}

// Takes in integer array and converts into a string of characters
public static String symbolArrayToString(int[] symbols) {
String message = "";
for (int i = 0; i < symbols.length; i++) {
int symbol = symbols[i] + 'A';
char letter = (char) symbol;
message += letter;
}
return message;
}

// Shifts each integer in array forward based on specified key
public static int shift(int symbol, int offset) {
int shiftedSymbol = 0;
if (0 <= symbol && symbol <= 25) {
shiftedSymbol = symbol + offset;
if (shiftedSymbol > 25) {
shiftedSymbol -= 26;
}
} else {
shiftedSymbol = symbol;
}
return shiftedSymbol;
}

// Shifts each integer in array backward based on specified key
public static int unshift(int symbol, int offset) {
int shiftedSymbol = 0;
if (0 <= symbol && symbol <= 25) {
shiftedSymbol = symbol - offset;
if (shiftedSymbol < 0) {
shiftedSymbol += 26;
}
} else {
shiftedSymbol = symbol;
}
return shiftedSymbol;
}

// Encrypts message with given key
public static String encrypt(String message, int key) {
int[] symbolArray = stringToSymbolArray(message);
int[] shiftedSymbolArray = new int[message.length()];
String encryptedMessage = "";
for (int i = 0; i < message.length(); i++) {
shiftedSymbolArray[i] = shift(symbolArray[i], key);
}
encryptedMessage = symbolArrayToString(shiftedSymbolArray);
return encryptedMessage;
}

// Decrypts message with given key
public static String decrypt(String cipher, int key) {
int[] symbolArray = stringToSymbolArray(cipher);
int[] shiftedSymbolArray = new int[cipher.length()];
String decryptedMessage = "";
for (int i = 0; i < cipher.length(); i++) {
shiftedSymbolArray[i] = unshift(symbolArray[i], key);
}
decryptedMessage = symbolArrayToString(shiftedSymbolArray);
return decryptedMessage;
}

// Takes language frequency data and records it into an array of doubles
public static double[] getDictionaryFrequencies(String filename) {
In inStream = new In(filename);
double[] dictionaryFrequencies = new double[26];
for (int i = 0; i < 26; i++) {
dictionaryFrequencies[i] = inStream.readDouble();
}
return dictionaryFrequencies;
}

// Records frequency of letters in encrypted file
public static double[] findFrequencies(int[] symbols) {
double[] frequencies = new double[26];
int numberOfLetters = 0;
for (int i = 0; i < symbols.length; i++) {
if (0 <= symbols[i] && symbols[i] <= 25) {
for (int j = 0; j < 26; j++) {
if (symbols[i] == j) {
frequencies[j]++;
}
}
numberOfLetters++;
}
}
for (int i = 0; i < frequencies.length; i++) {
frequencies[i] = frequencies[i] / numberOfLetters;
}
return frequencies;
}

/* Compares letter frequencies from encrypted file to letter frequency of
language */
public static double scoreFrequencies(double[] english, double[] currentFreqs) {
double frequencyScore = 0;
for (int i = 0; i < currentFreqs.length; i++) {
frequencyScore += Math.abs(currentFreqs[i] - english[i]);
}
return frequencyScore;
}

/* Cracks message by decrypting file with every key, and choosing the key that
results in a message that most resembles the language data provided */
public static char crack(String message, String letterFrequencies) {
double[] frequencyScores = new double[26];
for (int i = 0; i < 26; i++) {
String decryptedMessage = decrypt(message, i);
int[] symbolArray = stringToSymbolArray(decryptedMessage);
double[] dictionaryFrequencies =
getDictionaryFrequencies(letterFrequencies);
double[] frequencies = findFrequencies(symbolArray);
frequencyScores[i] =
scoreFrequencies(dictionaryFrequencies, frequencies);
}
double testScore = frequencyScores[0];
int possibleKey = 0;
for (int i = 0; i < 26; i++) {
if (frequencyScores[i] < testScore) {
testScore = frequencyScores[i];
possibleKey = i;
}
}

char foundKey = (char) possibleKey;
return foundKey;
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.