コード例 #1
0
ファイル: TripleDESCngTests.cs プロジェクト: hdracer/HIISDA
        /// <summary>
        ///     Utility method to help write AES round-trip tests
        /// </summary>
        private static bool RoundTripHelper(byte[] input,
                                            Type encryptionAlgorithm,
                                            Type decryptionAlgorithm,
                                            Action <SymmetricAlgorithm> encryptionSetup)
        {
            // Encryption parameters
            byte[]      key         = null;
            byte[]      iv          = null;
            CipherMode  cipherMode  = CipherMode.CBC;
            PaddingMode paddingMode = PaddingMode.PKCS7;

            // Round tripping data
            byte[] cipherText = null;

            SymmetricAlgorithm encryptionObject = null;

            try
            {
                // Setup the encryption algorithm
                encryptionObject = (SymmetricAlgorithm)Activator.CreateInstance(encryptionAlgorithm);
                encryptionSetup(encryptionObject);

                // Encrypt the data
                using (MemoryStream ms = new MemoryStream())
                    using (CryptoStream cs = new CryptoStream(ms, encryptionObject.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(input, 0, input.Length);
                        cs.FlushFinalBlock();

                        cipherText = ms.ToArray();
                    }

                // Save the encryption parameters
                key         = encryptionObject.Key;
                iv          = encryptionObject.IV;
                cipherMode  = encryptionObject.Mode;
                paddingMode = encryptionObject.Padding;
            }
            finally
            {
                if (encryptionObject != null)
                {
                    (encryptionObject as IDisposable).Dispose();
                }
            }

            byte[] roundTrip = null;

            // Now verify the data
            SymmetricAlgorithm decryptionObject = null;

            try
            {
                decryptionObject = (SymmetricAlgorithm)Activator.CreateInstance(decryptionAlgorithm);

                decryptionObject.Key     = key;
                decryptionObject.IV      = iv;
                decryptionObject.Mode    = cipherMode;
                decryptionObject.Padding = paddingMode;

                using (MemoryStream ms = new MemoryStream())
                    using (CryptoStream cs = new CryptoStream(ms, decryptionObject.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(cipherText, 0, cipherText.Length);
                        cs.FlushFinalBlock();

                        roundTrip = ms.ToArray();
                    }
            }
            finally
            {
                if (decryptionObject != null)
                {
                    (decryptionObject as IDisposable).Dispose();
                }
            }

            return(Util.CompareBytes(input, roundTrip));
        }