保护字节的数据。NET字节、数据、NET

2023-09-08 08:37:04 作者:哆啦只是个梦

我试图用protectedmemory和protecteddata在.NET应用程序,以保护字节的数据

am trying to protect bytes data using the protectedmemory and protecteddata in .net application

形成这个网站,http://www.$c$cdigest.com/Articles/Framework/69_Data_Encryption_and_Decryption_using_DPAPI_classes_in_NET.aspx  就是好像我不仅可以保护几个字节

form this site, http://www.codedigest.com/Articles/Framework/69_Data_Encryption_and_Decryption_using_DPAPI_classes_in_NET.aspx is seems i can only protect a few bytes

和也,我不能让样品此处提供的 http://msdn.microsoft.com/en-us/library/ms229741(V = vs.85)的.aspx 运行

and also, i cannot get the sample provided here http://msdn.microsoft.com/en-us/library/ms229741(v=vs.85).aspx to run

我收到以下错误:

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

Name 'MemoryProtectionScope' is not declared. (BC30451) Name 'DataProtectionScope' is not declared. (BC30451) Name 'ProtectedMemory' is not declared. (BC30451)

谁能帮我这样做的其他方法。

can anyone help me with other methods of doing this.

推荐答案

是什么让你认为你只能保护该文章的几个字节?这个API是相当简单 - 请记住,加密不会发生在地方,一个新的数组与加密内容返回

What makes you think that you can only protect a few bytes from that article ? The API is quite simple - remember that the encryption doesn't happen in place, a new array is returned with the encrypted content.

下面是使用一个完整的例子 ProtectedData.Protect 和回:

Here is a full example of using ProtectedData.Protect and back:

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程序。

Also, you will need to add a reference to the System.Security assembly.