public void EncodeDecodeTest() { string original = "Give me the GEP gun"; string encoded = Cezar.Encode(original, 4); Assert.AreEqual(original, Cezar.Decode(encoded, 4)); }
public void AutoDeCryptTest() { int Shift = Cezar.AutoDeCrypt("Щфцюъ щр яхпрю ъю ыьлнъэяпфк! К юяю эяпзк! Щр мроф нырьрп млюзцф!", "word-lib.txt"); Assert.AreEqual(21, Shift); Shift = Cezar.AutoDeCrypt("Аздцр вдш едщивхзс, мзд еёдшёхввюёдчхгюъ - тзд щдчдбсгд чъжъбд ю еёюадбсгд!", "word-lib.txt"); Assert.AreEqual(11, Shift); }
public void ToCesarTest() { string s1 = Cezar.ToCesar("Как дела? Привет! Чем занимашеься?", 5); Assert.AreEqual("Пеп ийре? Фхнжйч! Ьйс метнсеэйбцд?", s1); s1 = Cezar.ToCesar("Коты не программируют! Коты гадят и разрабатывают план по захвату мира(", -6); Assert.AreEqual("Еимх зя йкиэкъжжгкншм! Еимх эъющм г къвкъыъмхьъшм йёъз йи въпьъмн жгкъ(", s1); }
public void ReturnsEncryptedWord() { string input = "CRYPTOGRAPHY"; string expected = "FUBSWRJUDSKB"; Cezar cezar = new Cezar(3); string encrypted = cezar.Encrypt(input); Assert.AreEqual(expected, encrypted); }
void makeCezar() { if (textBox.Text != "" & keyBox.Text != "" && keyWordBox.Text != "") { try { Cezar cezar = new Cezar(keyWordBox.Text, Convert.ToByte(keyBox.Text), language); alphabetBox.Text = cezar.alphabetString; codedTextBox.Text = cezar.Encrypt(textBox.Text); decodedTextBox.Text = cezar.Decrypt(codedTextBox.Text); entropyBox.Text = Entropy.Calculate(textBox.Text).ToString(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
//шифруем текст public ActionResult Encrypt(string InputText, bool Enable, string Shifte) { try { ViewBag.Text = InputText; int Shift; if (Enable) { Shift = Cezar.AutoDeCrypt(InputText, Server.MapPath("~/Library/") + "word-lib.txt"); } else { if (!Int32.TryParse(Shifte, out Shift)) { ViewBag.Text = InputText; return(View("Index")); } } string temp = ""; temp = InputText; temp = Cezar.ToCesar(temp, Shift); ViewBag.Encrypted = temp; ViewBag.Shifte = Shift; return(View("Index")); } catch (Exception) { ViewBag.Text = InputText; ViewBag.Shifte = Shifte; return(View("Index")); } }
private void filesencryptDecryptPressed(object sender, RoutedEventArgs e) { // setup outcome list outcomeScrollViewerList.Clear(); // init key, algorithm and temp fields string buttonName = ((Button)sender).Name; string userKey = fileskeyTextBox.Text.ToString(); string userInput = ""; string encrypted = ""; string decrypted = ""; string status = ""; string algorithmName = ""; Cipher algorithm; // needed cause compiler cries about algorithm not being set algorithm = new RailFence(3); // check for encryption status if (buttonName == "filesencrypt") { status = "encrypting"; } else { status = "decrypting"; } parseColumnarTranspKey(userKey); // inicialize algorithm with key switch (currentAlgorithm) { case "RAIL_FENCE": if (fileskeyTextBox.Text == "") { MessageBox.Show("Rail fence key is emtpy. Please type in something."); return; } if (validateRailfenceFields(fileskeyTextBox.Text.ToString())) { int i = int.Parse(userKey); algorithm = new RailFence(i); algorithmName = "rail fence"; } else { return; } break; case "COLUMNAR_TRANSP": if (fileskeyTextBox.Text == "") { MessageBox.Show("Columnar transp key is emtpy. Please type in something."); return; } int[] inputTab = parseColumnarTranspKey(userKey); if (validateColumnarTranspkey(inputTab)) { algorithm = new ColumnarTransposition(parseColumnarTranspKey(userKey)); algorithmName = "columnar transposition"; } else { return; } break; case "MATRIX_TRANSP": if (fileskeyTextBox.Text == "") { MessageBox.Show("Matrix transp key is emtpy. Please type in something."); return; } if (validateMatrixTransp(userKey)) { algorithm = new MatrixTransp(parseMatrixTranspKey(userKey)); } else { return; } break; case "COLUMNAR_C": if (fileskeyTextBox.Text == "") { MessageBox.Show("Matrix transp version C text input is empty. Please type in something."); return; } if (validateMatrixTranspVerCKey(userKey)) { algorithm = new ColumnarTranspositionC(userKey); } else { return; } break; case "ViGENERE": if (fileskeyTextBox.Text == "") { MessageBox.Show("Winegret text input is empty. Please type in something."); return; } if (validateVinegretKey(userKey)) { algorithm = new Vigenere(userKey); } else { return; } break; case "CEZAR": if (fileskeyTextBox.Text == "") { MessageBox.Show("Cezar text input is empty. Please type in something."); return; } if (validateCezarKey(userKey)) { int i = int.Parse(userKey); algorithm = new Cezar(i); } else { return; } break; default: return; } // depending on the status move through input list and update outcome listsdfafsdafsadfsad string currentDate = DateTime.Now.ToString().Replace(':', ' ').Replace('/', ' '); if (status == "encrypting") { if (listOfLines.Count > textFileLinesAmountBreakPoint) { TextWriter tw = new StreamWriter("EncryptedListOfWords - " + currentAlgorithm + " - " + currentDate + ".txt"); string outcome = ""; foreach (string s in listOfLines) { outcome = algorithm.Encrypt(s); tw.WriteLine(outcome); } tw.Close(); } else { foreach (string line in userInputScrollList) { userInput = line; encrypted = algorithm.Encrypt(line); outcomeScrollViewerList.Add(encrypted); } } } else { if (listOfLines.Count > textFileLinesAmountBreakPoint) { TextWriter tw = new StreamWriter("DecryptedListOfWords - " + currentAlgorithm + " - " + currentDate + ".txt"); string outcome = ""; foreach (string s in listOfLines) { outcome = algorithm.Decrypt(s); tw.WriteLine(outcome); } tw.Close(); } else { foreach (string line in userInputScrollList) { userInput = line; decrypted = algorithm.Decrypt(line); outcomeScrollViewerList.Add(decrypted); } } } if (listOfLines.Count > textFileLinesAmountBreakPoint) { MessageBox.Show("Check app's folder for outcome of this operation. "); outcomeScrollViewerList.Add("Check your app's folder for"); if (status == "encrypting") { outcomeScrollViewerList.Add("EncryptedListOfWords - "); } else { outcomeScrollViewerList.Add("DecryptedListOfWords - "); } outcomeScrollViewerList.Add(currentAlgorithm); outcomeScrollViewerList.Add(" - "); outcomeScrollViewerList.Add(currentDate); outcomeScrollViewerList.Add(".txt"); } }
private void encryptDecryptPressed(object sender, RoutedEventArgs e) { string buttonName = ((Button)sender).Name; string userInput = mTextBox.Text.ToString(); string userKey = keyTextBox.Text.ToString(); string encrypted = ""; string decrypted = ""; bool normalDialog = true; switch (currentAlgorithm) { case "RAIL_FENCE": if (mTextBox.Text == "") { MessageBox.Show("Rail fence text input is empty. Please type in something."); return; } if (validateRailfenceFields(keyTextBox.Text.ToString())) { int i = int.Parse(userKey); rf = new RailFence(i); encrypted = rf.Encrypt(userInput); decrypted = rf.Decrypt(userInput); } break; case "COLUMNAR_TRANSP": if (mTextBox.Text == "") { MessageBox.Show("Columanr transp text input is empty. Please type in something."); return; } int[] inputTab = parseColumnarTranspKey(userKey); if (validateColumnarTranspkey(inputTab)) { ct = new ColumnarTransposition(parseColumnarTranspKey(userKey)); encrypted = ct.Encrypt(userInput); decrypted = ct.Decrypt(userInput); } else { return; } break; case "MATRIX_TRANSP": if (mTextBox.Text == "") { MessageBox.Show("Matrix transp text input is empty. Please type in something."); return; } if (validateMatrixTransp(userKey)) { mt = new MatrixTransp(parseMatrixTranspKey(userKey)); encrypted = mt.Encrypt(userInput); decrypted = mt.Decrypt(userInput); } else { return; } break; case "COLUMNAR_C": if (mTextBox.Text == "") { MessageBox.Show("Matrix transp version C text input is empty. Please type in something."); return; } if (validateMatrixTranspVerCKey(userKey) && validateMatrixTranspVerCWord(userInput)) { ctc = new ColumnarTranspositionC(userKey); encrypted = ctc.Encrypt(userInput); decrypted = ctc.Decrypt(userInput); } else { return; } break; case "ViGENERE": if (mTextBox.Text == "") { MessageBox.Show("Winegret text input is empty. Please type in something."); return; } if (validateVinegretKey(userKey) && validateVinegretWord(userInput)) { vig = new Vigenere(userKey); encrypted = vig.Encrypt(userInput); decrypted = vig.Decrypt(userInput); } else { return; } break; case "CEZAR": if (mTextBox.Text == "") { MessageBox.Show("Cezar text input is empty. Please type in something."); return; } if (validateCezarKey(userKey) && validateCezarWord(userInput)) { int i = int.Parse(userKey); cz = new Cezar(i); encrypted = cz.Encrypt(userInput); decrypted = cz.Decrypt(userInput); } else { return; } break; case "SYNC": if (syncFileName.Content.ToString() == " ") { MessageBox.Show("SYNC file input is empty. Please type in something."); return; } if (keyTextBox.Text == "") { MessageBox.Show("SYNC key text input is empty. Please type in something."); return; } if (!syncKeyGenerated) { MessageBox.Show("SYNC key needs to be generated first, please press run and then stop button before proceeding."); return; } normalDialog = false; synchronousStreamCipher = new SynchronousStreamCipher(lsfrGen.GetSequence()); TextWriter tw = new StreamWriter("LFSR KEY USED.txt"); List <bool> listOfBools = lsfrGen.GetSequence(); char boolOutcome = ' '; string boolOutcomeCollection = ""; int ij = 0; foreach (bool b in listOfBools) { if (b == true) { boolOutcome = '1'; } else { boolOutcome = '0'; } boolOutcomeCollection += boolOutcome; ij++; if (ij > 100) { ij = 0; tw.WriteLine(boolOutcomeCollection); boolOutcomeCollection = ""; } } tw.WriteLine(boolOutcomeCollection); tw.Close(); if (buttonName == "encrypt") { synchronousStreamCipher.Encrypt(syncFileName.Content.ToString()); } else { synchronousStreamCipher.Decrypt(syncFileName.Content.ToString()); } MessageBox.Show("Encrypted/decrypted file is located in folder where your .exe file is ( most probably bin/debug ). You will also find text file that contains LFSR key in it there."); break; case "DES": if (syncFileName.Content.ToString() == " ") { MessageBox.Show("DES file input is empty. Please type in something."); return; } if (keyTextBox.Text == "") { MessageBox.Show("DES key text input is empty. Please type in something."); return; } if (!validateDesKey(userKey)) { return; } des = new DES(desKeyParsing(userKey)); if (buttonName == "encrypt") { des.EncryptFile(syncFileName.Content.ToString()); } else { des.DecryptFile(syncFileName.Content.ToString()); } MessageBox.Show("Encrypted/decrypted file is located in folder where your .exe file is ( most probably bin/debug )."); break; default: break; } int length = encrypted.Length; outcomeLabel.FontSize = 30; if (length > 10) { outcomeLabel.FontSize = 20; } if (length > 20) { outcomeLabel.FontSize = 15; } if (normalDialog) { if (buttonName == "encrypt") { if (encrypted != "") { outcomeTypeLabel.Content = "Encrypted:"; } outcomeLabel.Content = encrypted; } else { if (decrypted != "") { outcomeTypeLabel.Content = "Decrypted:"; } outcomeLabel.Content = decrypted; } } else { } }
static void Main(string[] args) { int alg, opt, n = 0; Crypt C = null; // meniu pentru alegerea algoritmului de criptare do { Console.WriteLine("Alegeti algoritmul: "); Console.WriteLine("1. Cezar "); Console.WriteLine("2. PlusN "); Console.WriteLine("3. Monoalfabetica "); Console.WriteLine("4. Polialfabetica "); alg = Convert.ToInt32(Console.ReadLine()); if (alg == 2 || alg == 4) { Console.WriteLine("Introduceti numarul cheie: "); n = Convert.ToInt32(Console.ReadLine()); } switch (alg) { case 1: C = new Cezar(); break; case 2: C = new PlusN(n); break; case 3: C = new Monoalfabetica(); break; case 4: C = new Vigenere(n); break; } } while (alg <= 0 || alg > 4); // meniu pentru lucrarea cu algoritmul alesa Console.WriteLine("Alegeti optiunea: "); Console.WriteLine("1. Encriptare "); Console.WriteLine("2. Decriptare "); Console.WriteLine("3. Criptoanaliza "); opt = Convert.ToInt32(Console.ReadLine()); string text = null, filepath = @""; if (opt == 1) { text = Fisier.Citire(filepath + "text_clar.txt"); } else if (opt == 2 || opt == 3) { text = Fisier.Citire(filepath + "text_criptat.txt"); } switch (opt) { case 1: text = C.Encriptare(text); break; case 2: text = C.Decriptare(text); break; case 3: text = C.Criptoanaliza(text); break; default: Console.WriteLine("Optiune introdusa gresita!"); break; } if (opt == 1) { Fisier.Scriere(filepath + "text_criptat.txt", text); } else if (opt == 2 || opt == 3) { Fisier.Scriere(filepath + "text_clar.txt", text); } }