public void DecryptOneCharD() { char[] alphabet = Caesar.GetAlphabet(); string cipherText = Caesar.Decrypt(3, "d"); Assert.AreEqual("a", cipherText, "Error in decrypting a single character at beginning of alphabet"); }
public void DecryptAndThenDecryptASingleWordFollowedBySpace() { char[] alphabet = Caesar.GetAlphabet(); string decryptedText = Caesar.Decrypt(3, Caesar.Encrypt(3, "What ")); Assert.AreEqual("what ", decryptedText, "Error in encrypting and then decrypting a single word followed by a space"); }
public void DecryptAndThenDecryptALongPhrase() { char[] alphabet = Caesar.GetAlphabet(); string decryptedText = Caesar.Decrypt(3, Caesar.Encrypt(3, "What is the square root of a duck")); Assert.AreEqual("what is the square root of a duck", decryptedText, "Error in encrypting and then decrypting a long phrase"); }
private void DecryptButton_Click(object sender, EventArgs e) { if (EncryptedText.TextLength < 1) { return; } switch (AlgorithmSelect.SelectedIndex) { case 0: var caesar = new Caesar((int)caesarShift.Value, string25Key.Text); NormalText.Text = caesar.Decrypt(EncryptedText.Text); break; case 1: var playfaire = new Playfair(string25Key.Text); NormalText.Text = playfaire.Decrypt(EncryptedText.Text); break; case 2: var rsa = new RSA(rsaPublicText.Text, rsaPrivateText.Text); NormalText.Text = rsa.Decrypt(EncryptedText.Text); break; default: break; } }
public void DecryptAndThenDecryptOneChar() { char[] alphabet = Caesar.GetAlphabet(); string decryptedText = Caesar.Decrypt(3, Caesar.Encrypt(3, "d")); Assert.AreEqual("d", decryptedText, "Error in encrypting and then decrypting a single character at beginning of alphabet"); }
private static void Decrypt() { string cipherText = ReadFile(); string plainText = Caesar.Decrypt(_key, cipherText); WriteFile(plainText); }
public void EncryptDecryptBasic() { Caesar cipher = new Caesar(15); var intermediaryText = cipher.Encrypt(TestString); Assert.AreNotEqual(TestString, intermediaryText); var plainText = cipher.Decrypt(intermediaryText); Assert.AreEqual(TestString, plainText); }
static void Main(string[] args) { ICryptService service = new Caesar(); string cr = service.Crypt("Hello world", "1"); Console.WriteLine(cr); string dcr = service.Decrypt(cr, "2"); Console.WriteLine(dcr); }
public IActionResult Post([FromBody] EncryptedMessage message) { var alphabet = message.Alphabet; var encryptedText = message.EncryptedText; Caesar cipher = new Caesar(alphabet); message.EncryptedText = cipher.Decrypt(encryptedText); return(Json(message)); }
public void CipherEncryptsThenDecryptsToSamePhrase() { string phrase = "All Tests Work"; int shift = 5; Caesar caesar = new Caesar(); string actualResult = caesar.Decrypt(caesar.Encrypt(phrase, shift), shift); Assert.AreEqual(phrase, actualResult); }
public void Decrypt_Symbol() { char result = Caesar.Decrypt('в', 1); Assert.AreEqual(result, 'б'); result = Caesar.Decrypt('Е', 2); Assert.AreEqual(result, 'Г'); result = Caesar.Decrypt('F', 2); Assert.AreEqual(result, 'D'); }
public void CipherCorrectlyDecrypts() { string phrase = "CzggjFdoot"; int shift = 21; string expectedResult = "HelloKitty"; Caesar caesar = new Caesar(); string actualResult = caesar.Decrypt(phrase, shift); Assert.AreEqual(expectedResult, actualResult); }
public void CipherIgnoresNonLetterCharacters() { string phrase = "J3110 Y0t1f#$`)"; int shift = -24; string expectedResult = "H3110 W0r1d#$`)"; Caesar caesar = new Caesar(); string actualResult = caesar.Decrypt(phrase, shift); Assert.AreEqual(expectedResult, actualResult); }
public void netWork() //main network function { while (Console.KeyAvailable) //while key is available loop start { var key = Console.ReadKey(true); //get actual key switch (key.Key) //switch for keyboard controls { case ConsoleKey.Enter: byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(encyptor.Encrypt(message)); //encoding message to ascii + encrypting nwStream.Write(bytesToSend, 0, bytesToSend.Length); //sending a message to server ClearCurrentConsoleLine(); //clearing a line for message Console.WriteLine("You:" + message + " | " + DateTime.Now.ToLongTimeString()); message = ""; //clearing a string Console.Write("Your message:" + message); //drawing type message break; case ConsoleKey.Backspace: //if backspace was pressed if (message.Length > 0) { message = message.Remove(message.Length - 1); //remove last char from string ClearCurrentConsoleLine(); //clearing actual line Console.Write("Your message:" + message); //drawing this key } break; default: //if key was pressed message += key.KeyChar; Console.Write(key.KeyChar); break; } } if (nwStream.DataAvailable == true) //if nwStream received any data { byte[] bytesToRead = new byte[client.ReceiveBufferSize]; //creating data buffer int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize); //reading data from buffer if (Encoding.ASCII.GetString(bytesToRead, 0, bytesRead).Contains("$#@!disconnect")) //if found special command to disconnect { client.Close(); ////////////////////// nwStream.Close(); //closing connection Console.WriteLine("Server disconnected you from server| reason: AFK"); //writing log to user } else { ClearCurrentConsoleLine(); Console.WriteLine(Encoding.ASCII.GetString(bytesToRead, 0, bytesRead)); Console.WriteLine(encyptor.Decrypt(Encoding.ASCII.GetString(bytesToRead, 0, bytesRead)) + " | " + DateTime.Now.ToLongTimeString());//Writing data Console.Write("Your message:" + message); } } }
private string unnamedFunc(string sequence, double[] freqs) { double[] allChiQuadrats = new double[_alphabet.Length]; Caesar ceaser = new Caesar(); for (int i = 0; i < _alphabet.Length; i++) { string temp = sequence; double tempChiSum = 0.0; var sequenceOffset = ceaser.Decrypt(temp, i.ToString()).ToList(); var currentFreq = _alphabetDic.ToDictionary(t => t.Key, t => 0.0); var lettersCount = _alphabetDic.ToDictionary(t => t.Key, t => 0); foreach (var letter in sequenceOffset) { lettersCount[letter.ToString()]++; } foreach (var key in lettersCount.Keys) { currentFreq[key] = Convert.ToDouble(lettersCount[key]) / sequenceOffset.Count; } foreach (var key in currentFreq.Keys) { double tableFreq = _alphabetDic[key] / 100; tempChiSum += Math.Pow(currentFreq[key] - tableFreq, 2) / tableFreq; } allChiQuadrats[i] = tempChiSum; } int shift = 0; double min = double.MaxValue; for (int i = 0; i < allChiQuadrats.Length; i++) { if (allChiQuadrats[i] < min) { min = allChiQuadrats[i]; shift = i; } } return(_alphabet[shift].ToString()); }
static void Main(string[] args) { string[] strings = { "abcdefghijklmnopqrstuvwxyz", "This is a string", "This is also a string", "Here we change some text", "Text some change we here", "adding punctuation!.?" }; foreach (string str in strings) { Console.WriteLine(str); foreach (char letter in str) { Console.Write(Caesar.Encrypt(letter, 3)); } Console.Write("\n"); } string string2 = "defghijklmnopqrstuvwxyzabc"; foreach (char c in string2) { Console.Write(Caesar.Decrypt(c, 3)); } Console.ReadLine(); }
private void CaesarDecrypt_Click(object sender, EventArgs e) { CaesarOutput.Text = Caesar.Decrypt(CaesarInput.Text, Convert.ToInt32(shiftNumber.Value)); }
static void Main(string[] args) { string UserChoice; int UserKey = 0; string InFile; string input = ""; string output = ""; string OutFile; Caesar caesar = new Caesar(); Console.WriteLine("Welcome! Would you like to encrypt or decrypt your text file?"); Console.WriteLine("E/D..."); UserChoice = Console.ReadLine(); // TODO: Make sure E or D was acutually used. Console.WriteLine("Please enter the Caeser Key..."); UserKey = Convert.ToInt32(Console.ReadLine()); // TODO: "Would be nice" Loop here until we get a valid input Console.WriteLine("Please enter input text file (with .txt)"); InFile = Console.ReadLine(); try { input = System.IO.File.ReadAllText(Directory.GetCurrentDirectory() + "\\" + InFile); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine("Please enter output text file (with .txt)"); OutFile = Console.ReadLine(); if (UserChoice[0] == 'E' || UserChoice[0] == 'e') { output = caesar.Encrypt(input, UserKey); try { System.IO.File.WriteAllText(Directory.GetCurrentDirectory() + "\\" + OutFile, output); Console.WriteLine(InFile + " encrypted and stored in " + OutFile); } catch (Exception e) { Console.WriteLine(e.Message); } } if (UserChoice[0] == 'D' || UserChoice[0] == 'd') { output = caesar.Decrypt(input, UserKey); try { System.IO.File.WriteAllText(Directory.GetCurrentDirectory() + "\\" + OutFile, output); Console.WriteLine(InFile + " decrypted and stored in " + OutFile); } catch (Exception e) { Console.WriteLine(e.Message); } } Analyzer analyzer = new Analyzer(); // Commented out for debugging //analyzer.TestScore(); //"Press any key..." Console.ReadKey(); }
private static void CrackCipher(string cipherText) { // Step 1: Find the most common letters in the ciphertext foreach (char cipherLetter in cipherText) { for (int i = 0; i < commonLetters.Length; i++) { if (cipherLetter == commonLetters[i]) { (commonLetterCount[i])++; } } } int englishIndex = -1; // iterative index through english letter array int cipherIndex = 0; // index corresponding to the index of the current cipher letter being tested int matchCount = 0; // count for how many words match the list of common english words int key = 0; // The key to use to decrypt while (matchCount < 3 && englishIndex < 25) { // Step 2: Find the key that makes the most common ciphertext letter // Become equal to the most common english letter. cipherIndex = 0; englishIndex++; for (int i = 0; i < commonLetterCount.Length; i++) { if (commonLetterCount[i] > commonLetterCount[cipherIndex] && commonLetterChecked[i] == false) { cipherIndex = i; } } key = FindKey(englishIndex, cipherIndex); // Step 3: compare plainText to most common words, and if more than 3 // words match, assume it is correct string plainText = Caesar.Decrypt(key, cipherText); matchCount = 0; foreach (string plainWord in plainText.Split(' ')) { foreach (string commonWord in commonWords) { if (plainWord == commonWord) { matchCount++; if (matchCount > 3) { break; } } } if (matchCount > 3) { break; } } commonLetterChecked[cipherIndex] = true; } if (matchCount > 3) { string plainText = Caesar.Decrypt(key, cipherText); WriteFile(plainText, outputFile); Console.WriteLine("Plain Text written to: {0}", outputFile); Console.WriteLine("The decryption key is: {0}", key); } else { Console.WriteLine("This algorithm could not crack the text. Are you sure it's english?"); } }
private void Button_Click_1(object sender, RoutedEventArgs e) { ICryptService src = new Caesar(); text.Text = src.Decrypt(crtext.Text, key.Text); }
private void EncryptClick(object sender, EventArgs e) { CryptLibrary.ICryptService ser = new Caesar(); InputText.Text = ser.Decrypt(InputText.Text, KeyInput.Text); }