title: Decrypt "Saved Password" from Chrome in C
body: <p>As a part of a project, we are trying to decrypt saved password in Google Chrome. After little digging we understand that Chrome encrypt the password using CryptProtectData function which let you encrypt the data using login user credential i.e. the encrypted data can only be decrypted on same machine by same user (other users also if appropiate parameters are provided).</p>
  
  <p>Anyhow, I have previously wrote a Python script to do exactly that and it works perfectly fine.</p>
  
  <pre><code>from os import getenv, unlink
  from shutil import copy
  import sqlite3
  import win32crypt
  
  dbpath = "C:\Users\\"+ getenv('username') +"\AppData\Local\Google\Chrome\User Data\Default\\"
  copy(dbpath + "Login Data", dbpath + "Login Data.db")
  conn = sqlite3.connect(dbpath + "Login Data.db")
  cursor = conn.cursor()
  cursor.execute('SELECT action_url, username_value, password_value FROM logins')
  for result in cursor.fetchall():
      password = win32crypt.CryptUnprotectData(result[2], None, None, None, 0)[1]
      print result[0] + " - " + result[1] + ":" + password
  conn.close()
  unlink(dbpath + "Login Data.db")
  </code></pre>
  
  <p>But due to project requirement, i have to code it in C language.
  So i wrote the following code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;Wincrypt.h&gt;
  #include "sqlite3.h"
  
  static int callback(void *data, int argc, char **argv, char **azColName){
     int i;
     fprintf(stderr, "%s: \n", (const char*)data);
     for(i=0; i&lt;argc; i++){
        if (i == 2 ){
          DATA_BLOB DataIn;
          DATA_BLOB DataOut;
          BYTE *pbDataInput = (BYTE *) argv[i];
          DWORD cbDataInput = strlen((char *) pbDataInput) + 1;
          DataIn.pbData = pbDataInput;
          DataIn.cbData = cbDataInput;
          if (CryptUnprotectData (&amp;DataIn, NULL, NULL, NULL, NULL, 0, &amp;DataOut))
            printf("%s\n", DataOut.pbData);
          else
            printf("Error number %x.\n", GetLastError());
        }
        // printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
     }
     printf("\n");
     return 0;
  }
  
  int main(int argc, char* argv[])
  {
     sqlite3 *db;
     char *zErrMsg = 0;
     int rc;
     char *sql;
     const char* data = "Callback function called";
  
     /* Open database */
     rc = sqlite3_open("Login Data.db", &amp;db);
     if( rc ){
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        return(0);
     }else{
        fprintf(stderr, "Opened database successfully\n");
     }
  
     /* Create SQL statement */
     sql = "SELECT action_url, username_value, password_value from logins";
  
     /* Execute SQL statement, */
     rc = sqlite3_exec(db, sql, callback, (void*)data, &amp;zErrMsg);
     if( rc != SQLITE_OK ){
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
     }else{
        fprintf(stdout, "Operation done successfully\n");
     }
     sqlite3_close(db);
     return 0;
  }
  </code></pre>
  
  <p>It compiled correctly without any error, but the decrypted data was not correct.</p>
  
  <pre><code>C:\&gt;gcc -o decryptor decryptor.c sqlite3.o -lcrypt32
  
  C:\&gt;decryptor.exe
  Opened database successfully
  Callback function called: Important Data
  Error Number 57.
  
  Operation done successfully
  
  C:\&gt;
  </code></pre>
  
  <p>According to <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx" rel="nofollow noreferrer">System Error Codes (0-499)</a>,
  0x57 is for ERROR_INVALID_PARAMETER (The parameter is incorrect)</p>
  
  <p>What am I missing?</p>