Error while passing hex pKey to sqlite3_key

I have encrypted database using “ATTACH DATABASE” and hex key. But while decrypting using same hex-key I am getting error “file is encrypted or is not a database”. is anything wrong while passing the pKey to sqlite3_key(). I am using sqlcipher-2.2.0 version. Please see the code below:

sqlite3_exec(persistence_db, "ATTACH DATABASE \'./encrypted.db\' AS db2 KEY \"x\'02\'\";", NULL, NULL, &zErrMsg);
sqlite3_exec(persistence_db, "SELECT sqlcipher_export('db2');", NULL, NULL, &zErrMsg);    
sqlite3_exec(persistence_db,"DETACH DATABASE db2;",NULL, NULL, NULL)
//above 3 APIs returns SQLITE_OK

//but I got error while decrypting the same encrypted.db

sqlite3_key(persistence_db, "x'02'", 2);
sqlite3_exec(persistence_db, "SELECT count(*) FROM sqlite_master;", NULL, NULL, &zErrMsg);

//this returns with error: file is encrypted or is not a database

Hello @pdp

To use supply a raw hex key in order to avoid key derivation, you would need to provide a hex string of 64 characters which will be converted into 32 bytes for the key. Alternatively, you can also specify the salt in which your hex string would need to be 96 characters, 32 bytes for the key followed by 16 bytes for the salt. You can read more in our documentation here.

Hi @pdp

As a follow-up, you are likely receiving an error when you attempt to key the database with sqlite3_key because the 3rd parameter is the length of the key you are providing in the second argument. In your case, 2 is an incorrect value.