【问题标题】:How remove encryption setting - Delphi?如何删除加密设置 - Delphi?
【发布时间】:2017-08-10 19:26:09
【问题描述】:

我在 Delphi 10.2 中为我的 Sql-lite DB 使用加密我可以加密数据库并在连接参数中设置密码来访问它 - 很好。当连接参数密码为空时,我可以解密并访问它。很好。

问题是我想使用单个程序来访问数据库,无论它是否加密。如果数据库未加密,但连接参数中有密码,我会收到一条错误消息,即数据库“未加密”。尝试将密码设置为空白或“即时”删除,如何及时检测未加密状态以删除密码?

我试过了:

procedure TDataApp10.ConnectAppError(ASender, AInitiator: TObject;
  var AException: Exception);
begin
  ConnectApp.Connected:= False;
//handle most likely connect error, DB NOT encryped!
  FDSQLITESecurity1.Database:= 'C:\VCDat\VCDataApp.sdb';
  FDSQLITESecurity1.Password:= 'MyPW';
  FDSQLITESecurity1.RemovePassword; //HANGS MESSAGE HERE!!!
  ConnectApp.Params.Password:='';
  ConnectApp.Connected:= True; //retry or on demand
end;

挂断消息是 CIPHER: DB not encrypted

【问题讨论】:

标签: sqlite delphi encryption


【解决方案1】:

要在指定Password 参数时打开未加密的数据库,您可以这样做:

procedure TForm1.Button1Click(Sender: TObject);
begin
  FDConnection.Params.Add('DriverID=SQLite');
  FDConnection.Params.Add('Database=C:\MyUnencryptedData.db');
  FDConnection.Params.Add('Password=1234');

  try
    FDConnection.Open;
  except
    { if the engine reports unencrypted database, remove the password from connection
      parameters and retry to open the connection }
    on E: EFDDBEngineException do
      if E.FDCode = er_AD_SQLiteDBUnencrypted then
      begin
        FDConnection.Params.Values['Password'] := '';
        FDConnection.Open;
      end
      else
        raise;
  end;
  ...
end;

另一种方式是例如调用CheckEncryption 方法并比较其结果(内部发生与上面类似的情况;我只是不喜欢字符串返回,所以我个人更喜欢上面的方法):

procedure TForm1.Button1Click(Sender: TObject);
begin
  FDConnection.Params.Add('DriverID=SQLite');
  FDConnection.Params.Add('Database=C:\MyUnencryptedData.db');
  FDConnection.Params.Add('Password=1234');

  FDSQLiteSecurity.Database := 'C:\MyUnencryptedData.db';
  FDSQLiteSecurity.Password := '1234';

  if FDSQLiteSecurity.CheckEncryption = '<unencrypted>' then
    FDConnection.Params.Values['Password'] := '';

  FDConnection.Open;
  ...
end;

【讨论】:

  • 非常好。关于发布此答案的时间,我确实发现了第二种方法。了解到的见解是 FDSQLiteSecurity 例程会影响访问数据库的能力,但我需要重点关注的是数据库本身的连接参数以清空密码等。
  • 是的,如果您指定非空的Password 连接参数,您这样做是为了打开加密数据库(如果不更改加密数据库密码)。这并没有那么误导,因为 SQLite 通常没有登录名/密码的概念。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多