1

我正在尝试使用 .net 应用程序中的 protectedmemory 和 protecteddata 保护字节数据

形成这个网站,http://www.codedigest.com/Articles/Framework/69_Data_Encryption_and_Decryption_using_DPAPI_classes_in_NET.aspx 似乎我只能保护几个字节

而且,我无法获得此处提供的示例http://msdn.microsoft.com/en-us/library/ms229741(v=vs.85).aspx运行

我收到以下错误:

未声明名称“MemoryProtectionScope”。(BC30451)
未声明名称“DataProtectionScope”。(BC30451)
未声明名称“ProtectedMemory”。(BC30451)

任何人都可以用其他方法帮助我。

4

1 回答 1

1

是什么让您认为您只能保护该文章的几个字节?API 非常简单 - 请记住,加密不会发生在适当的位置,会返回一个带有加密内容的新数组。

这是使用ProtectedData.Protect返回的完整示例:

void Main()
{
    string data  = new WebClient().DownloadString("http://www.stackoverflow.com");
    var buffer = Encoding.UTF8.GetBytes(data);
    buffer = System.Security.Cryptography.ProtectedData.Protect(buffer, null, System.Security.Cryptography.DataProtectionScope.CurrentUser);
    // Data is now protected.

    // Unprotect
    buffer = System.Security.Cryptography.ProtectedData.Unprotect(buffer, null, System.Security.Cryptography.DataProtectionScope.CurrentUser);  
    string decrypted = Encoding.UTF8.GetString(buffer);
    Debug.Assert(data == decrypted);
}

此外,您需要添加对 System.Security 程序集的引用。

于 2012-02-12T16:29:57.960 回答