VS2008 编译SQLite 3.8.4.3 + sqlcipher-3.1.0 + openssl-1.0.1g

本文详细介绍了如何使用SQLCipher库实现SQLite数据库文件的256位AES加密过程,包括下载源码、编译openssl、创建win32dll项目、集成加密功能及使用方法等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SQLCipher是一个开源库,提供透明,安全的256位AES加密的SQLite数据库文件。
SQLCipher的社区版的源代码是一个BSD-风格的开源许可下发布,但是官方提供的二进制库需要购买。

引用官方的一张图,加密前和加密后的对比效果:

1、先下载以下源码文件
SQLite 3.8.4.3 源码

http://www.sqlite.org/2014/sqlite-amalgamation-3080403.zip

sqlite3.def 文件

http://www.sqlite.org/2014/sqlite-dll-win32-x86-3080403.zip

sqlcipher-3.1.0 源码

https://github.com/sqlcipher/sqlcipher/archive/v3.1.0.zip

openssl-1.0.1g源码 已修复 CVE-2014-0160 漏洞

http://www.openssl.org/source/openssl-1.0.1g.tar.gz

2、编译openssl-1.0.1g
系统先要 activeperl 配置openssl 的时候需要调用 http://www.activestate.com/activeperl。
解压源码包进入openssl-1.0.1g目录新键一个build.bat 文件内容如下:


01@echo off
02@set OPTS=no-asm
03@perl Configure VC-WIN32
04@perl util\mkfiles.pl >MINFO
05@perl util\mk1mf.pl %OPTS% debug VC-WIN32 >d32.mak
06@perl util\mk1mf.pl %OPTS% VC-WIN32 >32.mak
07@perl util\mkdef.pl 32 libeay > ms\libeay32.def
08@perl util\mkdef.pl 32 ssleay > ms\ssleay32.def
09@nmake -f d32.mak
10@nmake -f 32.mak

 

然后使用VC命令行进入openssl-1.0.1g目录运行 build.bat 就开始编译并生成 libeay32.lib ssleay32.lib;

3、编译SQLite 创建一个空的win32 dll项目,将解压缩的文件添加进来,进行编译,项目属性–配置属性–连接器–输入–模块定义文件,添加sqlite3.def,和附加依赖项添加 libeay32.lib ssleay32.lib。

在sqlite3.def 文件最后添加以下代码


1sqlite3_key
2sqlite3_rekey

 

在sqlite3.cpp 文件最前添加以下代码


1/*** START REQUIRED BY SQLCIPHER ***/
2#define SQLITE_HAS_CODEC
3#define SQLITE_ENABLE_RTREE
4#define SQLITE_ENABLE_COLUMN_METADATA
5#define SQLITE_TEMP_STORE 2
6/*** END REQUIRED BY SQLCIPHER ***/

 

然后拉到最后添加以下代码


1#include <sqlcipher/crypto.c>      /*** SQLCIPHER ADDITION ***/
2#include <sqlcipher/crypto_cc.c>      /*** SQLCIPHER ADDITION ***/
3#include <sqlcipher/crypto_impl.c> /*** SQLCIPHER ADDITION ***/
4#include <sqlcipher/crypto_openssl.c> /*** SQLCIPHER ADDITION ***/
5#include <sqlcipher/pager.c>       /*** SQLCIPHER ADDITION ***/

 

以上文件 里的这些头文件注示掉
/*#include “sqliteInt.h”*/
/*#include “btreeInt.h”*/

pager.c文件的内容为


01/* BEGIN CRYPTO */
02#ifdef SQLITE_HAS_CODEC
03void sqlite3pager_get_codec(Pager *pPager, void **ctx) {
04    *ctx = pPager->pCodec;
05}
06 
07int sqlite3pager_is_mj_pgno(Pager *pPager, Pgno pgno) {
08    return (PAGER_MJ_PGNO(pPager) == pgno) ? 1 : 0;
09}
10 
11sqlite3_file *sqlite3Pager_get_fd(Pager *pPager) {
12    return (isOpen(pPager->fd)) ? pPager->fd : NULL;
13}
14 
15void sqlite3pager_sqlite3PagerSetCodec(
16                                       Pager *pPager,
17                                       void *(*xCodec)(void*,void*,Pgno,int),
18                                       void (*xCodecSizeChng)(void*,int,int),
19                                       void (*xCodecFree)(void*),
20                                       void *pCodec
21                                       ){
22                                           sqlite3PagerSetCodec(pPager, xCodec, xCodecSizeChng, xCodecFree, pCodec);
23}
24 
25void sqlite3pager_sqlite3PagerSetError( Pager *pPager, int error) {
26    pPager->errCode = error;
27}
28 
29#endif
30/* END CRYPTO */

 

然后再编译可以成功生成sqlite3.dll

4、
使用方法


01#include "sqlite3.h"
02#include <stdio.h>
03 
04#define ERROR(X)  {printf("[ERROR] iteration %d: ", i); printf X;fflush(stdout);}
05 
06int main(int argc, char **argv) {
07  sqlite3 *db;
08  const char *file= "sqlcipher.db";
09 
10  const char *key1 = "test123";
11  char* key = (char *) key1;
12 
13    if (sqlite3_open(file, &db) == SQLITE_OK) {
14      int row, rc, master_rows;
15      sqlite3_stmt *stmt;
16       
17      if(db == NULL) {
18        ERROR(("sqlite3_open reported OK, but db is null, retrying open %s\n", sqlite3_errmsg(db)))
19      }
20  
21      if(sqlite3_key(db, key, strlen(key)) != SQLITE_OK) {
22    ERROR(("error setting key %s\n", sqlite3_errmsg(db)))
23        exit(1);
24      }
25//SQLlite 操作代码...
26 
27}
name: comprehensive_EHR_PT channels: - pytorch - conda-forge - anaconda - defaults dependencies: - asn1crypto=0.24.0=py37_0 - attrs=19.1.0=py_0 - backcall=0.1.0=py_0 - bcrypt=3.1.6=py37h7b6447c_0 - blas=1.0=mkl - bleach=3.1.0=py_0 - blosc=1.15.0=hd408876_0 - bokeh=1.1.0=py37_0 - boto=2.49.0=py37_0 - boto3=1.9.134=py_0 - botocore=1.12.134=py_0 - bottleneck=1.2.1=py37h035aef0_1 - bz2file=0.98=py37_1 - bzip2=1.0.6=h14c3975_5 - ca-certificates=2020.10.14=0 - certifi=2020.6.20=pyhd3eb1b0_3 - cffi=1.12.2=py37h2e261b9_1 - cftime=1.0.3.4=py37hdd07704_0 - chardet=3.0.4=py37_1 - click=7.0=py37_0 - cloudpickle=0.8.1=py_0 - colorama=0.4.1=py37_0 - cryptography=2.6.1=py37h1ba5d50_0 #- cudatoolkit=10.0.130=0 - cudatoolkit=11.1 - curl=7.64.1=hbc83047_0 - cycler=0.10.0=py37_0 - cytoolz=0.9.0.1=py37h14c3975_1 - dask=1.2.0=py_0 - dask-core=1.2.0=py_0 - dbus=1.13.2=h714fa37_1 - decorator=4.4.0=py_0 - defusedxml=0.5.0=py_1 - distributed=1.27.1=py37_0 - docutils=0.14=py37_0 - entrypoints=0.3=py37_1000 - expat=2.2.6=he6710b0_0 - fontconfig=2.13.1=he4413a7_1000 - freetype=2.9.1=h8a8886c_1 - gensim=3.4.0=py37h14c3975_0 - gettext=0.19.8.1=hc5be6a0_1002 - glib=2.56.2=had28632_1001 - gst-plugins-base=1.14.0=hbbd80ab_1 - gstreamer=1.14.0=hb453b48_1 - hdf4=4.2.13=h3ca952b_2 - hdf5=1.10.4=hb1b8bf9_0 - heapdict=1.0.0=py37_2 - icu=58.2=hf484d3e_1000 - idna=2.8=py37_0 - intel-openmp=2019.3=199 - ipykernel=5.1.0=py37h24bf2e0_1002 - ipython=7.4.0=py37h24bf2e0_0 - ipython_genutils=0.2.0=py_1 - ipywidgets=7.4.2=py_0 - jedi=0.13.3=py37_0 - jinja2=2.10.1=py_0 - jmespath=0.9.4=py_0 - jpeg=9b=h024ee3a_2 - jsonschema=3.0.1=py37_0 - jupyter=1.0.0=py_2 - jupyter_client=5.2.4=py_3 - jupyter_console=6.0.0=py_0 - jupyter_contrib_core=0.3.3=py_2 - jupyter_core=4.4.0=py_0 - kiwisolver=1.0.1=py37hf484d3e_0 - krb5=1.16.1=h173b8e3_7 - libcurl=7.64.1=h20c2e04_0 - libedit=3.1.20181209=hc058e9b_0 - libffi=3.2.1=hd88cf55_4 - libgcc-ng=8.2.0=hdf63c60_1 - libgfortran-ng=7.3.0=hdf63c60_0 - libiconv=1.15=h516909a_1005 - libnetcdf=4.6.1=h11d0813_2 - libpng=1.6.36=hbc83047_0 - libpq=11.2=h20c2e04_0 - libsodium=1.0.16=h14c3975_1001 - libssh2=1.8.2=h1ba5d50_0 - libstdcxx-ng=8.2.0=hdf63c60_1 - libtiff=4.0.10=h2733197_2 - libuuid=2.32.1=h14c3975_1000 - libxcb=1.13=h14c3975_1002 - libxml2=2.9.9=h13577e0_0 - libxslt=1.1.32=h4785a14_1002 - locket=0.2.0=py37_1 - lxml=4.3.3=py37h7ec2d77_0 - lzo=2.10=h49e0be7_2 - markupsafe=1.1.1=py37h14c3975_0 - matplotlib=3.0.3=py37h5429711_0 - mistune=0.8.4=py37h14c3975_1000 - mkl=2019.3=199 - mkl_fft=1.0.10=py37ha843d7b_0 - mkl_random=1.0.2=py37hd81dba3_0 - msgpack-python=0.6.1=py37hfd86e86_1 - nbconvert=5.4.1=py_2 - nbformat=4.4.0=py_1 - ncurses=6.1=he6710b0_1 - netcdf4=1.4.2=py37h808af73_0 - ninja=1.9.0=py37hfd86e86_0 - notebook=5.7.8=py37_0 - numexpr=2.6.9=py37h9e4a6bb_0 - numpy=1.16.2=py37h7e9f1db_0 - numpy-base=1.16.2=py37hde5b4d6_0 - olefile=0.46=py37_0 - openssl=1.1.1h=h7b6447c_0 - packaging=19.0=py37_0 - pandas=0.24.2=py37he6710b0_0 - pandoc=2.7.2=0 - pandocfilters=1.4.2=py_1 - paramiko=2.4.2=py37_0 - parso=0.4.0=py_0 - partd=0.3.10=py37_1 - pcre=8.43=he6710b0_0 - pexpect=4.7.0=py37_0 - pickleshare=0.7.5=py37_1000 - pillow=6.0.0=py37h34e0f95_0 - pip=19.0.3=py37_0 - prometheus_client=0.6.0=py_0 - prompt_toolkit=2.0.9=py_0 - psutil=5.6.2=py37h7b6447c_0 - psycopg2=2.7.6.1=py37h1ba5d50_0 - pthread-stubs=0.4=h14c3975_1001 - ptyprocess=0.6.0=py_1001 - pyasn1=0.4.5=py_0 - pycparser=2.19=py37_0 - pygments=2.3.1=py_0 - pynacl=1.3.0=py37h7b6447c_0 - pyopenssl=19.0.0=py37_0 - pyparsing=2.4.0=py_0 - pyqt=5.9.2=py37h05f1152_2 - pyrsistent=0.14.11=py37h14c3975_0 - pysocks=1.6.8=py37_0 - pytables=3.5.1=py37h71ec239_0 - python=3.7.3=h0371630_0 - python-dateutil=2.8.0=py37_0 #- pytorch=1.0.1=py3.7_cuda10.0.130_cudnn7.4.2_2 - pytorch==1.8.0=py3.7_cuda11.1_cudnn8.0.5_0 - pytz=2019.1=py_0 - pyyaml=5.1=py37h14c3975_0 - pyzmq=18.0.1=py37hc4ba49a_1 - qt=5.9.7=h5867ecd_1 - qtconsole=4.4.3=py_0 - readline=7.0=h7b6447c_5 - requests=2.21.0=py37_0 - s3transfer=0.2.0=py37_0 - scikit-learn=0.20.3=py37hd81dba3_0 - scipy=1.2.1=py37h7c811a0_0 - seaborn=0.11.0=py_0 - send2trash=1.5.0=py_0 - setuptools=41.0.0=py37_0 - sip=4.19.8=py37hf484d3e_0 - six=1.12.0=py37_0 - smart_open=1.8.2=py_0 - snappy=1.1.7=hbae5bb6_3 - sortedcontainers=2.1.0=py37_0 - sqlite=3.27.2=h7b6447c_0 - tblib=1.3.2=py37_0 - terminado=0.8.2=py37_0 - testpath=0.4.2=py_1001 - tk=8.6.8=hbc83047_0 - toolz=0.9.0=py37_0 #- torchvision=0.2.2=py_3 - torchvision==0.9.0=py37_cu111 - tornado=6.0.2=py37h516909a_0 - tqdm=4.31.1=py37_1 - traitlets=4.3.2=py37_1000 - urllib3=1.24.2=py37_0 - wcwidth=0.1.7=py_1 - webencodings=0.5.1=py_1 - wheel=0.33.1=py37_0 - widgetsnbextension=3.4.2=py37_0 - xarray=0.12.1=py_0 - xorg-libxau=1.0.9=h14c3975_0 - xorg-libxdmcp=1.1.3=h516909a_0 - xz=5.2.4=h14c3975_4 - yaml=0.1.7=h14c3975_1001 - zeromq=4.3.1=hf484d3e_1000 - zict=0.1.4=py37_0 - zlib=1.2.11=h7b6447c_3 - zstd=1.3.7=h0b5b093_0 - pip: - future==0.18.0 - hyperopt==0.1.2 - networkx==2.3 - pymongo==3.9.0 - pytorch-pretrained-bert==0.6.2 - regex==2019.8.19 - upsetplot==0.4.0 这里哪些包是incompatible的
06-05
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值