private static void EncryptDecryptPlainText(Options ops, ICypher cypher) { string originalText = File.ReadAllText(ops.SourceFilePath); EndOfLine endOfLine; if (!AlphabeticCypher.IsTextPlain(originalText, out endOfLine)) { throw new InvalidOperationException("Text is not considered plain text"); } cypher.Alphabet = AlphabeticCypher.GetPlainTextAlphabet(endOfLine); if (ops.Action == Action.Encrypt) { string plainText = originalText; string cryptoText = cypher.Encrypt(plainText, ops.EncryptionKey); File.WriteAllText(ops.TargetFilePath, cryptoText); } else { string cryptoText = originalText; string plainText = cypher.Decrypt(cryptoText, ops.EncryptionKey); File.WriteAllText(ops.TargetFilePath, plainText); } }
public void TestCheckPlainText() { var r = new Random(0); string plainEolWindows = PlainText; string plainEolUnix = PlainText.Replace("\r\n", "\n"); string plainEolMac = PlainText.Replace("\r\n", "\r"); string noEol = PlainText.Replace("\r\n", ""); byte[] randBytes = new byte[256]; r.NextBytes(randBytes); string randText = randBytes.ToTextString(); var tests = new[] { new { Name = "Windows", Text = plainEolWindows, Plain = true, Eol = EndOfLine.Mixed }, new { Name = "Unix", Text = plainEolUnix, Plain = true, Eol = EndOfLine.LineFeed }, new { Name = "Mac", Text = plainEolMac, Plain = true, Eol = EndOfLine.CarriageReturn }, new { Name = "None", Text = noEol, Plain = true, Eol = EndOfLine.None }, new { Name = "Binary", Text = randText, Plain = false, Eol = EndOfLine.None }, }; bool success = true; var errs = new List <string>(); foreach (var t in tests) { bool currentPased = Utils.WrappedTest(() => { using (Log.StartIndentScope()) { EndOfLine eol; bool isPlain = AlphabeticCypher.IsTextPlain(t.Text, out eol); return(isPlain == t.Plain && eol == t.Eol); } }, Log); if (!currentPased) { Log.Error($"Testing {t.Name} failed."); errs.Add(t.Name); } success &= currentPased; } Assert.IsTrue(success, $"Following tests failed: {string.Join(",", errs)}"); }