/// <summary>
        /// Initializes a new instance of the EncryptionProvider class.
        /// </summary>
        /// <param name="algorithm">The symmetric algorithm to use during cryptographic transformations.</param>
        /// <param name="padding">The padding mode to use.</param>
        /// <param name="mode">The cipher mode to use.</param>
        /// <param name="key">The key to use.</param>
        /// <param name="iv">The initialization vector to use.</param>
        public EncryptionProvider(SymmetricAlgorithm algorithm, byte[] key, byte[] iv)
        {
            _algorithm = algorithm;
            //            _algorithm.Padding = PaddingMode.PKCS7;
            //            _algorithm.Mode = CipherMode.CBC;
            _algorithm.Key = key;
            _algorithm.IV = iv;

            foreach (KeySizes keySize in _algorithm.LegalKeySizes)
                Debug.WriteLine(string.Format("Algorithm: {0}, KeyMaxSize: {1}, KeyMinSize: {2}", _algorithm.GetType().Name, keySize.MaxSize, keySize.MinSize));
        }
        /// <summary>
        /// Initializes an instance of the SecuritySession_KnownSymmetric class.
        /// </summary>
        /// <param name="symmetricAlgorithm">The symmetricAlgorithm to be used.</param>
        /// <param name="name">The name of the security context.</param>
        public SecuritySession_KnownSymmetric(SymmetricAlgorithm symmetricAlgorithm, string name)
            : base(name, null)
        {
            // LOG:
            BinaryLogWriter binaryLogWriter = GenuineLoggingServices.BinaryLogWriter;
            if (binaryLogWriter != null && binaryLogWriter[LogCategory.Security] > 0 )
            {
                binaryLogWriter.WriteEvent(LogCategory.Security, "SecuritySession_KnownSymmetric.SecuritySession_KnownSymmetric",
                    LogMessageType.SecuritySessionKey, null, null, this.Remote, null,
                    GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name, this,
                    name, -1,
                    0, 0, 0, "Encryption using " + symmetricAlgorithm.GetType().ToString(), null, null, null,
                    "Security Session security information is initialized.");
            }

            this.SymmetricAlgorithm = symmetricAlgorithm;
            this._encryptor = this.SymmetricAlgorithm.CreateEncryptor();
            this._decryptor = this.SymmetricAlgorithm.CreateDecryptor();
            this.IsEstablishedEvent.Set();
        }
Beispiel #3
0
 private void GenerateKey(SymmetricAlgorithm alg)
 {
     alg.GenerateKey();
       alg.GenerateIV();
       Debug.WriteLine("Algorithm: " + alg.GetType().Name.PadRight(30) + " keySize(bits): " + alg.KeySize + " key: " + HexUtil.ByteArrayToHex(alg.Key));
 }
Beispiel #4
0
        /// <summary>
        /// The encrypt.
        /// </summary>
        /// <param name="plainText">
        /// The plain text.
        /// </param>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <param name="iv">
        /// The iv.
        /// </param>
        /// <param name="desProvider">
        /// The des provider.
        /// </param>
        /// <returns>
        /// </returns>
        private static byte[] Encrypt(byte[] plainText, byte[] key, byte[] iv, SymmetricAlgorithm desProvider)
        {
            int appendLength = 8 - (plainText.Length % 8);
            appendLength = appendLength == 8 ? 0 : appendLength;
            if (appendLength != 0)
            {
                var newArray = new byte[plainText.Length + appendLength];
                Array.Copy(plainText, newArray, plainText.Length);
                plainText = newArray;
            }

            // ICryptoTransform cryptoTransform = desProvider.CreateEncryptor(desProvider.Key, desProvider.IV);

            // 密钥
            MethodInfo mi = desProvider.GetType().GetMethod(
                "_NewEncryptor", BindingFlags.NonPublic | BindingFlags.Instance);
            object[] param = { key, CipherMode.ECB, iv, desProvider.FeedbackSize, 0 };
            var desEncrypt = (ICryptoTransform)mi.Invoke(desProvider, param);

            // var ms = new MemoryStream();
            // var myCryptoStream = new CryptoStream(ms, desEncrypt, CryptoStreamMode.Write);
            // myCryptoStream.Write(plainText, 0, plainText.Length);
            // /* 密文 */
            // return ms.ToArray();
            return desEncrypt.TransformFinalBlock(plainText, 0, plainText.Length);
        }
Beispiel #5
0
        /// <summary>
        /// The decrypt.
        /// </summary>
        /// <param name="cipherText">
        /// The cipher text.
        /// </param>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <param name="iv">
        /// The iv.
        /// </param>
        /// <param name="desProvider">
        /// The des provider.
        /// </param>
        /// <returns>
        /// </returns>
        private static byte[] Decrypt(byte[] cipherText, byte[] key, byte[] iv, SymmetricAlgorithm desProvider)
        {
            /* DES解密 */

            // ICryptoTransform cryptoTransform = desProvider.CreateDecryptor(desProvider.Key, desProvider.IV);
            // 密钥
            MethodInfo mi = desProvider.GetType().GetMethod(
                "_NewEncryptor", BindingFlags.NonPublic | BindingFlags.Instance);
            object[] param = { key, CipherMode.ECB, iv, desProvider.FeedbackSize, 1 };
            var desEncrypt = (ICryptoTransform)mi.Invoke(desProvider, param);

            // var ms = new MemoryStream();
            // var myCryptoStream = new CryptoStream(ms, desEncrypt, CryptoStreamMode.Write);
            // myCryptoStream.Write(cipherText, 0, cipherText.Length);
            // return ms.ToArray();
            return desEncrypt.TransformFinalBlock(cipherText, 0, cipherText.Length);
        }
 public SymmetricCipherPair(byte[] cipher, byte[] iv, SymmetricAlgorithm alg)
     : base(cipher, iv)
 {
     Condition.Requires(alg).IsNotNull();
     this.AlgorithmType = alg.GetType();
 }