# Sqlcipher encrypt export with byte array password

**URL:** <https://discuss.zetetic.net/t/sqlcipher-encrypt-export-with-byte-array-password/4798>\
**Category:** SQLCipher\
**Created:** [January 25, 2021, 7:31am UTC](https://discuss.zetetic.net/t/sqlcipher-encrypt-export-with-byte-array-password/4798 "2021-01-25T07:31:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eternalblue](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/eternalblue/32/1553_2.png) [@eternalblue](https://discuss.zetetic.net/u/eternalblue)\
**Post date:** [January 25, 2021, 7:31am UTC](https://discuss.zetetic.net/t/sqlcipher-encrypt-export-with-byte-array-password/4798/1 "2021-01-25T07:31:32Z")

</div>

This is database encryption example in github  
(sqlcipher-android-tests/ImportUnencryptedDatabaseTest.java at master · sqlcipher/sqlcipher-android-tests · GitHub)

database = SQLiteDatabase.openOrCreateDatabase(unencryptedDatabase, “”, null);  
database.rawExecSQL(String.format(“ATTACH DATABASE ‘%s’ AS encrypted KEY ‘%s’”,  
encryptedDatabase.getAbsolutePath(), ZeteticApplication.DATABASE\_PASSWORD));  
database.rawExecSQL(“select sqlcipher\_export(‘encrypted’)”);  
database.rawExecSQL(“DETACH DATABASE encrypted”);  
database.close();

after that, I cannot open with same password.  
Because there’s difference between byte array password and converted string.  
rawExecSQL using only string format.

If I open database like below, it opened. (byte password)

SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(sourceDbFile.getAbsolutePath(), “”, null);  
destDbFile.getAbsolutePath().length()-3);  
db.rawExecSQL(String.format(“ATTACH DATABASE ‘%s’ AS encrypted KEY ‘%s’;”, destDbFile.getAbsolutePath(), password));  
db.rawExecSQL(“SELECT sqlcipher\_export(‘encrypted’)”);  
db.rawExecSQL(“DETACH DATABASE encrypted;”);  
db.close();

db.SQLiteDatabase.openOrCreateDatabase(destDbFile, String.valueOf(password), null)

Is there another way to passing byte array parameter?

---

<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:** [January 25, 2021, 2:11pm UTC](https://discuss.zetetic.net/t/sqlcipher-encrypt-export-with-byte-array-password/4798/2 "2021-01-25T14:11:52Z")

</div>

Hi @eternalblue

You can use the `?` within the string for binding parameter values. Would you give that a try?

---

<div class="post-metadata">

**Author:** ![eternalblue](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/eternalblue/32/1553_2.png) [@eternalblue](https://discuss.zetetic.net/u/eternalblue)\
**Post date:** [January 26, 2021, 12:28am UTC](https://discuss.zetetic.net/t/sqlcipher-encrypt-export-with-byte-array-password/4798/3 "2021-01-26T00:28:26Z")

</div>

Thanks for reply.  
I just want to bind parameter by byte array value like below.

(byte password)  
db.rawExecSQL(String.format(“ATTACH DATABASE ‘%s’ AS encrypted KEY ‘%s’;”, destDbFile.getAbsolutePath(), **password** ));

getWritableDatabse, getReadableDatabase API can get byte array value,  
but there’s no way to passing byte array when sqlcipher\_export query.

---

<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:** [January 26, 2021, 8:13pm UTC](https://discuss.zetetic.net/t/sqlcipher-encrypt-export-with-byte-array-password/4798/4 "2021-01-26T20:13:32Z")

</div>

Hi @eternalblue

You can use `execSQL(...)` to pass an `Object[]` for your parameters. You can do that in conjunction with using the `?` operator within your SQL statement itself.

---

<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:** [January 26, 2021, 10:09pm UTC](https://discuss.zetetic.net/t/sqlcipher-encrypt-export-with-byte-array-password/4798/5 "2021-01-26T22:09:26Z")

</div>

Hi @eternalblue

I just wanted to call attention to some of our guidance around keying a database, please make sure you are using a valid UTF-8 sequence of key material when opting for a byte array.

> **[Technical Guidance: Using Random Values as SQLCipher Keys](https://www.zetetic.net/blog/2019/06/07/technical-guidance-using-random-values-as-sqlcipher-keys/)**
>
> Technical guidance on using random values as SQLCipher keys.
