本文整理汇总了C#中System.Security.Cryptography.SymmetricAlgorithm.ValidKeySize方法的典型用法代码示例。如果您正苦于以下问题:C# SymmetricAlgorithm.ValidKeySize方法的具体用法?C# SymmetricAlgorithm.ValidKeySize怎么用?C# SymmetricAlgorithm.ValidKeySize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.SymmetricAlgorithm
的用法示例。
在下文中一共展示了SymmetricAlgorithm.ValidKeySize方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InitSymmetric
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;
}
}
示例2: InitSymmetric
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;
}
}
}
示例3: GetStartingKeyAndIVForEncryption
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));
}