Unable to properly decrypt data in a file using ProtectedData.Unprotect() (.Net 4 DPAPI) RRS feed

  • Question

  • Hello,


    I'm trying to encrypt/decrypt some data in a cleartext file, and I get some problems at the decryption side of the operation.


    I followed the documentation's examples -adapted for my needs- and still stumble. Here's how I proceeded.


    First, I encrypt the data:

    private static string encrypt(string data2encrypt)
       byte[] entropy = { 2, 6, 9, 9 };
       new RNGCryptoServiceProvider().GetBytes(entropy);
       byte[] encryptedData = ProtectedData.Protect(UnicodeEncoding.ASCII.GetBytes(data2encrypt),<br/>   entropy, DataProtectionScope.CurrentUser);
       return UnicodeEncoding.ASCII.GetString(encryptedData);

    (the entropy[] array is temporary, I wanted to keep things simple for my tests; later on it'll be generated differently).


    The string is returned to the calling method and inserted in the file stream. Looks OK when I open the resulting file.


    Now, if I want to decrypt it:


    private static string decrypt(string encryptedData)
      string dataClear = "";
      byte[] entropy = { 2, 6, 9, 9 };
      new RNGCryptoServiceProvider().GetBytes(entropy);
        dataClear = UnicodeEncoding.ASCII.GetString(ProtectedData.Unprotect
            (UnicodeEncoding.ASCII.GetBytes(encryptedData), entropy,
      catch (CryptographicException cEx)
        dataClear = cEx.Message;
      return dataClear;

    That's where it fails... dataClear always return "Provider DLL could not be found.".

    I just don't know of "provider of what" we're talking about here !


    Any ideas ?


    -- Jeff

    Thursday, July 21, 2011 2:18 PM

All replies

  • Check the file that needs to encrypt and decrypt.
    Hard hard work, Day day up!
    Sunday, July 24, 2011 6:10 PM
  • Not sure I understand your reply ?


    Anyway, the file is not completely encrypted, only some parts (fields) are.


    Most of the file is in cleartext and remains so, it's the fields that are encrypted that throw the errors.


    -- Jeff

    Monday, July 25, 2011 12:08 AM