示例#1
0
        private static byte[] DecryptKey(byte[] stretchedKey, byte[] buffer, int offset)
        {
            using var twofish = new TwofishManaged();
            twofish.Mode      = CipherMode.ECB;
            twofish.Padding   = PaddingMode.None;
            twofish.KeySize   = 256;
            twofish.Key       = stretchedKey;

            using var transform = twofish.CreateDecryptor();
            return(transform.TransformFinalBlock(buffer, offset, 32));
        }
示例#2
0
        private static byte[] DecryptData(byte[] key, byte[] iv, byte[] buffer, int offset, int length)
        {
            using var twofish = new TwofishManaged();
            twofish.Mode      = CipherMode.CBC;
            twofish.Padding   = PaddingMode.None;
            twofish.KeySize   = 256;
            twofish.Key       = key;
            twofish.IV        = iv;

            using var dataDecryptor = twofish.CreateDecryptor();
            return(dataDecryptor.TransformFinalBlock(buffer, offset, length));
        }
示例#3
0
        public void Twofish_MultiBlockFinal_ECB_256_Decrypt()
        {
            var key = ParseBytes("0000000000000000000000000000000000000000000000000000000000000000");
            var ct  = ParseBytes("57FF739D4DC92C1BD7FC01700CC8216F57FF739D4DC92C1BD7FC01700CC8216F57FF739D4DC92C1BD7FC01700CC8216F");

            using (var algorithm = new TwofishManaged()
            {
                KeySize = 256, Mode = CipherMode.ECB, Padding = PaddingMode.None
            }) {
                algorithm.Key = key;
                var pt = algorithm.CreateDecryptor().TransformFinalBlock(ct, 0, ct.Length);
                Assert.AreEqual("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", BitConverter.ToString(pt).Replace("-", ""));
            }
        }
示例#4
0
        public void Twofish_TransformBlock_Decrypt_UseSameArray()
        {
            var key  = ParseBytes("00000000000000000000000000000000");
            var iv   = ParseBytes("00000000000000000000000000000000");
            var ctpt = ParseBytes("B0DD30E9AB1F1329C1BEE154DDBE88AF8C47A4FE24D56DC027ED503652C9D164CE26E0C6E32BCA8756482B99988E8C79");

            using (var twofish = new TwofishManaged()
            {
                Mode = CipherMode.CBC, Padding = PaddingMode.None, KeySize = 128, Key = key, IV = iv
            }) {
                using (var transform = twofish.CreateDecryptor()) {
                    transform.TransformBlock(ctpt, 0, 48, ctpt, 0); //no caching last block if Padding is none
                }
            }
            Assert.AreEqual("The quick brown fox jumps over the lazy dog once", Encoding.UTF8.GetString(ctpt));
        }
示例#5
0
        public void Twofish_MultiBlockFinal_CBC_256_Decrypt()
        {
            var key = ParseBytes("0000000000000000000000000000000000000000000000000000000000000000");
            var iv  = ParseBytes("00000000000000000000000000000000");
            var ct  = ParseBytes("61B5BC459C4E9491DD9E6ACB7478813047BE7250D34F792C17F0C23583C0B040B95C9FAE11107EE9BAC3D79BBFE019EE");

            using (var algorithm = new TwofishManaged()
            {
                KeySize = 256, Mode = CipherMode.CBC, Padding = PaddingMode.None
            }) {
                algorithm.Key = key;
                algorithm.IV  = iv;
                var pt = algorithm.CreateDecryptor().TransformFinalBlock(ct, 0, ct.Length);
                Assert.AreEqual("9F589F5CF6122C32B6BFEC2F2AE8C35A9F589F5CF6122C32B6BFEC2F2AE8C35A9F589F5CF6122C32B6BFEC2F2AE8C35A", BitConverter.ToString(pt).Replace("-", ""));
            }
        }
示例#6
0
        public static void Main(string[] args)
        {
            Console.Title = "Twofish.Tests";

            var bIn = Encoding.UTF8.GetBytes("It works!");

            byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; // 128bit key
            byte[] iv  = { 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; // initialization vector

            using (var algorithm = new TwofishManaged {
                KeySize = key.Length * 8, Mode = CipherMode.CBC
            })
            {
                byte[] encrypted;

                using (var ms = new MemoryStream())
                {
                    using (var transform = algorithm.CreateEncryptor(key, iv))
                    {
                        using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
                        {
                            cs.Write(bIn, 0, bIn.Length);
                        }
                    }

                    encrypted = ms.ToArray();

                    Console.WriteLine($"Encrypted: {BitConverter.ToString(encrypted).Replace("-", string.Empty)}");
                }

                using (var ms = new MemoryStream())
                {
                    using (var transform = algorithm.CreateDecryptor(key, iv))
                    {
                        using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
                        {
                            cs.Write(encrypted, 0, encrypted.Length);
                        }
                    }

                    Console.WriteLine($"Decrypted: {Encoding.UTF8.GetString(ms.ToArray())}");
                }
            }

            Console.Read();
        }
示例#7
0
        public static bool TestTwofish()
        {
            TwofishManaged twofish = new TwofishManaged();

            twofish.Padding = PaddingMode.None;
            twofish.Mode    = CipherMode.ECB;
            byte[] key        = new byte[] { 0xD4, 0x3B, 0xB7, 0x55, 0x6E, 0xA3, 0x2E, 0x46, 0xF2, 0xA2, 0x82, 0xB7, 0xD4, 0x5B, 0x4E, 0x0D, 0x57, 0xFF, 0x73, 0x9D, 0x4D, 0xC9, 0x2C, 0x1B, 0xD7, 0xFC, 0x01, 0x70, 0x0C, 0xC8, 0x21, 0x6F };
            byte[] plaintext  = new byte[] { 0x90, 0xAF, 0xE9, 0x1B, 0xB2, 0x88, 0x54, 0x4F, 0x2C, 0x32, 0xDC, 0x23, 0x9B, 0x26, 0x35, 0xE6 };
            byte[] ciphertext = new byte[] { 0x6C, 0xB4, 0x56, 0x1C, 0x40, 0xBF, 0x0A, 0x97, 0x05, 0x93, 0x1C, 0xB6, 0xD4, 0x08, 0xE7, 0xFA };

            byte[]           initializationVector = new byte[16];
            ICryptoTransform encryptor            = twofish.CreateEncryptor(key, initializationVector);
            ICryptoTransform decryptor            = twofish.CreateDecryptor(key, initializationVector);

            byte[] result1 = new byte[16];
            byte[] result2 = new byte[16];
            encryptor.TransformBlock(plaintext, 0, 16, result1, 0);
            decryptor.TransformBlock(ciphertext, 0, 16, result2, 0);
            return(ByteUtils.AreByteArraysEqual(result1, ciphertext) && ByteUtils.AreByteArraysEqual(result2, plaintext));
        }