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 AutoKeyCypherWikipedia() { // Check Wikipedia var tc = new Blaze.Cryptography.Classics.AutokeyCypher(); tc.Alphabet = AlphabeticCypher.GetSimpleAlphabet(true, false, false); string k = "QUEENLY"; string plain1 = "ATTACKATDAWN"; string cypher1 = tc.Encrypt(plain1, k); Assert.AreEqual("QNXEPVYTWTWP", cypher1); string decypher1 = tc.Decrypt(cypher1, k); Assert.AreEqual(plain1, decypher1); string k2 = "KILT"; string plain2 = "MEETATTHEFOUNTAIN"; string cypher2 = tc.Encrypt(plain2, k2); Assert.AreEqual("WMPMMXXAEYHBRYOCA", cypher2); string decypher2 = tc.Decrypt(cypher2, k2); Assert.AreEqual(plain2, decypher2); }
public void ColumnarTranspositionCypherCustom() { var cypher = new ColumnarTranspositionCypher(); cypher.Alphabet = AlphabeticCypher.GetSimpleAlphabet(true, false, false); string[] testStrings = { "TIMESUP", "WEMUSTATTACKATDAWN", "ATTACKATDAWN", "WEAREDISCOVEREDFLEEATONCE" }; bool success = true; Log.Info("Without fill"); string[] keys = { "DACB", "AKAB", "KEY", "ZEBRAS" }; using (Log.StartIndentScope()) { foreach (string keyStr in keys) { byte[] key = keyStr.ToByteArray(); using (Log.StartIndentScope()) { foreach (string testStr in testStrings) { success &= Utils.WrappedTest(() => { byte[] testArr = testStr.ToByteArray(); byte[] cypherArr = cypher.EncryptClassic(testArr, key); string plainStr = cypher.DecryptClassic(cypherArr, key) .ToTextString(); string cypherStr = cypherArr.ToTextString(); Log.Info($"Plain text: {testStr}"); Log.Info($"Cypher text: {cypherStr}"); Log.Info($"Decypher text: {plainStr}"); return(plainStr == testStr); }, Log); } } } } Assert.IsTrue(success, "One or more cases failed"); }
public void TranspositionBasicTest() { var cypher = new TranspositionCypher(); cypher.Alphabet = AlphabeticCypher.GetSimpleAlphabet(true, false, false); string[] testStrings = { "WEMUSTATTACKATDAWN", "ATTACKATDAWN", "TIMESUP" }; bool success = true; Log.Info("Without fill"); using (Log.StartIndentScope()) foreach (string testStr in testStrings) { success &= Utils.WrappedTest(() => { string cypherStr = cypher.Encrypt(testStr.ToByteArray(), 4).ToTextString(); string plainStr = cypher.Decrypt(cypherStr.ToByteArray(), 4).ToTextString(); Log.Info($"Plain text: {testStr}"); Log.Info($"Cypher text: {cypherStr}"); Log.Info($"Decypher text: {plainStr}"); return(plainStr == testStr); }, Log); } byte[] key = "KEY".ToByteArray(); //With fill Log.Info("With fill"); using (Log.StartIndentScope()) foreach (string testStr in testStrings) { success &= Utils.WrappedTest(() => { string cypherStr = cypher.EncryptWithFill(testStr.ToByteArray(), key).ToTextString(); string plainStr = cypher.DecryptWithFill(cypherStr.ToByteArray(), key).ToTextString(); Log.Info($"Plain text: {testStr}"); Log.Info($"Cypher text: {cypherStr}"); Log.Info($"Decypher text: {plainStr}"); return(plainStr == testStr); }, Log); } Assert.IsTrue(success, "One or more cases failed"); }
protected List <EncryptTest> GetEncryptions() { var res = new ListConstruct <EncryptTest> { { new NullCypher(), "Null Cypher" }, { new AutokeyCypher(), "AutoKey Cypher", TestType.All }, { new BifidCypher(), "Bifid Cypher", TestType.Full | TestType.Xor }, { new CaesarCypher(), "Caesar Cypher" }, { new Vigenere(), "Vigenere Cypher" }, { new HillCypher(), "Hill Cypher", TestType.All }, { new Rot13(), "Rot 13", AlphabeticCypher.GetSimpleAlphabet(true, false, false), ROT13_TEXTS }, { new ShuffleCypher(), "Shuffle Cypher", TestType.All }, { new StreamCypher(), "Stream Cypher" }, { new SaltedCypher(new StreamCypher()), "Salted Cypher", TestType.All }, { new TranspositionCypher(), "Transposition Cypher", TestType.All }, { new ColumnarTranspositionCypher(), "Columnar Transposition Cypher", TestType.All }, { new FibonacciCypher(), "Fibonacci Cypher" }, { new FibonacciCypherV2(), "Fibonacci Cypher V2" }, { new FibonacciCypherV3(), "Fibonacci Cypher V3" }, { new RandomBijection(new NullCypher()), "RBIJ on Null", TestType.NotXor }, { new RandomBijection(new Vigenere()), "RBIJ on Vigenere", TestType.NotXor }, { new RandomBijection(new FibonacciCypher()), "RBIJ on Fibonacci", TestType.NotXor }, { new RandomBijection(new FibonacciCypherV3()), "RBIJ on FibonaccyV3", TestType.NotXor }, { new RandomBijection(new StreamCypher()), "RBIJ on Stream", TestType.NotXor }, { new ChainCypher( typeof(FibonacciCypher), typeof(StreamCypher), typeof(FibonacciCypherV3)), "Chain1", TestType.Full }, }; return(res); }
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)}"); }