4

我在 Delphi xe10 中制作了一个示例应用程序,并在连接时对用户 ID 和密码以及数据库名称进行加密和解密问题是当我通过内存扫描仪在内存中打开 exe 进程时,我可以通过搜索连接的某些部分轻松找到所有这些string 在 win 应用程序中找到安全连接数据是否如此容易,还是我做错了什么? 连接 在此处输入图像描述

4

3 回答 3

0

开箱即用……为什么要隐藏密码?

如果数据库在用户的电脑上,那么他/她可以通过windows身份验证模式使用SQL管理工作室打开数据库,无需密码!

如果数据库在远程服务器上,那么最好编写一个 Web 服务来为您的程序获取数据并以 XML 格式发送结果,而不是远程打开数据库。

于 2017-12-27T17:12:35.530 回答
0

尽量保护内存。使用 CryptProtectMemory 和 CryptUnprotectMemory。

https://msdn.microsoft.com/de-de/library/windows/desktop/aa380262(v=vs.85).aspx

这是我班上的一个小片段。玩它:

        uses
         Winapi.Windows,
          System.SysUtils;
        ....

TMyMemEncryptBlaBla = class
    private
    //......
     public
      function MemEncrypt(const StrInp: String; CryptFlags: DWORD = 0): TBytes;
      function MemDecrypt(const EncInp: TBytes; CryptFlags: DWORD = 0): String;
     end;

    {
 BOOL WINAPI CryptProtectMemory(_Inout_ LPVOID pData,
                                _In_    DWORD  cbData,
                                _In_    DWORD  dwFlags );

 }
function CryptProtectMemory(Data: Pointer; Size: DWORD; Flags: DWORD) : BOOL; stdcall;
 {
 BOOL WINAPI CryptUnprotectMemory(_Inout_ LPVOID pData,
                                  _In_    DWORD  cbData,
                                  _In_    DWORD  dwFlags );

 }

function CryptUnProtectMemory(Data: Pointer; Size : DWORD;Flags: DWORD) : BOOL; stdcall;

// CryptProtectMemory and CryptUnprotectMemory.

 CRYPTPROTECTMEMORY_SAME_PROCESS  = 0; // Set as default
 CRYPTPROTECTMEMORY_CROSS_PROCESS = 1;
 CRYPTPROTECTMEMORY_SAME_LOGON    = 2;
 CRYPTPROTECTMEMORY_BLOCK_SIZE = 16;

implementation

function CryptProtectMemory;  external 'Crypt32.dll' Name 'CryptProtectMemory';
function CryptUnProtectMemory; external 'Crypt32.dll' Name 'CryptUnprotectMemory';

// encrypt
function TMyMemEncryptBlaBla.MemEncrypt(const StrInp: String; CryptFlags: DWORD): TBytes;
begin
  Result := TEncoding.Unicode.GetBytes(StrInp);
  try
    if Length(Result) mod CRYPTPROTECTMEMORY_BLOCK_SIZE <> 0 then
      SetLength(Result, ((Length(Result) div CRYPTPROTECTMEMORY_BLOCK_SIZE) + 1) * CRYPTPROTECTMEMORY_BLOCK_SIZE);
  except
    on E:Exception do
     begin
      MessageBox(0, PChar(E.Message), PChar('E_OUTOFMEMORY'), MB_ICONERROR or MB_OK);
     end;
  end;
  try
     if not CryptProtectMemory(Result, Length(Result), CryptFlags) then
      begin
        MessageBox(0, PChar('MemCrypt: ' + SysErrorMessage(GetLastError)), PChar('MemEncrypt failed'), MB_ICONERROR or MB_OK);
        ZeroMemory(Result, Length(Result));
      end;
  except
    on E:Exception do
      begin
        MessageBox(0, PChar(E.Message), PChar('MemEncrypt Exception'), MB_ICONERROR or MB_OK);
      end;
  end;
end;
//decrypt
function TMyMemEncryptBlaBla.MemDecrypt(const EncInp: TBytes; CryptFlags: DWORD): String;
var
  DecTmp: TBytes;
begin
  DecTmp := Copy(EncInp);
  try
     if CryptUnprotectMemory(DecTmp, Length(DecTmp), CryptFlags) then
        Result := TEncoding.Unicode.GetString(DecTmp)
     else
        MessageBox(0, PChar('MemDecrypt: ' + SysErrorMessage(GetLastError)), PChar('MemDecrypt failed'), MB_ICONERROR or MB_OK);

      ZeroMemory(DecTmp, Length(DecTmp));
  except
    on E:Exception do
        MessageBox(0, PChar(E.Message), PChar('MemDecrypt Exception'), MB_ICONERROR or MB_OK);
  end;
end;

end.

阿克塞尔

于 2017-12-26T15:02:39.480 回答
0

不要将密码放在连接字符串中。而是将 OnWillConnect 事件处理程序分配给 TADOConnection 并在提供的参数中提供密码。

于 2017-12-23T17:02:09.037 回答