我可以使用pysqlcipher创建一个加密的数据库,并使用pysqlcipher打开它,但是当从源代码安装sqlcipher时,我无法使用安装在Mac OS X上的sqlcipher命令行工具打开同一个数据库。对于安装,我遵循了下面列出的步骤:https://github.com/leapcode/pysqlcipher/issues/17#issuecomment-113776360,因此在这两种情况下,libsqlcipher的版本应该是相同的。
测试脚本

from pysqlcipher import dbapi2 as sqlite
conn = sqlite.connect('test.db')
c = conn.cursor()
c.execute("PRAGMA key='test'")
c.execute('''create table stocks (date text, trans text, symbol text, qty real, price real)''')
c.execute("""insert into stocks values ('2006-01-05','BUY','RHAT',100,35.14)""")
conn.commit()
c.close()


conn = sqlite.connect('test.db')
c = conn.cursor()
c.execute("PRAGMA key='test'")
c.execute("SELECT * FROM stocks;")
print(c.fetchall())
c.close()

输出
$ python ciphertest.py
[(u'2006-01-05', u'BUY', u'RHAT', 100.0, 35.14)]

这一部分如预期。hexdump的输出还确认数据库实际上是加密的:
$ hexdump -C test.db
00000000  65 17 e3 50 08 b6 5c 94  d5 18 10 f1 61 cc 4f 04  |e..P..\.....a.O.|
00000010  18 02 37 43 15 fc 17 b9  36 e4 3c 55 0a 95 db 80  |..7C....6.<U....|
00000020  37 6e f3 71 97 7e 69 e7  61 81 33 c7 24 68 80 80  |7n.q.~i.a.3.$h..|
00000030  32 b1 b0 27 6a 19 22 31  50 29 16 96 48 9b 63 16  |2..'j."1P)..H.c.|
00000040  e2 6a de 7b c8 0b 1d bf  ba 48 29 6c 41 4d 73 36  |.j.{.....H)lAMs6|
00000050  24 19 25 11 66 60 5d 89  e6 d6 d3 07 66 d2 7a 34  |$.%.f`].....f.z4|
00000060  c3 7b f8 e4 3f 41 d2 3c  ab 28 fb 65 9c 6d 88 e2  |.{..?A.<.(.e.m..|
00000070  3f 4a d7 e3 89 50 04 e7  24 36 64 a8 49 65 88 db  |?J...P..$6d.Ie..|

现在,我尝试使用sqlcipher命令行工具打开test.db文件:
$ sqlcipher test.db
SQLCipher version 3.8.8.3 2015-02-25 13:29:11
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> PRAGMA key='test';
sqlite> SELECT * FROM stocks;
Error: file is encrypted or is not a database

作为最后一个测试,我使用相同的密钥从sqlcipher命令行工具创建了一个加密数据库。我希望这个数据库与Python脚本创建的数据库完全相同(这是合理的期望吗?或许他们会有所不同:
$ sqlcipher test2.db
SQLCipher version 3.8.8.3 2015-02-25 13:29:11
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> PRAGMA key='test';
sqlite> create table stocks (date text, trans text, symbol text, qty real, price real);
sqlite> insert into stocks values ('2006-01-05','BUY','RHAT',100,35.14)
   ...> ;
sqlite> .q

一个hexdump证实了它们是不同的:
$ hexdump -C test2.db
00000000  9e 08 4c 64 cb 31 05 b0  f7 73 ce 96 9a 22 72 1c  |..Ld.1...s..."r.|
00000010  7f 3f 59 a6 58 7f 5b ff  18 b1 86 03 93 4d f4 8b  |.?Y.X.[......M..|
00000020  08 41 66 16 67 a4 cf d8  e3 7d c0 ca 62 df 3f 37  |.Af.g....}..b.?7|
00000030  82 82 65 10 f6 69 a4 68  25 cb c7 32 33 4c 89 70  |..e..i.h%..23L.p|
00000040  1d d9 fe 4d ae eb 73 67  77 13 c9 a5 3e 5e ad a6  |...M..sgw...>^..|
00000050  77 dc b9 62 63 ed f5 41  ad 93 d7 08 11 d7 9e 4f  |w..bc..A.......O|
00000060  85 55 e7 2e 2a e8 8e 46  e0 4d 02 e2 75 ec c7 51  |.U..*..F.M..u..Q|
00000070  9d b7 9e 2a 91 b9 fd a7  de 2f 12 4b 2f 47 e5 cc  |...*...../.K/G..|

我不确定这是我这边的错误,还是pysqlcipher库的问题。为了安全起见,我非常感谢你的建议!

最佳答案

我做了一些和你类似的事情。
在最新版本的sqlcipher(如3.1.0或3.3.0)中,它的cipher默认值是64000。
但我的Pysqlchipher可能和你的一样,是4000。
所以你可以像
“PRAGMA cipher_default_kdf_iter=4000;”
在你的sqlcipher.exe中。
希望对你有帮助。

关于python - 无法在命令行sqlcipher工具中打开pysqlcipher加密的字典,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30955716/

10-14 13:39