title: C# Error Using ProtectedData.Unprotect() Function
body: <p>I'm experimenting with <code>ProtectData.Protect()</code> and <code>ProtectData.Unprotect()</code> functions in C#. I've written a program that writes encrypted data to an SQLite database, and then reads the database and decrypt the data. Every time I run my code I receive the following error:</p>
  <pre><code>   Unhandled Exception: System.Security.Cryptography.CryptographicException: The parameter is incorrect.
  
     at System.Security.Cryptography.ProtectedData.Unprotect(Byte[] encryptedData, Byte[] optionalEntropy, DataProtectionScope scope)
     at MyProgram.ReadData(SQLiteConnection conn)
     at MyProgram.Main()
  </code></pre>
  <p>The full code is very long, but here is the <code>decrypt()</code> function. I'm passing a byte array; I have verified the type to be a byte array using <code>myByteData.GetType()</code>, which returns <code>System.Byte[]</code>.</p>
  <pre><code>public static byte [] decrypt( byte [] data ) {
          
          byte [] s_additionalEntropy = null;
          
          try {
              
              //Decrypt
              return ProtectedData.Unprotect( data, s_additionalEntropy, DataProtectionScope.CurrentUser );
              
          }catch{
              
              try {
              
                  return ProtectedData.Unprotect( data, s_additionalEntropy, DataProtectionScope.LocalMachine );
                  
              }catch (Exception e){
                  
                  Console.Write(&quot;Error: &quot;);
                  Console.WriteLine(e.Message);
                  return null;
                  
              }
          }
      }
  </code></pre>