在Microsoft.AspNetCore.DataProtection中使用的IV,具有默认加密算法AES-256-C



我使用Microsoft.AspNetCore.DataProtection使用默认算法(AES-256-CBC(对数据进行加密和解密。根据我的发现,我知道,给定相同的IV和相同的明文,这种加密一次又一次地产生相同的密文。我有一个用例,我需要为一个我可能已经加密并存储在某个数据库中的纯文本进行数据查找。我没有从数据库中提取并解密数据以检查匹配的选项。

代码示例,

public class MyClass 
{
IDataProtector dataProtector;
IMyStoreRepository externalStore;
public MyClass(IDataProtectionProvider dataProtectionProvider, IMyStoreRepository externalStore) 
{
this.dataProtector = dataProtectionProvider.CreateProtector("somePurposeString");
this.externalStore = externalStore;
}
public string GetOrAddValue(string someKey)
{
string encryptedKey = this.dataProtector.Protect(someKey); // encrypt the given key
if (this.externalStore.KeyExists(encryptedKey) // look up in the external store
{
return this.externalStore.GetValue(encryptedKey); // return the value if match in external store
}
string someValue = "foo-bar-foo-bar"; 
this.externalStore.Set(encryptedKey, someValue); // setting the value in the external store with encrypted key
return someValue;
}
}

我在Program.cs中注入了与数据保护相关的依赖项,并使用了大多数默认配置。

我的问题是:

  1. 如果我按照上面的代码使用IV,用于加密的IV是什么
  2. 如果我以上述方式使用它,它会为给定的明文产生相同的密文吗。假设主密钥在所有加密和解密过程中都是恒定的

根据这些文档,每个Encrypt调用都会生成一个单独的密钥和一个随机初始化向量(IV(,至少使用默认设置,即用于有效载荷保护的AES-256-CBC和用于真实性的HMACSHA256。由于这个原因,我们不能生成与给定的明文对应的相同密文。

最新更新