public IActionResult TextHandler(string text, string key, string EncryptOrDecrypt, string Download, string name) { if (text == null) { text = ""; } if ((key == "") || (key == null)) { key = "a"; } if (EncryptOrDecrypt == "Encrypt") { text = VigenereEncryptor.Encrypt(text, key, 0, out int _); } if (EncryptOrDecrypt == "Decrypt") { text = VigenereEncryptor.Decrypt(text, key, 0, out int _); } if (Download == "true") { if ((name == "") || (name == null)) { name = "solution.txt"; } else if ((name.Length < 4) || (name.Substring(name.Length - 4) != ".txt")) { name += ".txt"; } return(File(Encoding.UTF8.GetBytes(text), "text/plain", name)); } ViewBag.Text = text; return(View()); }
public void FileToTxtHandlerTest() { IFormFile file = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("Мама мыла раму")), 0, 100, "TestTxt", "TestTxt.txt"); var result = controller.FileHandler(file, "solution", "мама", "Decrypt", "true") as FileContentResult; byte[] bytes = result.FileContents; Assert.IsType <FileContentResult>(result); Assert.Equal("solution.txt", result.FileDownloadName); Assert.Equal(VigenereEncryptor.Decrypt("Мама мыла раму", "мама", 0, out int _), Encoding.UTF8.GetString(bytes)); Assert.Equal(@"text/plain", result.ContentType); }
public void ReturnsIteratorOfDecryptedStringsWhenParamsNotNullOrEmpty( IEnumerable <string> source, string key, IEnumerable <string> expectedResult) { //Arrange VigenereEncryptor encryptor = new VigenereEncryptor(); //Act IEnumerable <string> actualResult = encryptor.Decrypt(source, key); //Assert Assert.AreEqual(expectedResult, actualResult); }
public void VigenereDecryptTest() { string EncryptedText = "Оээ гфъбьукн ввщэшс, ъяяснямпдоп (12312?№*:!№*) унжцёывоисш ъцээф ьчура хърбъо"; Assert.Equal("Это тестовая строка, проверяющая (12312?№*:!№*) дешифрующий метод моего класса", VigenereEncryptor.Decrypt(EncryptedText, "скорпион", 0, out int _)); }
public ActionResult FileHandler(IFormFile file, string name, string key, string EncryptOrDecrypt, string download) { if (file != null) { var extension = Path.GetExtension(file.FileName); if ((key == "") || (key == null)) { key = "a"; } string ResponseText = ""; Console.WriteLine(download); if (extension == ".txt") { string text = ""; using (var reader = new StreamReader(file.OpenReadStream())) { text = reader.ReadToEnd(); } if (EncryptOrDecrypt == "Encrypt") { ResponseText = VigenereEncryptor.Encrypt(text, key, 0, out int _); } if (EncryptOrDecrypt == "Decrypt") { ResponseText = VigenereEncryptor.Decrypt(text, key, 0, out int _); } if ((name.Length < 4) || (name.Substring(name.Length - 4) != ".txt")) { name += ".txt"; } if (download == "true") { return(File(Encoding.UTF8.GetBytes(ResponseText), "text/plain", name)); } ViewBag.Text = ResponseText; return(View()); } if (extension == ".docx") { byte[] BytesResponse; using (Stream stream = file.OpenReadStream()) { using (var ms = new MemoryStream()) { stream.CopyTo(ms); var doc = new WordDocument(ms); if (EncryptOrDecrypt == "Encrypt") { doc.EncryptDecrypt(key, true); } if (EncryptOrDecrypt == "Decrypt") { doc.EncryptDecrypt(key, false); } doc.Dispose(); BytesResponse = ms.ToArray(); } if ((name == "") || (name == null)) { name = "solution.docx"; } else if ((name.Length < 5) || (name.Substring(name.Length - 5) != ".docx")) { name += ".docx"; } return(File(BytesResponse, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", name)); } } } return(RedirectToAction("Index", "File")); }