sqlite - 如何删除加密设置 - Delphi?

标签 sqlite delphi encryption

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

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

我尝试过:

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:数据库未加密

最佳答案

在具有 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;

关于sqlite - 如何删除加密设置 - Delphi?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45596650/

相关文章:

python - 如何使用 Python 连接到多个 sqlite3 数据库

Android Room SQLite_ERROR 没有这样的表

ios - 不可编辑的数据库本地存储在 iOS 应用程序中

delphi - 电子邮件中 ​​ShellExecute 正文的空白字符

java - 是否可以生成 64 字节(256 位) key 并使用 AndroidKeyStore 存储/检索它?

linux - 加密不解密

java - SQLiteDatabase删除涉及子查询

delphi - 用Delphi获取文本边界的坐标

Delphi XE2 数据模块只需要数据库组件?

java - 这段Java代码解密的算法是什么?