1
我注意到下面的代碼中有以下奇怪的行爲,如果我將Key設置在對象初始值設定項中,它會生成一個隨機密鑰並且不會設置我的密鑰。這是一個小故障?在RijndaelManaged施工期間的奇怪行爲
var algorithm = new RijndaelManaged
{
Mode = CipherMode.CBC,
Key = keyBytes, //if i set the keyBytes here
KeySize = _keySize,
IV = Encoding.ASCII.GetBytes(_initVector),
BlockSize = 128,
Padding = PaddingMode.Zeros
}; // Set encryption mode to Cipher Block Chaining
bool wtf= algorithm.Key.AreEqual(keyBytes);
if (!wtf) // <!-- the Key is not the same here
{
algorithm.Key = keyBytes; // so i end up having to set it again here so that i can decrypt properly
}
我想按定義它不是一個錯誤..但我仍然覺得這是一個奇怪的實現。沒有反映你永遠不會指望會發生。它違背了對象初始化的規則。加上它只是感到骯髒有一個財產偷偷取消另一個。 – drowhunter
由於這些屬性是「公共虛擬」,因此您可以創建自己的類,從「RijndaelManaged」繼承並覆蓋「Key」和「KeySize」。但我會首先尋找它爲什麼以這種方式實施的原因。也許KeyValue有不同的'KeySizeValue'引入了一些其他的錯誤,甚至運行時錯誤等。@drowhunter –
是有道理的,但它引發的問題..如果關鍵數組和密鑰大小需要匹配爲什麼甚至允許它們是根本不同?對我來說似乎很差邏輯。 – drowhunter