DbDefence Database Encryption API

There are two DLLs in API folder of installation.

dbencrypt.dll is used for 32-bit applications
dbencrypt64.dll is used for 64-bit applications.

You can use API from C#, C++ or any other language.

For further reference see code examples in the included API folder. C#/C++ examples available.

The API encryption process consist of oneprimary function:

int Encrypt (TCallbackFunc * func, int argc, wchar_t * argv[]);

TCallbackFunc * func is the pointer to the function to be called on events during encryption: info, error and success messages.

It can be defined as:

void WINAPI ShowMessage (const wchar_t * msg, int code);

Info messages - msg - comes with error code 0. Possible error codes:

0 - Success.
-777 - Encryption finished. (used only in API)
145 - There are active connections in the database. Use option -F to kill.
144 - Can't decrypt database. Maybe wrong password.
152 - Can't create symmetric key in the database.
-21 - Error messages from SQL Server.
-20 - Can't connect to server.
-19 - DbDefence not installed or not started.
-18 - Database already encrypted.
-17 - No password specified.
-16 - Can't switch to select db.
-15 - Can't create DbDefence tables in database or created incorrectly.
-14 - Unexpected error.
-13 - Can't detach database.
-12 - Can't open database files for RW access.
-11 - Can't attach database after encryption.
-10 - File with exceptions specified but it's not accessible.
-4 - Can't access all files of the database.
-3 - Database too big for this license.
-2 - Unknown command line option.

If the API encryption is done successfully, the API call func wll be code -777 and return 0. In case of an error, it returns one of the error codes used in the callback.

The parameter argc is the number of parameters in the next parameter, argv. It is the same as the traditional parameters of the function used in C/C++ programming.

Parameters used in the encryption API process are the same as parameters used in the command line encryption tool.

C# example:


using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;


namespace csharpdlltest
{
    
    unsafe class Program
    {
        public delegate void Callback([MarshalAs(UnmanagedType.LPWStr)]String msg, int code);

        // Please pay attention to the platform!
        const string _dllLocation = "L:\\work\\dbencrypt.dll";
//      const string _dllLocation = "L:\\work\\dbencrypt64.dll";

        [DllImport(_dllLocation, CharSet=CharSet.Unicode)]
        public static extern int Encrypt (Delegate Func,int a, String  [] s);

        static void MyCallback(String msg, int code)
        {
            System.Console.WriteLine("Output from DLL: {0} {1}",msg,code);
        }

        static void Main(string[] args)
        {
               
            // you must run the program locally on the target server
// if you can't login with trust connection, add -U and -P parameters for sql user and sql password
String [] param={"MyProc","-S",".\\sql2005","-p","CoolPassword111","-d","mydatabase"}; Callback cb = new Callback (MyCallback); int res = Encrypt(cb,param.Length, param); if (res == 0) { //well done! } return; } } }


VB.NET Example:

-----------------------------------------------------------------------------
Imports System
Imports System.Runtime.InteropServices

Public Delegate Sub CallBack(<MarshalAs(UnmanagedType.LPWStr)> str As String, code As Integer)

Module Module1
Public Declare Sub Encrypt Lib "dbencrypt.dll" (cb As CallBack, num As Integer, <MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.LPWStr)> params() As String)
Public Declare Sub Encrypt64 Lib "dbencrypt64.dll" Alias "Encrypt" (cb As CallBack, num As Integer, <MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.LPWStr)> params() As
String)
Public Sub CallFromDbDefence(<MarshalAs(UnmanagedType.LPWStr)> str As String, code As Integer)

Console.Write("message " + str + ", code " + code.ToString())
End Sub

Sub Main()

Dim param() As String = {"AnyProcessName", "-S", ".\sql201264", "-p", "CoolPassword111", "-d", "mydatabase"}
If IntPtr.Size = 8 Then
' call 64 bit dll
Encrypt64(AddressOf CallFromDbDefence, param.Length, param)
Else
' call 32 bit dll
Encrypt(AddressOf CallFromDbDefence, param.Length, param)
End If
End Sub
End Module
-----------------------------------------------------------------------------