Exemple #1
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);
            }
        }
Exemple #2
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);
            }
        }
Exemple #3
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);
            }
        }
Exemple #4
0
        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);
                }
            }
        }
Exemple #5
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);
            }
        }
 protected override SymmetricAlgorithm CreateAlgorithm() => TripleDESFactory.Create();