ASP.NET Core数据保护跨平台加密提供程序

时间:2018-10-01 19:51:10

标签: .net encryption asp.net-core .net-core core

我正在尝试将asp.net核心DataProtectionProvider与支持服务器场的单个数据库一起使用。我为商店实现了IXmlRepository,并且成功地向运行服务器的2台机器提供了相同的证书。我确认我以相同的顺序使用了相同目的的字符串,但是当我尝试从机器b取消保护机器a编码的字符串时,我得到了通用的CryptographicException。

机器a和机器b是2个不同的操作系统是否重要? (Linux和Windows Server)

我得到的例外是:

  

{System.Security.Cryptography.CryptographicException:的异常   类型'System.Security.Cryptography.CryptographicException'被抛出。   在   Microsoft.AspNetCore.Cryptography.UnsafeNativeMethods.ThrowExceptionForBCryptStatusImpl(Int32   ntstatus)   Microsoft.AspNetCore.Cryptography.UnsafeNativeMethods.ThrowExceptionForBCryptStatus(Int32   ntstatus)   Microsoft.AspNetCore.DataProtection.Cng.GcmAuthenticatedEncryptor.DecryptImpl(Byte *   pbCiphertext,UInt32 cbCiphertext,字节*   pbAdditionalAuthenticatedData,UInt32 cbAdditionalAuthenticatedData)
  在   Microsoft.AspNetCore.DataProtection.Cng.Internal.CngAuthenticatedEncryptorBase.Decrypt(ArraySegment 1 ciphertext, ArraySegment 1 AdditionalAuthenticatedData)在   Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte []   protectedData,布尔allowOperationsOnRevokedKeys,UnprotectStatus&   状态)   Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect(Byte []   protectedData,布尔值ignoreRevocationErrors,布尔值&   requireMigration,Boolean&wasRevoked)   Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte []   protectedData)位于   Microsoft.AspNetCore.DataProtection.DataProtectionCommonExtensions.Unprotect(IDataProtector   保护器,字符串protectedData)

1 个答案:

答案 0 :(得分:2)

我发现提供者具有用于顶级用途的文件夹名称。我假设这是为了隔离存储在注册表中的密钥存储。解决方案是在配置设置中添加ApplicationName,以便它使用它而不是在两台不同计算机上不同的文件夹。

示例:

services.AddDataProtection()
                .UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration()
                {
                    EncryptionAlgorithm = EncryptionAlgorithm.AES_256_GCM,
                    ValidationAlgorithm = ValidationAlgorithm.HMACSHA256
                })
                .SetApplicationName("MyCommonName");