• Welcome, Guest. Please login.
 
October 21, 2019, 02:56:16 am

News:

Welcome to the SQLitening support forums!


How to Implement SQLCipher in SQLitening

Started by Calvin Chipman, November 23, 2011, 02:24:19 pm

Previous topic - Next topic

Calvin Chipman

November 23, 2011, 02:24:19 pm Last Edit: November 23, 2011, 02:26:37 pm by Calvin Chipman
I used the following steps to implement SQLCipher .DLL (http://sqlcipher.net/) in SQLitening version 1.54:

Modify the source code for SQLitening.dll by modifying SQLitening.bas as follows:

  • Add the following declarations:

Declare Function UsingSetKey(ByVal rhDab As Dword, lpKey As AsciiZ, ByVal lKeyLength As Long) As Long
Declare Function UsingReKey(ByVal rhDab As Dword, lpKey As AsciiZ, ByVal lKeyLength As Long) As Long

  • Modify the slOpen function to appear as follows:

Function slOpen Alias "slOpen" (Optional ByVal rsFileName As String, _
                                Optional ByVal rsModChars As String, _
                                Optional ByVal sKey As String)Export As Long

  • Change part of slOpen as follows:

   ' Open it
   If Verify(lsFileName, "0123456789") Or IsFalse Len(lsFileName) Then
      ' File name passed
      irGetRutAddress "sqlite3_open_v2", lhRutAddr
      Call Dword lhRutAddr Using UsingOpen(IIf$(tuFlags.AreRunningRemote, rsFileName, lsFileName), _
                                           thDab, _
                                           IIf&(InStr(rsModChars, "R"), %SQLITE_OPEN_READONLY, _
                                           %SQLITE_OPEN_READWRITE Or IIf&(InStr(rsModChars, "C"), _
                                           %SQLITE_OPEN_CREATE, 0)), _
                                           0) To tlLastError
      If tlLastError <> %SQLITE3_OK Then GoTo ErrorRut
      'Here's the new addition.
      If Len(sKey) Then
          irGetRutAddress "sqlite3_key", lhRutAddr
          Call Dword lhRutAddr Using UsingSetKey(thDab, ByCopy sKey, Len(sKey)) To tlLastError
          If tlLastError <> %SQLITE3_OK Then GoTo ErrorRut
      End If
   Else
      ' Database handle inherited
      thDab = Val(lsFileName)
      tuDbFlags.Bits.DabInheritedSoCantClose = %True
   End If

  • Add the slReKey function:

'============================<[ slReKey ]>============================
'Assumes the file is already open.
Function slReKey Alias "slReKey" (ByVal sKey As String) Export As Long
    Local lhRutAddr As Dword

    Reset tlLastError

    If thDab = 0 Then
        tlLastError = %SQLitening_NoOpenDatabase
        GoTo ErrorRut
    End If

    irGetRutAddress "sqlite3_rekey", lhRutAddr
    Call Dword lhRutAddr Using UsingReKey(thDab, ByCopy sKey, Len(sKey)) To tlLastError
    If tlLastError <> %SQLITE3_OK Then GoTo ErrorRut

    Function = %SQLITE3_OK
    Exit Function

    ErrorRut:
    Function = tlLastError
End Function

  • Compile the .DLL



Modify SQLitening.inc as follows:

  • Modify the following declaration:

Declare Function slOpen Lib "SQLitening.Dll" Alias "slOpen" (Optional ByVal rsFileName As String, Optional ByVal rsModChars As String, Optional ByVal sKey As String) As Long
  • Add the following declaration:

Declare Function slReKey Lib "SQLitening.Dll" Alias "slReKey" (ByVal sKey As String) As Long



With these changes, you can easily encrypt the entire database file by specifying the key in slOpen upon creation of the database. You must also use the key upon every open of the existing database. To change the key, call slReKey on an opened database.

It's just that simple.

Calvin

Paul Squires