# In-memory encryption not working

**URL:** <https://discuss.zetetic.net/t/in-memory-encryption-not-working/3865>\
**Category:** SQLCipher\
**Created:** [August 28, 2019, 12:24pm UTC](https://discuss.zetetic.net/t/in-memory-encryption-not-working/3865 "2019-08-28T12:24:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pradeep](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@pradeep](https://discuss.zetetic.net/u/pradeep)\
**Post date:** [August 28, 2019, 12:24pm UTC](https://discuss.zetetic.net/t/in-memory-encryption-not-working/3865/1 "2019-08-28T12:24:23Z")

</div>

I created one In-memory database and I tried to encrypt and serialize the database by using sqlite3\_serialize() the in-memory database, but the output of sqlite3\_serialize() is a nonencrypted database.

my code:

```
sqlite3* db;
char* ErrMsg;
int status = sqlite3_open(":memory:", &db);

if (sqlite3_exec(db, "PRAGMA key = 'KEY';", 0, NULL, &ErrMsg) == SQLITE_OK)
{

}
if (sqlite3_exec(db, "PRAGMA cipher_hmac_algorithm = HMAC_SHA1;", 0, NULL, &ErrMsg) == SQLITE_OK)
{

}
if (sqlite3_exec(db, "PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;", 0, NULL, &ErrMsg) == SQLITE_OK)
{

}

if (status == SQLITE_OK)
{
	
	int exit = 0;

	char* messaggeError = NULL;
	

	std::string sql = "CREATE TABLE PERSON("
		"ID INT PRIMARY KEY NOT NULL, "
		"NAME TEXT NOT NULL, "
		"SURNAME TEXT NOT NULL, "
		"AGE INT NOT NULL, "
		"ADDRESS CHAR(50), "
		"SALARY REAL );";

	exit = sqlite3_exec(db, sql.c_str(), NULL, NULL, &messaggeError);

	std::string insert_query("INSERT INTO PERSON VALUES(1, 'STEVE', 'GATES', 30, 'PALO ALTO', 1000.0);"
		"INSERT INTO PERSON VALUES(2, 'BILL', 'ALLEN', 20, 'SEATTLE', 300.22);"
		"INSERT INTO PERSON VALUES(3, 'PAUL', 'JOBS', 24, 'SEATTLE', 9900.0);");

	exit = sqlite3_exec(db, insert_query.c_str(), NULL, NULL, &messaggeError);	
	sqlite3_int64 serilized_count;
	unsigned char* serialized_output = sqlite3_serialize(db,0, &serilized_count,0);

```

The output of the sqlite3\_serialize() is non encrypted db;

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [August 28, 2019, 1:42pm UTC](https://discuss.zetetic.net/t/in-memory-encryption-not-working/3865/2 "2019-08-28T13:42:45Z")

</div>

Hi @pradeep

One solution that will work for your scenario is to attach a database with a specified key, then calling [`sqlcipher_export(...)`](https://www.zetetic.net/sqlcipher/sqlcipher-api/#sqlcipher_export). Would you give that a try and let us know your results?

---

<div class="post-metadata">

**Author:** ![pradeep](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@pradeep](https://discuss.zetetic.net/u/pradeep)\
**Post date:** [August 28, 2019, 3:15pm UTC](https://discuss.zetetic.net/t/in-memory-encryption-not-working/3865/3 "2019-08-28T15:15:27Z")

</div>

Thanks for your response  
I am new to sqlite kindly provide example based on my code.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [August 28, 2019, 3:27pm UTC](https://discuss.zetetic.net/t/in-memory-encryption-not-working/3865/4 "2019-08-28T15:27:15Z")

</div>

Hi @pradeep

Please review the documentation for [`sqlcipher_export(...)`](https://www.zetetic.net/sqlcipher/sqlcipher-api/#sqlcipher_export), specifically example #1. Those commands can be executed using `sqlite3_exec` as well.

---

<div class="post-metadata">

**Author:** ![pradeep](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@pradeep](https://discuss.zetetic.net/u/pradeep)\
**Post date:** [August 28, 2019, 3:58pm UTC](https://discuss.zetetic.net/t/in-memory-encryption-not-working/3865/5 "2019-08-28T15:58:31Z")

</div>

I need to encyrpt inmemory db without creating any physical file,it is possible?

---

<div class="post-metadata">

**Author:** ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)\
**Post date:** [August 28, 2019, 4:31pm UTC](https://discuss.zetetic.net/t/in-memory-encryption-not-working/3865/6 "2019-08-28T16:31:02Z")

</div>

For further context, it is not possible to use `sqlite3_serialize()` to get access to encrypted data. `sqlite3_serialize()` creates a representation of the database after it is retrieved through paging system. Therefore, even if the underlying database is encrypted the serialized version of the database will be plaintext in the allocated memory segment returned from the function. You will observe the same behavior even when using an encrypted database file (i.e. when serialized, the resulting memory buffer will be plaintext, even if the file is encrypted on disk).

---

<div class="post-metadata">

**Author:** ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)\
**Post date:** [August 28, 2019, 4:31pm UTC](https://discuss.zetetic.net/t/in-memory-encryption-not-working/3865/7 "2019-08-28T16:31:47Z")

</div>

@pradeep - No, that is not a feature that SQLCipher supports at this time.

---

<div class="post-metadata">

**Author:** ![pradeep](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@pradeep](https://discuss.zetetic.net/u/pradeep)\
**Post date:** [August 28, 2019, 4:48pm UTC](https://discuss.zetetic.net/t/in-memory-encryption-not-working/3865/8 "2019-08-28T16:48:39Z")

</div>

There is any other way to backup encrypted in-memory database without physical file.

---

<div class="post-metadata">

**Author:** ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)\
**Post date:** [August 28, 2019, 5:18pm UTC](https://discuss.zetetic.net/t/in-memory-encryption-not-working/3865/9 "2019-08-28T17:18:03Z")

</div>

Hello @pradeep - no, there isn’t really any way to do that with SQLCipher. You could `sqlite3_serialize()` into a block of memory and then encrypt it yourself, but SQLCipher will not it for you.

---

<div class="post-metadata">

**Author:** ![pradeep](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@pradeep](https://discuss.zetetic.net/u/pradeep)\
**Post date:** [August 28, 2019, 5:30pm UTC](https://discuss.zetetic.net/t/in-memory-encryption-not-working/3865/10 "2019-08-28T17:30:51Z")

</div>

Thanks for you reply.
