SQLite实现加密
SQLite3 4 Delphi:Delphi中操作SQLite库。项目主页:http://code.google.com/p/sv-utils/最新库下载:http://sv-utils.googlecode.com/files/SQLite3_4_Delphi_RC2.zip
wxsqlite:这是wxWidgets组件之一,我们用它来实现加密。下载地址: http://jaist.dl.sourceforge.net/project/wxcode/Components/wxSQLite3/wxsqlite3-3.0.3.zip
下载完上述两个文件后,分别取两个文件中的部分文件:
1,SQLite3 4 Delphi:取“SQLite\sv-utils\SQLite3\Source”目录下3个文件:“SQLite3.pas”、“SQLiteTable3.pas”、“sv.inc”,放到工程里,并 把“SQLite3.pas”、“SQLiteTable3.pas”这两个文件添加到工程里,在使用地方引入这两个单元。
2,wxsqlite:取“wxsqlite3-3.0.3\sqlite3\secure\aes128\dll\release”下的“sqlite3.dll”,放到程序运行目录中。
上面文件准备完毕后,就已经完成对数据库加密需要的全部文件了。
演示代码:
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TForm2 = class(TForm) logMemo: TMemo; clearLogBtn: TButton; Button2: TButton; dbNameEdt: TEdit; Label1: TLabel; passwordEdt: TEdit; Label2: TLabel; createTableBtn: TButton; insertDataBtn: TButton; deleteDataBtn: TButton; selectDataBtn: TButton; procedure clearLogBtnClick(Sender: TObject); procedure Button2Click(Sender: TObject); procedure createTableBtnClick(Sender: TObject); procedure insertDataBtnClick(Sender: TObject); procedure selectDataBtnClick(Sender: TObject); procedure deleteDataBtnClick(Sender: TObject); private { Private declarations } public { Public declarations } procedure MyLog(tempLog: string); // log方法 end; var Form2: TForm2; logInfo: string; // log信息 implementation {$R *.dfm} uses SQLite3, SQLiteTable3; procedure TForm2.Button2Click(Sender: TObject); var sldb: TSQLiteDatabase; sltb: TSQLiteTable; sSQL: String; dbName: string; // 数据库名 password: string; // 密码 begin dbName := trim(dbNameEdt.Text); password := trim(passwordEdt.Text); try sltb := nil; sldb := TSQLiteDatabase.Create(dbName, seUTF8, password); sldb.ChangePassword(password); MyLog('加密数据库创建完毕:数据库 = ' + dbName + ' ; 密码 = ' + password); finally sldb.Free; if sltb <> nil then begin sltb.Free; end; end; end; procedure TForm2.clearLogBtnClick(Sender: TObject); begin logMemo.Clear; end; // 创建表 procedure TForm2.createTableBtnClick(Sender: TObject); var sldb: TSQLiteDatabase; sltb: TSQLiteTable; sSQL: String; dbName: string; // 数据库名 password: string; // 密码 begin dbName := trim(dbNameEdt.Text); password := trim(passwordEdt.Text); try sltb := nil; sldb := TSQLiteDatabase.Create(dbName, seUTF8, password); sldb.ChangePassword(password); sSQL := 'CREATE TABLE IF NOT EXISTS [test] ('; sSQL := sSQL + '[a] VARCHAR(100) NULL,'; sSQL := sSQL + '[b] VARCHAR(100) NULL'; sSQL := sSQL + ')'; sldb.ExecSQL(sSQL); MyLog('创建表: test'); finally sldb.Free; if sltb <> nil then begin sltb.Free; end; end; end; procedure TForm2.deleteDataBtnClick(Sender: TObject); var sldb: TSQLiteDatabase; sltb: TSQLiteTable; sSQL: String; dbName: string; // 数据库名 password: string; // 密码 begin dbName := trim(dbNameEdt.Text); password := trim(passwordEdt.Text); try sltb := nil; sldb := TSQLiteDatabase.Create(dbName, seUTF8, password); sldb.ChangePassword(password); sSQL := 'DELETE FROM [test] '; sldb.ExecSQL(sSQL); MyLog('删除数据完毕 '); finally sldb.Free; if sltb <> nil then begin sltb.Free; end; end; end; // 添加数据 procedure TForm2.insertDataBtnClick(Sender: TObject); var sldb: TSQLiteDatabase; sltb: TSQLiteTable; sSQL: String; dbName: string; // 数据库名 password: string; // 密码 i: integer; maxCount: integer; // 添加数据最大数量 begin dbName := trim(dbNameEdt.Text); password := trim(passwordEdt.Text); try sltb := nil; sldb := TSQLiteDatabase.Create(dbName, seUTF8, password); sldb.ChangePassword(password); maxCount := 9; for i := 0 to maxCount do begin sSQL := 'INSERT INTO [test] VALUES ('; sSQL := sSQL + QuotedStr('a' + inttostr(i)) + ','; sSQL := sSQL + QuotedStr('b' + inttostr(i)); sSQL := sSQL + ')'; sldb.ExecSQL(sSQL); end; MyLog('共添加: ' + inttostr(maxCount + 1) + ' 条数据'); finally sldb.Free; if sltb <> nil then begin sltb.Free; end; end; end; // log方法 procedure TForm2.MyLog(tempLog: string); var temp: string; oldLog: string; begin if trim(tempLog) <> '' then begin oldLog := trim(logMemo.Text); logMemo.Clear; temp := FormatDateTime('yyyy-mm-dd hh:mm:ss', now) + ' ' + trim(tempLog); if oldLog = '' then begin logMemo.Lines.Add(temp); logMemo.Lines.Add(''); end else begin logMemo.Lines.Add(temp); logMemo.Lines.Add(''); logMemo.Lines.Add(oldLog); end; end; end; procedure TForm2.selectDataBtnClick(Sender: TObject); var sldb: TSQLiteDatabase; sltb: TSQLiteTable; sSQL: String; dbName: string; // 数据库名 password: string; // 密码 totalCount: integer; temp: string; begin dbName := trim(dbNameEdt.Text); password := trim(passwordEdt.Text); try sltb := nil; sldb := TSQLiteDatabase.Create(dbName, seUTF8, password); sldb.ChangePassword(password); sSQL := 'SELECT * FROM [test] '; sltb := sldb.GetTable(sSQL); // 1,显示总数据条数 totalCount := 0; if not sltb.EOF then begin totalCount := sltb.Count; end; MyLog('共: ' + inttostr(totalCount) + ' 条数据'); // 2,显示详细信息 temp := ''; while not sltb.EOF do begin if temp = '' then begin temp := #13#10 + sltb.FieldValByNameAsString['a'] + ' ; ' + sltb.FieldValByNameAsString['b'] + #13#10; end else begin temp := temp + sltb.FieldValByNameAsString['a'] + ' ; ' + sltb.FieldValByNameAsString['b'] + #13#10; end; sltb.Next; end; if temp <> '' then begin MyLog('详细数据:' + #13#10 + temp); end; finally sldb.Free; if sltb <> nil then begin sltb.Free; end; end; end; end.