我正在编写一个加密敏感数据的asp.net应用程序,该应用程序由同一域中不同用户帐户上运行的另一个asp.net应用程序解密。
我读了很多文章,说使用DPAPI将密钥管理传递到操作系统级别。
如何在此场景中使用DPAPI?我不想将加密密钥存储在文件或数据库中。
发布于 2011-02-11 18:14:48
您需要引用System.Security,并拥有类似于此的代码(它是VB.NET,但它被简单地移植到C#):
Imports System.Security.Cryptography
' ....
Dim sensitiveDataBytes() As Byte = Encoding.Unicode.GetBytes(sensitiveData)
Dim entropy As Byte() = Guid.NewGuid().ToByteArray()
Dim encryptedSensitiveDataBytes() As Byte = ProtectedData.Protect(sensitiveDataBytes, entropy, DataProtectionScope.LocalMachine)
Dim entropyPlusSensitiveData As Byte() = entropy.Concat(encryptedSensitiveDataBytes).ToArray()
Return entropyPlusSensitiveData您在这里所做的是使用System.Security.Cryptography.ProtectedData来使用DPAPI来保护具有“本地机器”作用域的数据,然后创建一些随机的16字节熵,并将其添加到加密数据的前面。然后,您可以安全地传递16+(加密数据长度)-sized数组。
在解密方面,您可以使用类似的技巧:剥离16个熵字节,然后使用DPAPI进行解密:
Dim entropyPlusSensitiveData As Byte() = data ' the byte array created previously
Dim entropy() As Byte = entropyPlusSensitiveData.Take(16).ToArray()
Dim encryptedSensitiveDataBytes() As Byte = entropyPlusSensitiveData.Skip(16).ToArray()
Dim sensitiveDataBytes() As Byte = ProtectedData.Unprotect(encryptedSensitiveDataBytes, entropy, DataProtectionScope.LocalMachine)熵并不是严格要求的,但强烈建议使用。
https://stackoverflow.com/questions/4967657
复制相似问题