Beispiel #1
0
 public static void TripleDESDefaults()
 {
     using (TripleDES des = TripleDESFactory.Create())
     {
         Assert.Equal(192, des.KeySize);
         Assert.Equal(64, des.BlockSize);
     }
 }
Beispiel #2
0
 public static void TripleDESInvalidKeySizes()
 {
     using (TripleDES des = TripleDESFactory.Create())
     {
         Assert.Throws <CryptographicException>(() => des.KeySize = 128 - des.BlockSize);
         Assert.Throws <CryptographicException>(() => des.KeySize = 192 + des.BlockSize);
     }
 }
Beispiel #3
0
        private static void TestTripleDESTransformDirectKey(
            CipherMode cipherMode,
            PaddingMode paddingMode,
            byte[] key,
            byte[] iv,
            byte[] plainBytes,
            byte[] cipherBytes,
            int?feedbackSize = default)
        {
            byte[] liveEncryptBytes;
            byte[] liveDecryptBytes;
            byte[] liveOneShotDecryptBytes = null;
            byte[] liveOneShotEncryptBytes = null;

            using (TripleDES tdes = TripleDESFactory.Create())
            {
                tdes.Mode    = cipherMode;
                tdes.Padding = paddingMode;
                tdes.Key     = key;

                if (feedbackSize.HasValue)
                {
                    tdes.FeedbackSize = feedbackSize.Value;
                }

                liveEncryptBytes = TripleDESEncryptDirectKey(tdes, key, iv, plainBytes);
                liveDecryptBytes = TripleDESDecryptDirectKey(tdes, key, iv, cipherBytes);

                if (cipherMode == CipherMode.ECB)
                {
                    liveOneShotDecryptBytes = tdes.DecryptEcb(cipherBytes, paddingMode);
                    liveOneShotEncryptBytes = tdes.EncryptEcb(plainBytes, paddingMode);
                }
                else if (cipherMode == CipherMode.CBC)
                {
                    liveOneShotDecryptBytes = tdes.DecryptCbc(cipherBytes, iv, paddingMode);
                    liveOneShotEncryptBytes = tdes.EncryptCbc(plainBytes, iv, paddingMode);
                }
                else if (cipherMode == CipherMode.CFB)
                {
                    liveOneShotDecryptBytes = tdes.DecryptCfb(cipherBytes, iv, paddingMode, feedbackSizeInBits: feedbackSize.Value);
                    liveOneShotEncryptBytes = tdes.EncryptCfb(plainBytes, iv, paddingMode, feedbackSizeInBits: feedbackSize.Value);
                }

                if (liveOneShotDecryptBytes is not null)
                {
                    Assert.Equal(plainBytes, liveOneShotDecryptBytes);
                }

                if (liveOneShotEncryptBytes is not null)
                {
                    Assert.Equal(cipherBytes, liveOneShotEncryptBytes);
                }
            }

            Assert.Equal(cipherBytes, liveEncryptBytes);
            Assert.Equal(plainBytes, liveDecryptBytes);
        }
Beispiel #4
0
 public static void TripleDESGenerate128Key()
 {
     using (TripleDES des = TripleDESFactory.Create())
     {
         des.KeySize = 128;
         byte[] key = des.Key;
         Assert.Equal(128, key.Length * 8);
     }
 }
        public static void Windows7DoesNotSupportCFB64()
        {
            using (TripleDES tdes = TripleDESFactory.Create())
            {
                tdes.GenerateKey();
                tdes.Mode         = CipherMode.CFB;
                tdes.FeedbackSize = 64;

                Assert.ThrowsAny <CryptographicException>(() => tdes.CreateDecryptor());
                Assert.ThrowsAny <CryptographicException>(() => tdes.CreateEncryptor());
            }
        }
        public static void ValidCFBFeedbackSizes(int feedbackSize)
        {
            using (TripleDES tdes = TripleDESFactory.Create())
            {
                tdes.GenerateKey();
                tdes.Mode = CipherMode.CFB;

                tdes.FeedbackSize = feedbackSize;

                using var decryptor = tdes.CreateDecryptor();
                using var encryptor = tdes.CreateEncryptor();
                Assert.NotNull(decryptor);
                Assert.NotNull(encryptor);
            }
        }
Beispiel #7
0
        public static void Cfb8ModeCanDepadCfb64Padding()
        {
            using (TripleDES tdes = TripleDESFactory.Create())
            {
                // 1, 2, 3, 4, 5 encrypted with CFB8 but padded with block-size padding.
                byte[] ciphertext = "97F1CE6A6D869A85".HexToByteArray();
                tdes.Key          = "3D1ECCEE6C99B029950ED23688AA229AF85177421609F7BF".HexToByteArray();
                tdes.IV           = new byte[8];
                tdes.Padding      = PaddingMode.PKCS7;
                tdes.Mode         = CipherMode.CFB;
                tdes.FeedbackSize = 8;

                using ICryptoTransform transform = tdes.CreateDecryptor();
                byte[] decrypted = transform.TransformFinalBlock(ciphertext, 0, ciphertext.Length);
                Assert.Equal(new byte[] { 1, 2, 3, 4, 5 }, decrypted);
            }
        }
Beispiel #8
0
        public static void EcbRoundtrip(byte[] plaintext, byte[] ciphertext, PaddingMode padding)
        {
            using (TripleDES tdes = TripleDESFactory.Create())
            {
                tdes.Key = s_tdes192OneShotKey;

                // Even though we have set the instance to use CFB, the Ecb one shots should
                // always be done in ECB.
                tdes.FeedbackSize = 8;
                tdes.Mode         = CipherMode.CFB;
                tdes.Padding      = padding == PaddingMode.None ? PaddingMode.PKCS7 : PaddingMode.None;

                byte[] encrypted = tdes.EncryptEcb(plaintext, padding);
                byte[] decrypted = tdes.DecryptEcb(encrypted, padding);

                if (padding == PaddingMode.Zeros)
                {
                    Assert.Equal(plaintext, decrypted[..plaintext.Length]);
        public static void VerifyNetFxCompat_CFB8_PKCS7Padding()
        {
            // .NET Framework would always pad to the nearest block
            // with CFB8 and PKCS7 padding even though the shortest possible
            // padding is always 1 byte. This ensures we can continue to decrypt
            // .NET Framework encrypted data with the excessive padding.

            byte[] key        = "531bd715cbf785c10169b6e4926562b8e1e5c4c8884ed791".HexToByteArray();
            byte[] iv         = "dbeba40532a5304a".HexToByteArray();
            byte[] plaintext  = "70656e6e79".HexToByteArray();
            byte[] ciphertext = "8798c2da055c9ea0".HexToByteArray();

            using TripleDES tdes = TripleDESFactory.Create();
            tdes.Mode            = CipherMode.CFB;
            tdes.Padding         = PaddingMode.PKCS7;
            tdes.FeedbackSize    = 8;

            byte[] decrypted = TripleDESDecryptDirectKey(tdes, key, iv, ciphertext);
            Assert.Equal(plaintext, decrypted);
        }
        public static void TripleDESRoundTrip192BitsPKCS7ECB()
        {
            byte[] key = "155425f12109cd89378795a4ca337b3264689dca497ba2fa".HexToByteArray();

            using (TripleDES alg = TripleDESFactory.Create())
            {
                alg.Key     = key;
                alg.Padding = PaddingMode.PKCS7;
                alg.Mode    = CipherMode.ECB;

                byte[] plainText      = "5bd3c4e16a723a17ac60dd0efdb158e269cddfd0fa".HexToByteArray();
                byte[] cipher         = alg.Encrypt(plainText);
                byte[] expectedCipher = "7b8d982ee0c14821daf1b8cf4e407c2eb328627b696ac36e".HexToByteArray();
                Assert.Equal <byte>(expectedCipher, cipher);

                byte[] decrypted         = alg.Decrypt(cipher);
                byte[] expectedDecrypted = "5bd3c4e16a723a17ac60dd0efdb158e269cddfd0fa".HexToByteArray();
                Assert.Equal <byte>(expectedDecrypted, decrypted);
            }
        }
        public static void TripleDESRoundTrip192BitsISO10126ECB()
        {
            byte[] key = "9da5b265179d65f634dfc95513f25094411e51bb3be877ef".HexToByteArray();

            using (TripleDES alg = TripleDESFactory.Create())
            {
                alg.Key     = key;
                alg.Padding = PaddingMode.ISO10126;
                alg.Mode    = CipherMode.ECB;

                byte[] plainText = "77a8b2efb45addb38d7ef3aa9e6ab5d71957445ab8".HexToByteArray();
                byte[] cipher    = alg.Encrypt(plainText);

                // the padding data for ISO10126 is made up of random bytes, so we cannot actually test
                // the full encrypted text. We need to strip the padding and then compare
                byte[] decrypted = alg.Decrypt(cipher);

                Assert.Equal <byte>(plainText, decrypted);
            }
        }
        public static void TripleDESRoundTrip192BitsZerosECB()
        {
            byte[] key = "9da5b265179d65f634dfc95513f25094411e51bb3be877ef".HexToByteArray();

            using (TripleDES alg = TripleDESFactory.Create())
            {
                alg.Key     = key;
                alg.Padding = PaddingMode.Zeros;
                alg.Mode    = CipherMode.ECB;

                byte[] plainText      = "77a8b2efb45addb38d7ef3aa9e6ab5d71957445ab8".HexToByteArray();
                byte[] cipher         = alg.Encrypt(plainText);
                byte[] expectedCipher = "149ec32f558b27c7e4151e340d8184f18b4e25d2518f69d9".HexToByteArray();
                Assert.Equal <byte>(expectedCipher, cipher);

                byte[] decrypted         = alg.Decrypt(cipher);
                byte[] expectedDecrypted = "77a8b2efb45addb38d7ef3aa9e6ab5d71957445ab8000000".HexToByteArray();
                Assert.Equal <byte>(expectedDecrypted, decrypted);
            }
        }
        public static void TripleDESRoundTrip192BitsNoneECB()
        {
            byte[] key = "c5629363d957054eba793093b83739bb78711db221a82379".HexToByteArray();

            using (TripleDES alg = TripleDESFactory.Create())
            {
                alg.Key     = key;
                alg.Padding = PaddingMode.None;
                alg.Mode    = CipherMode.ECB;

                byte[] plainText      = "de7d2dddea96b691e979e647dc9d3ca27d7f1ad673ca9570".HexToByteArray();
                byte[] cipher         = alg.Encrypt(plainText);
                byte[] expectedCipher = "e56f72478c7479d169d54c0548b744af5b53efb1cdd26037".HexToByteArray();
                Assert.Equal <byte>(expectedCipher, cipher);

                byte[] decrypted         = alg.Decrypt(cipher);
                byte[] expectedDecrypted = "de7d2dddea96b691e979e647dc9d3ca27d7f1ad673ca9570".HexToByteArray();
                Assert.Equal <byte>(expectedDecrypted, decrypted);
            }
        }
        public static void ValidCFBFeedbackSizes(int feedbackSize)
        {
            // Windows 7 only supports CFB8.
            if (feedbackSize != 8 && PlatformDetection.IsWindows7)
            {
                return;
            }

            using (TripleDES tdes = TripleDESFactory.Create())
            {
                tdes.GenerateKey();
                tdes.Mode = CipherMode.CFB;

                tdes.FeedbackSize = feedbackSize;

                using var decryptor = tdes.CreateDecryptor();
                using var encryptor = tdes.CreateEncryptor();
                Assert.NotNull(decryptor);
                Assert.NotNull(encryptor);
            }
        }
        public static void TripleDESRoundTrip192BitsNoneCBC()
        {
            byte[] key = "b43eaf0260813fb47c87ae073a146006d359ad04061eb0e6".HexToByteArray();
            byte[] iv  = "5fbc5bc21b8597d8".HexToByteArray();

            using (TripleDES alg = TripleDESFactory.Create())
            {
                alg.Key     = key;
                alg.IV      = iv;
                alg.Padding = PaddingMode.None;
                alg.Mode    = CipherMode.CBC;

                byte[] plainText      = "79a86903608e133e020e1dc68c9835250c2f17b0ebeed91b".HexToByteArray();
                byte[] cipher         = alg.Encrypt(plainText);
                byte[] expectedCipher = "dea36279600f19c602b6ed9bf3ffdac5ebf25c1c470eb61c".HexToByteArray();
                Assert.Equal <byte>(expectedCipher, cipher);

                byte[] decrypted         = alg.Decrypt(cipher);
                byte[] expectedDecrypted = "79a86903608e133e020e1dc68c9835250c2f17b0ebeed91b".HexToByteArray();
                Assert.Equal <byte>(expectedDecrypted, decrypted);
            }
        }
        public static void TripleDESRoundTrip192BitsZerosCBC()
        {
            byte[] key = "5e970c0d2323d53b28fa3de507d6d20f9f0cd97123398b4d".HexToByteArray();
            byte[] iv  = "95498b5bf570f4c8".HexToByteArray();

            using (TripleDES alg = TripleDESFactory.Create())
            {
                alg.Key     = key;
                alg.IV      = iv;
                alg.Padding = PaddingMode.Zeros;
                alg.Mode    = CipherMode.CBC;

                byte[] plainText      = "f9e9a1385bf3bd056d6a06eac662736891bd3e6837".HexToByteArray();
                byte[] cipher         = alg.Encrypt(plainText);
                byte[] expectedCipher = "65f3dc211876a9daad238aa7d0c7ed7a3662296faf77dff9".HexToByteArray();
                Assert.Equal <byte>(expectedCipher, cipher);

                byte[] decrypted         = alg.Decrypt(cipher);
                byte[] expectedDecrypted = "f9e9a1385bf3bd056d6a06eac662736891bd3e6837000000".HexToByteArray();
                Assert.Equal <byte>(expectedDecrypted, decrypted);
            }
        }
        public static void TripleDESRoundTrip192BitsPKCS7CBC()
        {
            byte[] key = "6b42da08f93e819fbd26fce0785b0eec3d0cb6bfa053c505".HexToByteArray();
            byte[] iv  = "8fc67ce5e7f28cde".HexToByteArray();

            using (TripleDES alg = TripleDESFactory.Create())
            {
                alg.Key     = key;
                alg.IV      = iv;
                alg.Padding = PaddingMode.PKCS7;
                alg.Mode    = CipherMode.CBC;

                byte[] plainText      = "e867f915e275eab27d6951165d26dec6dd0acafcfc".HexToByteArray();
                byte[] cipher         = alg.Encrypt(plainText);
                byte[] expectedCipher = "446f57875e107702afde16b57eaf250b87b8110bef29af89".HexToByteArray();
                Assert.Equal <byte>(expectedCipher, cipher);

                byte[] decrypted         = alg.Decrypt(cipher);
                byte[] expectedDecrypted = "e867f915e275eab27d6951165d26dec6dd0acafcfc".HexToByteArray();
                Assert.Equal <byte>(expectedDecrypted, decrypted);
            }
        }
Beispiel #18
0
        public static void TripleDESReuseEncryptorDecryptor()
        {
            byte[] key = "6b42da08f93e819fbd26fce0785b0eec3d0cb6bfa053c505".HexToByteArray();
            byte[] iv  = "8fc67ce5e7f28cde".HexToByteArray();

            using (TripleDES alg = TripleDESFactory.Create())
            {
                alg.Key     = key;
                alg.IV      = iv;
                alg.Padding = PaddingMode.PKCS7;
                alg.Mode    = CipherMode.CBC;

                using (ICryptoTransform encryptor = alg.CreateEncryptor())
                    using (ICryptoTransform decryptor = alg.CreateDecryptor())
                    {
                        for (int i = 0; i < 2; i++)
                        {
                            byte[] plainText1      = "e867f915e275eab27d6951165d26dec6dd0acafcfc".HexToByteArray();
                            byte[] cipher1         = encryptor.Transform(plainText1);
                            byte[] expectedCipher1 = "446f57875e107702afde16b57eaf250b87b8110bef29af89".HexToByteArray();
                            Assert.Equal <byte>(expectedCipher1, cipher1);

                            byte[] decrypted1         = decryptor.Transform(cipher1);
                            byte[] expectedDecrypted1 = "e867f915e275eab27d6951165d26dec6dd0acafcfc".HexToByteArray();
                            Assert.Equal <byte>(expectedDecrypted1, decrypted1);


                            byte[] plainText2      = "54686973206973206120736563726574206d657373616765".HexToByteArray();
                            byte[] cipher2         = encryptor.Transform(plainText2);
                            byte[] expectedCipher2 = "da6af8adc5d934c24943176db82eef34aa027c93e9dbe52dc5f1fa64fef4061c".HexToByteArray();
                            Assert.Equal <byte>(expectedCipher2, cipher2);

                            byte[] decrypted2         = decryptor.Transform(cipher2);
                            byte[] expectedDecrypted2 = "54686973206973206120736563726574206d657373616765".HexToByteArray();
                            Assert.Equal <byte>(expectedDecrypted2, decrypted2);
                        }
                    }
            }
        }
Beispiel #19
0
        public static void TripleDESRoundTripISO10126ECB(int keySize, string keyHex)
        {
            byte[] key = keyHex.HexToByteArray();

            using (TripleDES alg = TripleDESFactory.Create())
            {
                alg.Key = key;
                Assert.Equal(keySize, alg.KeySize);

                alg.Padding = PaddingMode.ISO10126;
                alg.Mode    = CipherMode.ECB;

                byte[] plainText = "77a8b2efb45addb38d7ef3aa9e6ab5d71957445ab8".HexToByteArray();
                byte[] cipher    = alg.Encrypt(plainText);

                // the padding data for ISO10126 is made up of random bytes, so we cannot actually test
                // the full encrypted text. We need to strip the padding and then compare
                byte[] decrypted = alg.Decrypt(cipher);

                Assert.Equal <byte>(plainText, decrypted);
            }
        }
Beispiel #20
0
        public static void TripleDESRoundTripNoneECB(int keySize, string expectedCipherHex, string keyHex)
        {
            byte[] key = keyHex.HexToByteArray();

            using (TripleDES alg = TripleDESFactory.Create())
            {
                alg.Key = key;
                Assert.Equal(keySize, alg.KeySize);

                alg.Padding = PaddingMode.None;
                alg.Mode    = CipherMode.ECB;

                byte[] plainText      = "de7d2dddea96b691e979e647dc9d3ca27d7f1ad673ca9570".HexToByteArray();
                byte[] cipher         = alg.Encrypt(plainText);
                byte[] expectedCipher = expectedCipherHex.HexToByteArray();
                Assert.Equal <byte>(expectedCipher, cipher);

                byte[] decrypted         = alg.Decrypt(cipher);
                byte[] expectedDecrypted = "de7d2dddea96b691e979e647dc9d3ca27d7f1ad673ca9570".HexToByteArray();
                Assert.Equal <byte>(expectedDecrypted, decrypted);
            }
        }
Beispiel #21
0
        public static void TripleDESRoundTripPKCS7ECB(int keySize, string expectedCipherHex, string keyHex)
        {
            byte[] key = keyHex.HexToByteArray();

            using (TripleDES alg = TripleDESFactory.Create())
            {
                alg.Key = key;
                Assert.Equal(keySize, alg.KeySize);

                alg.Padding = PaddingMode.PKCS7;
                alg.Mode    = CipherMode.ECB;

                byte[] plainText      = "5bd3c4e16a723a17ac60dd0efdb158e269cddfd0fa".HexToByteArray();
                byte[] cipher         = alg.Encrypt(plainText);
                byte[] expectedCipher = expectedCipherHex.HexToByteArray();
                Assert.Equal <byte>(expectedCipher, cipher);

                byte[] decrypted         = alg.Decrypt(cipher);
                byte[] expectedDecrypted = "5bd3c4e16a723a17ac60dd0efdb158e269cddfd0fa".HexToByteArray();
                Assert.Equal <byte>(expectedDecrypted, decrypted);
            }
        }
Beispiel #22
0
        public static void TripleDESRoundTripZerosECB(int keySize, string expectedCipherHex, string keyHex)
        {
            byte[] key = keyHex.HexToByteArray();

            using (TripleDES alg = TripleDESFactory.Create())
            {
                alg.Key = key;
                Assert.Equal(keySize, alg.KeySize);

                alg.Padding = PaddingMode.Zeros;
                alg.Mode    = CipherMode.ECB;

                byte[] plainText      = "77a8b2efb45addb38d7ef3aa9e6ab5d71957445ab8".HexToByteArray();
                byte[] cipher         = alg.Encrypt(plainText);
                byte[] expectedCipher = expectedCipherHex.HexToByteArray();
                Assert.Equal <byte>(expectedCipher, cipher);

                byte[] decrypted         = alg.Decrypt(cipher);
                byte[] expectedDecrypted = "77a8b2efb45addb38d7ef3aa9e6ab5d71957445ab8000000".HexToByteArray();
                Assert.Equal <byte>(expectedDecrypted, decrypted);
            }
        }
        public static void EncryptWithLargeOutputBuffer(bool blockAlignedOutput)
        {
            using (TripleDES alg = TripleDESFactory.Create())
                using (ICryptoTransform xform = alg.CreateEncryptor())
                {
                    // 8 blocks, plus maybe three bytes
                    int    outputPadding = blockAlignedOutput ? 0 : 3;
                    byte[] output        = new byte[alg.BlockSize + outputPadding];
                    // 2 blocks of 0x00
                    byte[] input        = new byte[alg.BlockSize / 4];
                    int    outputOffset = 0;

                    outputOffset += xform.TransformBlock(input, 0, input.Length, output, outputOffset);
                    byte[] overflow = xform.TransformFinalBlock(Array.Empty <byte>(), 0, 0);
                    Buffer.BlockCopy(overflow, 0, output, outputOffset, overflow.Length);
                    outputOffset += overflow.Length;

                    Assert.Equal(3 * (alg.BlockSize / 8), outputOffset);
                    string outputAsHex = output.ByteArrayToHex();
                    Assert.NotEqual(new string('0', outputOffset * 2), outputAsHex.Substring(0, outputOffset * 2));
                    Assert.Equal(new string('0', (output.Length - outputOffset) * 2), outputAsHex.Substring(outputOffset * 2));
                }
        }
Beispiel #24
0
        public static void TripleDESRoundTripNoneCBC(int keySize, string expectedCipherHex, string keyHex)
        {
            byte[] key = keyHex.HexToByteArray();
            byte[] iv  = "5fbc5bc21b8597d8".HexToByteArray();

            using (TripleDES alg = TripleDESFactory.Create())
            {
                alg.Key = key;
                Assert.Equal(keySize, alg.KeySize);

                alg.IV      = iv;
                alg.Padding = PaddingMode.None;
                alg.Mode    = CipherMode.CBC;

                byte[] plainText      = "79a86903608e133e020e1dc68c9835250c2f17b0ebeed91b".HexToByteArray();
                byte[] cipher         = alg.Encrypt(plainText);
                byte[] expectedCipher = expectedCipherHex.HexToByteArray();
                Assert.Equal <byte>(expectedCipher, cipher);

                byte[] decrypted         = alg.Decrypt(cipher);
                byte[] expectedDecrypted = "79a86903608e133e020e1dc68c9835250c2f17b0ebeed91b".HexToByteArray();
                Assert.Equal <byte>(expectedDecrypted, decrypted);
            }
        }
Beispiel #25
0
        public static void TripleDESRoundTripPKCS7CBC(int keySize, string expectedCipherHex, string keyHex)
        {
            byte[] key = keyHex.HexToByteArray();
            byte[] iv  = "8fc67ce5e7f28cde".HexToByteArray();

            using (TripleDES alg = TripleDESFactory.Create())
            {
                alg.Key = key;
                Assert.Equal(keySize, alg.KeySize);

                alg.IV      = iv;
                alg.Padding = PaddingMode.PKCS7;
                alg.Mode    = CipherMode.CBC;

                byte[] plainText      = "e867f915e275eab27d6951165d26dec6dd0acafcfc".HexToByteArray();
                byte[] cipher         = alg.Encrypt(plainText);
                byte[] expectedCipher = expectedCipherHex.HexToByteArray();
                Assert.Equal <byte>(expectedCipher, cipher);

                byte[] decrypted         = alg.Decrypt(cipher);
                byte[] expectedDecrypted = "e867f915e275eab27d6951165d26dec6dd0acafcfc".HexToByteArray();
                Assert.Equal <byte>(expectedDecrypted, decrypted);
            }
        }
Beispiel #26
0
        public static void TripleDESRoundTripZerosCBC(int keySize, string expectedCipherHex, string keyHex)
        {
            byte[] key = keyHex.HexToByteArray();
            byte[] iv  = "95498b5bf570f4c8".HexToByteArray();

            using (TripleDES alg = TripleDESFactory.Create())
            {
                alg.Key = key;
                Assert.Equal(keySize, alg.KeySize);

                alg.IV      = iv;
                alg.Padding = PaddingMode.Zeros;
                alg.Mode    = CipherMode.CBC;

                byte[] plainText      = "f9e9a1385bf3bd056d6a06eac662736891bd3e6837".HexToByteArray();
                byte[] cipher         = alg.Encrypt(plainText);
                byte[] expectedCipher = expectedCipherHex.HexToByteArray();
                Assert.Equal <byte>(expectedCipher, cipher);

                byte[] decrypted         = alg.Decrypt(cipher);
                byte[] expectedDecrypted = "f9e9a1385bf3bd056d6a06eac662736891bd3e6837000000".HexToByteArray();
                Assert.Equal <byte>(expectedDecrypted, decrypted);
            }
        }
Beispiel #27
0
        public static void TripleDES_FailureToRoundTrip192Bits_DifferentPadding_ANSIX923_ZerosECB()
        {
            byte[] key = "9da5b265179d65f634dfc95513f25094411e51bb3be877ef".HexToByteArray();

            using (TripleDES alg = TripleDESFactory.Create())
            {
                alg.Key     = key;
                alg.Padding = PaddingMode.ANSIX923;
                alg.Mode    = CipherMode.ECB;

                byte[] plainText = "77a8b2efb45addb38d7ef3aa9e6ab5d71957445ab8".HexToByteArray();
                byte[] cipher    = alg.Encrypt(plainText);

                byte[] expectedCipher = "149ec32f558b27c7e4151e340d8184f1c90f0a499e20fda9".HexToByteArray();
                Assert.Equal <byte>(expectedCipher, cipher);

                alg.Padding = PaddingMode.Zeros;
                byte[] decrypted         = alg.Decrypt(cipher);
                byte[] expectedDecrypted = "77a8b2efb45addb38d7ef3aa9e6ab5d71957445ab8".HexToByteArray();

                // They should not decrypt to the same value
                Assert.NotEqual <byte>(plainText, decrypted);
            }
        }
        public static void EncryptorReuse_LeadsToSameResults(CipherMode cipherMode, int feedbackSize)
        {
            // AppleCCCryptor does not allow calling Reset on CFB cipher.
            // this test validates that the behavior is taken into consideration.
            var input = "b72606c98d8e4fabf08839abf7a0ac61".HexToByteArray();

            using (TripleDES tdes = TripleDESFactory.Create())
            {
                tdes.Mode = cipherMode;

                if (feedbackSize > 0)
                {
                    tdes.FeedbackSize = feedbackSize;
                }

                using (ICryptoTransform transform = tdes.CreateEncryptor())
                {
                    byte[] output1 = transform.TransformFinalBlock(input, 0, input.Length);
                    byte[] output2 = transform.TransformFinalBlock(input, 0, input.Length);

                    Assert.Equal(output1, output2);
                }
            }
        }
 protected override SymmetricAlgorithm CreateAlgorithm() => TripleDESFactory.Create();