Tools

Compiling sqlcipher (sqlite encrypted) for Windows using Visual Studio 2022

4 min read

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 SEESQLCipherSQLiteCrypt, 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

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.

git clone git@github.com:sqlcipher/sqlcipher.git 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.

You can also do the same with the sqlcipher version and not use the 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!