Exemplo n.º 1
0
        /// <summary>
        /// Encrypts the specified data using a symmetric key algorithm.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="key">The key.</param>
        /// <param name="initializationVector">The initialization vector.</param>
        /// <param name="method">The encryption method.</param>
        /// <returns></returns>
        public static byte[] Encrypt(
            byte[] data, byte[] key, byte[] initializationVector, SymmetricEncryptionMethod method)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            using (SymmetricAlgorithm provider = GetSymmetricEncryptionAlgorithm(method))
            {
                // assigned the specified key and IV as
                // a pair is created upon creation of the provider
                provider.Key = key;
                provider.IV  = initializationVector;
                ICryptoTransform encryptor = provider.CreateEncryptor();

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                    {
                        cs.Write(data, 0, data.Length);
                        cs.FlushFinalBlock();
                        return(ms.ToArray());
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Encrypts the specified data using a symmetric key algorithm.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="key">The key.</param>
        /// <param name="initializationVector">The initialization vector.</param>
        /// <param name="method">The method.</param>
        /// <param name="encoding">The encoding of parameter <c>data</c>.</param>
        /// <returns></returns>
        public static byte[] Encrypt(
            string data, byte[] key, byte[] initializationVector, SymmetricEncryptionMethod method, Encoding encoding)
        {
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            return(Encrypt(encoding.GetBytes(data), key, initializationVector, method));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Decrypts he specified data to string using a symmetric key algorithm.
        /// </summary>
        /// <param name="encryptedData">The encrypted data.</param>
        /// <param name="key">The key.</param>
        /// <param name="initializationVector">The initialization vector.</param>
        /// <param name="method">The method.</param>
        /// <param name="encoding">The encoding of the string to be encoded.</param>
        /// <returns></returns>
        public static string DecryptToString(
            byte[] encryptedData, byte[] key, byte[] initializationVector, SymmetricEncryptionMethod method, Encoding encoding)
        {
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            byte[] decryptedBytes = Decrypt(encryptedData, key, initializationVector, method);
            return(encoding.GetString(decryptedBytes));
        }
Exemplo n.º 4
0
        private static SymmetricAlgorithm GetSymmetricEncryptionAlgorithm(SymmetricEncryptionMethod method)
        {
            switch (method)
            {
            case SymmetricEncryptionMethod.AES:
                return(new AesCryptoServiceProvider());

            case SymmetricEncryptionMethod.DES:
                return(new DESCryptoServiceProvider());

            case SymmetricEncryptionMethod.TripleDES:
                return(new TripleDESCryptoServiceProvider());

            case SymmetricEncryptionMethod.Rijndael:
                return(new RijndaelManaged());

            default:
                throw new NotSupportedException(
                          string.Format("Method [{0}] is not supported.", method.ToString()));
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Decrypts the specified encrypted data using a symmetric key algorithm.
        /// </summary>
        /// <param name="encryptedData">The encrypted data.</param>
        /// <param name="key">The key.</param>
        /// <param name="initializationVector">The initialization vector.</param>
        /// <param name="method">The encryption method.</param>
        /// <returns></returns>
        public static byte[] Decrypt(
            byte[] encryptedData, byte[] key, byte[] initializationVector, SymmetricEncryptionMethod method)
        {
            using (SymmetricAlgorithm provider = GetSymmetricEncryptionAlgorithm(method))
            {
                // assigned the specified key and IV as
                // a pair is created upon creation of the provider
                provider.Key = key;
                provider.IV  = initializationVector;
                ICryptoTransform decryptor = provider.CreateDecryptor();

                using (MemoryStream ms = new MemoryStream(encryptedData))
                {
                    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    {
                        using (MemoryStream os = new MemoryStream())
                        {
                            cs.CopyTo(os);
                            return(os.ToArray());
                        }
                    }
                }
            }
        }