public ValidKeySize ( int bitLength ) : bool | ||
bitLength | int | |
Résultat | bool |
public static SymmetricAlgorithm InitSymmetric(SymmetricAlgorithm algorithm, string password, int key_bit_length) { var salt = new byte[] { 1, 2, 23, 234, 37, 48, 134, 63, 248, 4 }; const int iterations = 234; using (var rfc2898_derive_bytes = new Rfc2898DeriveBytes(password, salt, iterations)) { if (!algorithm.ValidKeySize(key_bit_length)) throw new InvalidOperationException("Invalid size key"); algorithm.Key = rfc2898_derive_bytes.GetBytes(key_bit_length / 8); algorithm.IV = rfc2898_derive_bytes.GetBytes(algorithm.BlockSize / 8); return algorithm; } }
private static SymmetricAlgorithm InitSymmetric(SymmetricAlgorithm algorithm, SecureString password, int keyBitLength, byte[] salt) { const int Iterations = 1000; using (var secureStringBytes = new SecureStringBytes(password)) { using (var rfc2898DeriveBytes = new Rfc2898DeriveBytes(secureStringBytes.GetBytes(), salt, Iterations)) { if (!algorithm.ValidKeySize(keyBitLength)) { throw new InvalidOperationException("Invalid size key"); } algorithm.Key = rfc2898DeriveBytes.GetBytes(keyBitLength / 8); algorithm.IV = rfc2898DeriveBytes.GetBytes(algorithm.BlockSize / 8); return algorithm; } } }
private Tuple<byte[], byte[]> GetStartingKeyAndIVForEncryption(SymmetricAlgorithm algorithm) { int bits = algorithm.ValidKeySize(EncryptionSettings.PreferedEncryptionKeyBitsSize) ? EncryptionSettings.PreferedEncryptionKeyBitsSize : algorithm.LegalKeySizes[0].MaxSize; encryptionKeySize = bits / 8; encryptionIVSize = algorithm.IV.Length; var deriveBytes = new Rfc2898DeriveBytes(EncryptionSettings.EncryptionKey, GetSaltFromEncryptionKey(EncryptionSettings.EncryptionKey), Constants.Rfc2898Iterations); return Tuple.Create(deriveBytes.GetBytes(encryptionKeySize.Value), deriveBytes.GetBytes(encryptionIVSize.Value)); }