**The cipher_freq.txt and corpus_freq.txt are files that already have so you can
ID: 3591992 • Letter: #
Question
**The cipher_freq.txt and corpus_freq.txt are files that already have so you can just make the program to run any file but that the extension of the file can be change.
Cipher cracker Please create a program called sol_cracker.ext, where ext denotes the file extension corresponding to your choice of programming language (.py, .c, .cpp, .c++, or .java). This program reads in three files: ciphertext.txt, cipher_freq.txt, and corpus_freq.txt. The output of this program should be a file called cracked.txt. It should be the result of replacing each charact of ciphertext.txt with a capital Roman letter or a space. The correspondnce is given by the files cipher_freq.txt and corpus_freq.txt. The character in row i of cipher_freq.txt should be replaced with the letter (or space) in row i of corpus_freq.txt. (Note that for
4
this to work, both cipher_freq.txt and corpus_freq.txt must be sorted by frequency.) The result will probably not be a perfect decrypt, but the output should be close enough to cleartext to repair the remaining problems manually.
Explanation / Answer
// Compile in VS 2012, C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ciphertext
{
class yourClass
{
public static char ciphers(char ch, int key)
{
if (!char.IsLetter(ch))
{
return ch;
}
char d = char.IsUpper(ch) ? 'A' : 'a';
return (char)((((ch + key) - d) % 26) + d);
}
public static string Enciphers(string input, int key)
{
string output = string.Empty;
foreach (char ch in input)
output += ciphers(ch, key);
return output;
}
public static string Deciphers(string input, int key)
{
return Enciphers(input, 26 - key);
}
static void Main(string[] args)
{
string fileName = @"E:chegganswer22septciphertext.txt";
// Read a text file using StreamReader
using (System.IO.StreamReader sr = new System.IO.StreamReader(fileName))
{
String line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
string cipherText = Enciphers(fileName, key);
Console.WriteLine(cipherText);
Console.Write(" ");
Console.WriteLine(" All Decrypted Data");
string ts = Deciphers(cipherText, key);
Console.WriteLine(ts);
Console.Write(" ");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.