public void TextToTextHandlerTest() { var result = controller.TextHandler("Мама мыла раму", "мама", "Encrypt", "", "") as ViewResult; Assert.IsType <ViewResult>(result); Assert.Equal(VigenereEncryptor.Encrypt("Мама мыла раму", "мама", 0, out int _), result?.ViewData["Text"]); }
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()); }
private static ErrorMsg TryEncryptDocx(string filePath, string keyWord, string guid, VigenereEncryptor.Operation op, out string rawResult) { rawResult = null; //used for preview try { Document docx = new Document(filePath); VigenereEncryptor ve = new VigenereEncryptor(keyWord, op); for (int i = 1; i < docx.Sections[0].Body.Paragraphs.Count; i++) { string ciph = ve.Encrypt(docx.Sections[0].Body.Paragraphs[i].GetText()); docx.Sections[0].Body.Paragraphs[i].Runs[0].Text = ciph; if (ciph == null) { return(ErrorMsg.InvalidKeyWord); } rawResult += ciph + "\n"; } OoxmlSaveOptions opt = new OoxmlSaveOptions(SaveFormat.Docx); opt.Compliance = OoxmlCompliance.Ecma376_2006; docx.Save(WebApiApplication._ResultFilesDir + guid + ".docx", opt); } catch (Exception) { return(ErrorMsg.InvalidFileContent); } return(ErrorMsg.Ok); }
public void FileToTextHandlerTest() { IFormFile file = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("Мама мыла раму")), 0, 100, "TestTxt", "TestTxt.txt"); var result = controller.FileHandler(file, "123", "Кто мыл раму", "Encrypt", "") as ViewResult; Assert.IsType <ViewResult>(result); Assert.Equal(VigenereEncryptor.Encrypt("Мама мыла раму", "Кто мыл раму", 0, out int _), result?.ViewData["Text"]); }
public void TextToTxtHandlerTest() { var result = controller.TextHandler("Мама мыла раму", "мама", "Encrypt", "true", "solution.txt") as FileContentResult; byte[] bytes = result.FileContents; Assert.IsType <FileContentResult>(result); Assert.Equal(VigenereEncryptor.Encrypt("Мама мыла раму", "мама", 0, out int _), Encoding.UTF8.GetString(bytes)); Assert.Equal(@"text/plain", result.ContentType); }
public void ReturnsSourceIteratorIfNullOrEmptyKey(IEnumerable <string> source, string key) { //Arrange VigenereEncryptor encryptor = new VigenereEncryptor(); //Act IEnumerable <string> result = encryptor.Encrypt(source, key); //Assert Assert.AreEqual(source, result); }
private static ErrorMsg TryEncryptRawText(string text, string keyWord, string guid, VigenereEncryptor.Operation op, out string rawResult) { rawResult = VigenereEncryptor.Encrypt(text, keyWord, op); var status = rawResult == null ? ErrorMsg.InvalidKeyWord : ErrorMsg.Ok; if (status == ErrorMsg.Ok) { System.IO.File.WriteAllText(WebApiApplication._ResultFilesDir + guid + ".txt", rawResult); } return(status); }
public void EncrypteLimitedNumberOfElements() { //Arrange //limitOfElements should be the same as limitOfBufferLength in VigenereEncryptor.ModifyCollection method int limitOfElements = byte.MaxValue; VigenereEncryptor encryptor = new VigenereEncryptor(); IEnumerable <string> source = TestListGenerator.GetList(limitOfElements + 1); //Act int actualResult = encryptor.Encrypt(source, "key").Count(); //Assert Assert.AreEqual(limitOfElements, actualResult); }
public void ReturnsIteratorOfEncryptedStringsWhenParamsNotNullOrEmpty( IEnumerable <string> source, string key, IEnumerable <string> expectedResult) { //Arrange VigenereEncryptor encryptor = new VigenereEncryptor(); //Act IEnumerable <string> actualResult = encryptor.Encrypt(source, key); //Assert Assert.AreEqual(expectedResult, actualResult); }
private static ErrorMsg TryEncryptTxt(string filePath, string keyWord, string guid, VigenereEncryptor.Operation op, out string result) { result = System.IO.File.ReadAllText(filePath); if (result.Contains(Convert.ToChar(0xFFFD))) { result = System.IO.File.ReadAllText(filePath, Encoding.Default); } result = VigenereEncryptor.Encrypt(result, keyWord, op); if (result == null) { return(ErrorMsg.InvalidKeyWord); } System.IO.File.WriteAllText(WebApiApplication._ResultFilesDir + guid + ".txt", result); return(ErrorMsg.Ok); }
public void ReturnsIteratorWhithEmptyStringIfNullOrEmptySource( IEnumerable <string> source, string key) { //Arrange VigenereEncryptor encryptor = new VigenereEncryptor(); List <string> expectedResult = new List <string> { "" }; //Act IEnumerable <string> actualResult = encryptor.Encrypt(source, key); //Assert Assert.AreEqual(expectedResult, actualResult); }
public void VigenereEncryptTest() { string DecryptedText = "Мама мыла раму !!11!"; Assert.Equal("Щаща щыша эащу !!11!", VigenereEncryptor.Encrypt(DecryptedText, "мама", 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")); }