Exemple #1
0
        /// <summary>
        /// Encrypts the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="key">The key.</param>
        /// <param name="initialVector">The initial vector. 16 ASCII characters long.</param>
        /// <param name="keySize">
        /// Size of the key. Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)
        /// </param>
        /// <param name="algorithm">The algorithm.</param>
        /// <returns>The encrypted data.</returns>
        public byte[] Encrypt(
            byte[] data,
            PasswordDeriveBytes key,
            byte[] initialVector,
            int keySize,
            SymmetricAlgorithms algorithm)
        {
            var SymmetricAlgorithm = Symmetrics.FirstOrDefault(x => string.Equals(x.Name, algorithm.ToString(), StringComparison.OrdinalIgnoreCase));

            if (SymmetricAlgorithm == null)
            {
                return(Array.Empty <byte>());
            }
            return(SymmetricAlgorithm.Encrypt(data, key, initialVector, keySize));
        }
Exemple #2
0
        /// <summary>
        /// Creates a key.
        /// </summary>
        /// <param name="algorithm">The algorithm.</param>
        /// <returns>The key</returns>
        public byte[] CreateRandomKey(SymmetricAlgorithms algorithm)
        {
            var SymmetricAlgorithm = Symmetrics.FirstOrDefault(x => string.Equals(x.Name, algorithm.ToString(), StringComparison.OrdinalIgnoreCase));

            if (SymmetricAlgorithm == null)
            {
                return(Array.Empty <byte>());
            }
            return(SymmetricAlgorithm.CreateKey());
        }
Exemple #3
0
        /// <summary>
        /// Encrypts the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="key">The key.</param>
        /// <param name="salt">The salt.</param>
        /// <param name="hashingAlgorithm">The hashing algorithm.</param>
        /// <param name="passwordIterations">The password iterations.</param>
        /// <param name="initialVector">The initial vector. 16 ASCII characters long.</param>
        /// <param name="keySize">
        /// Size of the key. Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)
        /// </param>
        /// <returns>The encrypted data.</returns>
        public byte[] Encrypt(
            byte[] data,
            byte[] key,
            byte[] salt,
            HashingAlgorithms hashingAlgorithm,
            int passwordIterations,
            byte[] initialVector,
            int keySize,
            SymmetricAlgorithms algorithm)
        {
            var SymmetricAlgorithm = Symmetrics.FirstOrDefault(x => string.Equals(x.Name, algorithm.ToString(), StringComparison.OrdinalIgnoreCase));

            if (SymmetricAlgorithm == null)
            {
                return(Array.Empty <byte>());
            }
            key = (byte[])key.Clone();
            return(SymmetricAlgorithm.Encrypt(data, key, salt, hashingAlgorithm, passwordIterations, initialVector, keySize));
        }