Compiling sqlcipher (sqlite encrypted) for Windows using Visual Studio 2022
source link: https://www.domstamand.com/compiling-sqlcipher-sqlite-encrypted-for-windows-using-visual-studio-2022/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Compiling sqlcipher (sqlite encrypted) for Windows using Visual Studio 2022
I was searching for what was available in terms of encryption for SQLite and stumbled upon the Microsoft SQLite documentation for encryption. It states
SQLite doesn’t support encrypting database files by default. Instead, you need to use a modified version of SQLite like SEE, SQLCipher, SQLiteCrypt, or wxSQLite3.
I looked at all the proposed products and saw that SQLCipher was giving a community (open source) version of their product. However, they do not give the compiled binaries that comes with that. So I said to myself, let me try to compile it. This one was a tough one! I could not get it to work as easily as I thought I would. After a few hours playing, I got it to work and wanted to document the step by step for all that would be in a similar scenario.
Shout out to the following 2 resources that I found that helped me in the right direction.
- YouTube video on how to compile
- GitHub repository of a plugin for Gradle that builds sqlcipher for various platforms.
I was able to compile using OpenSSL 3.0.1 and version 4.5.0 of sqlcipher.
Note that I am not a C++ pro here, but used my instincts to try something out and it worked!
Requirements
Visual Studio 2022 C++ tools
You will need to install the compilers for C++. SQLCipher uses nmake (MSVC) and a Makefile.msc. To do that, add the Desktop Development with C++ into your Visual Studio 2022 through the installer.
OpenSSL
Download yourself a copy of OpenSSL. You can find all the 3rd binaries distribution here. I downloaded mine from https://slproweb.com/products/Win32OpenSSL.html.
Make sure to download the none light version (full for developers).
Tcl is required to build SQLite. I downloaded mine from IronTCL. Once you have downloaded it, extract it to a folder and navigate to the bin directory. Copy the file tclsh86t.exe to tclsh.exe. This is because the build looks for tclsh.exe.
Copy the full path of the bin directory. In my case, it was E:\Temp\irontcl-amd64-8.6.7\IronTcl\bin.
Building
We’re now ready to build SQLCipher. Open the developer command prompt for Visual Studio 2022. You can find it in your start menu or just fire up a cmd prompt and type
cmd /k "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\VsDevCmd.bat"
We’re going to compile for 64bit. For this, we need to have our command prompt with the 64bit variables. You can do this by executing the following command:
"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64
If successful, you should see something like
Now we need to put Tcl on our path. Run the following:
SET PATH=%PATH%;E:\Temp\irontcl-amd64-8.6.7\IronTcl\bin
Once that is done, we will set the platform for which we want to build sqlite for, that is 64bit or x64. Run the following:
SET PLATFORM=x64
Download sqlcipher
Clone the repository of sqlcipher into a folder. In my case I cloned it into E:\Temp\GitHub\sqlcipher.
Fixing the Makefile
Since we’re compiling for Windows, we need to fix the Makefile.msc
located in the root of the repository with the following:
1. Find the string -DSQLITE_TEMP_STORE=1
and change 1 to 2. You should change it in 2 places the TCC and RCC variables.
Once you’ve changed those, add the following right below
where C:\Program Files\OpenSSL-Win64
is the folder where you installed OpenSSL.
2. Locate the string LTLIBPATHS = $(LTLIBPATHS) /LIBPATH:$(ICULIBDIR)
right after the !ENDIF
, add the following:
Create binary folder
Create a folder where you want the binaries to be built. I chose to create a folder at the same level as the cloned repository, located at E:\Temp\GitHub\sqlcipher-build
Compiling
In your binary folder ( cd E:\Temp\GitHub\sqlcipher-build), run the following:
nmake /f E:\Temp\GitHub\sqlcipher-build\sqlcipher\Makefile.msc TOP=E:\Temp\GitHub\sqlcipher
Replace the folders with your appropriate folders. You should now find sqlite3.exe and sqlite3.dll in the directory.
Testing
To test, download yourself a copy of sqlite for Windows. Make sure to get the tools package, that include sqlite.exe etc. Once you downloaded it, extract it to a folder and execute the following commands in a PowerShell prompt to create a database and add data.
In the same prompt, let’s look at the hexdump of the database file using the command
Format-Hex testnoencryption.db
You should see something alone the lines of
Now repeating the same exercise with the sqlcipher binary
The command PRAGMA key
is to set the password for encryption.
and the hexdump
You can see the difference between the encrypted and unencrypted database files.
PRAGMA Key
command, but here it was for demo purposes.Conclusion
We have now compiled the binary for sqlcipher. If you want to keep only what is necessary, copy the following files wherever you see fit:
Hope this can help some.
Shout out to SQLCipher for the community version!
Recommend
-
48
README.md SQLCipher SQLCipher extends the SQLite database library to add security enhancements that make it more s...
-
8
Discussion on /r/rust , HN ,
-
10
使用 SSMS/Visual Studio 查詢 SQLite-黑暗執行緒認識 SQLite 後,它成為我單機程式或小型網站的資料庫首選,免費免安裝還跨平台,應付幾萬筆資料十來人同時連線綽綽有餘。大家正在閱讀的這個部落格,背後也是靠 SQLite 撐起來的。
-
5
SQLite开发文档:SQLCipher加密 2021-05-14 22:30:12 / easeapi.com / 错误反馈
-
2
Introduction Last November I added a new job to our CI to cross compile our project for x86_64-pc-windows-msvc from an x86_64-unknown-linux-gnu host. I had wanted to blog about that at the time but never got...
-
6
Compiling SQLite for use with Python Applications July 13, 2018 16:28 / python
-
8
Compiling ZeroMQ for Windows in Centos 7 2/Mar 2015...
-
1
Selekt Selekt is a Kotlin and familiar Android wrapper over the community edition of SQLCipher, an SQLite extension that provides 256-bit AES encryption of database files....
-
5
Because cross-compiling binaries for Windows is easier than building natively I want Microsoft to do better, want Windows to be a decent development platform-and yet, I constantly see Microsoft playing the
-
4
sqlite简介 本人最近在写一个小的安卓项目,开发app过程中用到了安卓自带的sqlite。本文主要对sqlite图片操作进行介绍,其他存入文本之类的操作和普通数据库一样,众所周知,sqlite是一款轻型的数据库,以下先简单介绍一下sqlite,为后续做铺垫,有了解的...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK