Esempio n. 1
0
        public static SymmetricKey GetKey(string password, string salt = null, SymmetricKeySize keySize = defaultKeySize, SymmetricBlockSize blockSize = defaultBlockSize)
        {
            var saltBytes = SaltFromPassword(password, salt);

            using (var deriveBytes = new Rfc2898DeriveBytes(password, saltBytes))
            {
                var keySizeValue   = (int)keySize;
                var blockSizeValue = (int)blockSize;
                var keyBytes       = deriveBytes.GetBytes(keySizeValue / 8);
                var ivBytes        = deriveBytes.GetBytes(blockSizeValue / 8);

                var symmetricKey = new SymmetricKey(keyBytes, ivBytes);
                return(symmetricKey);
            }
        }
Esempio n. 2
0
 /// <summary>Initializes a new instance of the <see cref="SymmetricKeyAlgorithm"/> class.</summary>
 /// <param name="password">The sequence of bytes which is used as password.
 ///     <para>For more information, see <see cref="DestroySecretData()">here</see>.</para>
 /// </param>
 /// <param name="salt">The sequence of bytes which is used as salt.
 ///     <para>For more information, see <see cref="DestroySecretData()">here</see>.</para>
 /// </param>
 /// <param name="iterations">The number of iterations for the operation.</param>
 /// <param name="blockSize">The block size, in bits, of the cryptographic operation.</param>
 /// <param name="keySize">The size, in bits, of the secret key used for the symmetric algorithm.</param>
 /// <exception cref="ArgumentNullException">inputStream, outputStream, password or salt is null.</exception>
 /// <exception cref="ArgumentOutOfRangeException">iterations is less than 1.</exception>
 /// <exception cref="ArgumentException">salt size is smaller than 8 bytes.</exception>
 protected SymmetricKeyAlgorithm(byte[] password, byte[] salt, int iterations, int blockSize, SymmetricKeySize keySize)
 {
     _password = password ?? throw new ArgumentNullException(nameof(password));
     if (salt == null)
     {
         throw new ArgumentNullException(nameof(salt));
     }
     if (salt.Length < 8)
     {
         throw new ArgumentException(ExceptionMessages.ArgumentSizeTooSmall, nameof(salt));
     }
     _salt = salt;
     if (iterations < 1)
     {
         throw new ArgumentOutOfRangeException(nameof(iterations), iterations, null);
     }
     Iterations = iterations;
     BlockSize  = blockSize;
     KeySize    = keySize;
 }
Esempio n. 3
0
        public static SymmetricKey GenerateKey(SymmetricAlgorithmType symmetricAlgorithmType, SymmetricKeySize keySize = defaultKeySize, SymmetricBlockSize blockSize = defaultBlockSize)
        {
            var(symmetricAlgorithm, _)   = GetAlgorithm(symmetricAlgorithmType);
            symmetricAlgorithm.KeySize   = (int)keySize;
            symmetricAlgorithm.BlockSize = (int)blockSize;
            symmetricAlgorithm.GenerateKey();
            symmetricAlgorithm.GenerateIV();
            var symmetricKey = new SymmetricKey(symmetricAlgorithm.Key, symmetricAlgorithm.IV);

            return(symmetricKey);
        }
Esempio n. 4
0
 /// <summary>Initializes a new instance of the <see cref="Rijndael"/> class.</summary>
 /// <param name="password">The sequence of bytes which is used as password.
 ///     <para>For more information, see <see cref="SymmetricKeyAlgorithm.DestroySecretData()">here</see>.</para>
 /// </param>
 /// <param name="salt">The sequence of bytes which is used as salt.
 ///     <para>For more information, see <see cref="SymmetricKeyAlgorithm.DestroySecretData()">here</see>.</para>
 /// </param>
 /// <param name="iterations">The number of iterations for the operation.</param>
 /// <param name="keySize">The size of the secret key.</param>
 /// <inheritdoc cref="SymmetricKeyAlgorithm(byte[], byte[], int, int, SymmetricKeySize)"/>
 public Rijndael(byte[] password, byte[] salt, int iterations = 1000, SymmetricKeySize keySize = SymmetricKeySize.Large) : base(password, salt, iterations, 128, keySize)
 {
 }