Question

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

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

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

I get the following errors:

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.

Était-ce utile?

La solution

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.

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);
}

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top