事故现场
KeySize应该是Key的长度*8(单位是bit)
当我设置Key为长度32的字节数组后,(断点可以看到此时KeySize=256)加密结果符合期望;
当我既设置Key(未修改),又设置KeySize=256时,加密结果不同.
public virtual byte[] Key
{
get
{
if (KeyValue == null)
GenerateKey();
return KeyValue.CloneByteArray()!;
}
set
{
ArgumentNullException.ThrowIfNull(value);
long bitLength = value.Length * 8L;
if (bitLength > int.MaxValue || !ValidKeySize((int)bitLength))
throw new CryptographicException(SR.Cryptography_InvalidKeySize);
// must convert bytes to bits
this.KeySize = (int)bitLength;
KeyValue = value.CloneByteArray();
}
}
public virtual int KeySize
{
get
{
return KeySizeValue;
}
set
{
if (!ValidKeySize(value))
throw new CryptographicException(SR.Cryptography_InvalidKeySize);
KeySizeValue = value;
KeyValue = null;
}
}
设置KeySize后,会重置Key(KeyValue = null
)
总结
要么仅设置Key,要么仅设置KeySize
标签:加密,Key,value,KeyValue,KeySize,设置 From: https://www.cnblogs.com/sigeer0-o/p/18333952