AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Shop-Zugangsdaten verschlüsseln

Ein Thema von freimatz · begonnen am 25. Aug 2018 · letzter Beitrag vom 10. Sep 2018
Antwort Antwort
Seite 1 von 3  1 23      
freimatz

Registriert seit: 20. Mai 2010
1.383 Beiträge
 
Delphi 11 Alexandria
 
#1

Shop-Zugangsdaten verschlüsseln

  Alt 25. Aug 2018, 09:40
Hallo zusammen,
ich habe eine ähnliche Problematik wie bei DB-Zugangsdaten verschlüsseln. Bei mir geht es aber nicht um Zugangsdaten die nur das Programm kennt sondern um Zugangsdaten die der jeweilige Anwender spezifisch hat.

Es geht um einen Internet-Shop auf den ich zugreifen muss. Dazu benötige ich den Namen und Passwort des Anwenders. Also fordere ich den Anwender auf diese einzugeben und mein Programm macht was es tun muss. Nun soll er das nicht jedesmal tun müssen, dann müssen aber die Daten gespeichert werden. Das ist ja eine Unsicherheit.

Wie geht man da am Besten vor? In einem anderen Thread wo es eher um technische Probleme ging, wurde vorgeschlagen die Daten mit AES verschlüsselt zu speichern. Dazu muss der Key ja auch im Programm fix drin sein. Oder?

Ein ähnliches Problem haben doch auch Browser? Wenn ich mich hier im Forum anmelde, dann hat mein Browser Name und Passwort auch gespeichert. Nicht sehr vertrauenserweckend wenn man z.B. so was liest: https://www.heise.de/security/meldun...t-3998599.html). Drum mache ich beim Online-Banking u.a. das grundsätzlich nicht.

Gut wäre es auch wenn ich den Anwender nicht zwinge das Passwort zu speichern sondern es ihm überlasse. Wenn er nicht will muss er es halt jedesmal eingeben. Oder?
  Mit Zitat antworten Zitat
Schokohase
(Gast)

n/a Beiträge
 
#2

AW: Shop-Zugangsdaten verschlüsseln

  Alt 25. Aug 2018, 10:23
Du kannst dafür die Data Protection API verwenden (gibt es seit Windows 2000)
  Mit Zitat antworten Zitat
freimatz

Registriert seit: 20. Mai 2010
1.383 Beiträge
 
Delphi 11 Alexandria
 
#3

AW: Shop-Zugangsdaten verschlüsseln

  Alt 25. Aug 2018, 10:32
Danke.
Wofür und an welcher Stelle? Um die Daten des Anwenders zu speichern?

Habe kurz reingeschaut. Wenn ich das richtig sehe ist das eine API für Verschlüsselungen.
Zitat:
DPAPI is a password-based data protection service. It requires a password to provide protection. The drawback, of course, is that all protection provided by DPAPI rests on the password provided.
Wenn ich es richtig verstehe brauche ich dann auch ein Key dazu, der in meinem Programm gespeichert werden muss. Oder?
  Mit Zitat antworten Zitat
Schokohase
(Gast)

n/a Beiträge
 
#4

AW: Shop-Zugangsdaten verschlüsseln

  Alt 25. Aug 2018, 10:53
Nein, du brauchst die Daten und eine optionale Entropy (wird auch als Salt bezeichnet).

Der Schlüssel wird von der API selber generiert/verwaltet. Siehe dazu bei dwFlags
Zitat:
CRYPTPROTECT_LOCAL_MACHINE
When this flag is set, it associates the data protected with the current computer instead of with an individual user. Any user on the computer on which the internal protect function is called with this flag can use the internal unprotect function to unprotect the data. Application developers should understand that by using this flag no "real" protection is provided by DPAPI. By "real" we mean that any process running on the system can unprotect any data protected with this flag. We highly recommended that this flag not be used on workstations to protect user's data. It does make sense, however, for a server process to use the flag on a server where untrusted users are not allowed to logon. It also makes sense for a local machine process to use the flag to protect data to be stored off the machine or on a shared drive.
Die Daten können somit nur von dem Benutzer oder auf dieser Maschine wieder entschlüsselt werden.

Hier noch etwas auf stackoverflow

Geändert von Schokohase (25. Aug 2018 um 10:57 Uhr)
  Mit Zitat antworten Zitat
Schokohase
(Gast)

n/a Beiträge
 
#5

AW: Shop-Zugangsdaten verschlüsseln

  Alt 25. Aug 2018, 20:16
So sieht das dann aus, wenn mans es mit einem netten Wrapper versieht (läuft ab Windows 2000 sowohl x32 und x64)
Delphi-Quellcode:
program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  Security.Cryptographic.ProtectedData;

procedure SmallTest( );
var
  inStr, outStr: string;
  inBuffer, outBuffer, encrypted: TBytes;
begin
  inStr := 'Some Secret Data Here';
  WriteLn( inStr );
  inBuffer := TEncoding.UTF8.GetBytes( inStr );
  encrypted := TProtectedData.Protect( inbuffer ); // verschlüsseln
  outBuffer := TProtectedData.Unprotect( encrypted ); // entschlüsseln
  outStr := TEncoding.UTF8.GetString( outBuffer );
  WriteLn( outStr );
end;

begin
  try
    SmallTest( );
  except
    on E: Exception do
      Writeln( E.ClassName, ': ', E.Message );
  end;
  ReadLn;
end.
Delphi-Quellcode:
unit Security.Cryptographic.ProtectedData;

interface

uses
  System.SysUtils;

{$SCOPEDENUMS ON}

type
  TDataProtectionScope = (
    CurrentUser = $00,
    LocalMachine = $01 );

  TProtectedData = class
  strict private
    const
      DefaultDataProtectionScope = TDataProtectionScope.CurrentUser;
  public
    class function Protect( const AUserData: TBytes; AScope: TDataProtectionScope = DefaultDataProtectionScope ): TBytes; overload; static;
    class function Protect( const AUserData: TBytes; const AOptionalEntropy: TBytes; AScope: TDataProtectionScope = DefaultDataProtectionScope ): TBytes; overload; static;
    class function Unprotect( const AEncryptedDataData: TBytes; AScope: TDataProtectionScope = DefaultDataProtectionScope ): TBytes; overload; static;
    class function Unprotect( const AEncryptedDataData: TBytes; const AOptionalEntropy: TBytes; AScope: TDataProtectionScope = DefaultDataProtectionScope ): TBytes; overload; static;
  end;

implementation

{$IFDEF MSWINDOWS}
uses
  Winapi.Windows,
  Winapi.DataProtectApi;
{$ENDIF}

{ TProtectedData }

class function TProtectedData.Protect( const AUserData, AOptionalEntropy: TBytes; AScope: TDataProtectionScope ): TBytes;
{$IFDEF MSWINDOWS}
var
  dataIn, dataOut, entropy: DATA_BLOB;
  pEntropy: PDATA_BLOB;
  flags: DWORD;
begin
  if Length( AUserData ) = 0 then
    raise EArgumentException.Create( 'AUserData' );

  dataOut.cbData := 0;
  dataOut.pbData := nil;

  dataIn.cbData := length( AUserData );
  dataIn.pbData := @AUserData[0];

  if Length( AOptionalEntropy ) > 0 then
  begin
    entropy.cbData := length( AOptionalEntropy );
    entropy.pbData := @AOptionalEntropy[0];
    pEntropy := @entropy;
  end
  else
    pEntropy := nil;

  flags := CRYPTPROTECT_UI_FORBIDDEN;
  if AScope = TDataProtectionScope.LocalMachine then
    flags := flags or CRYPTPROTECT_LOCAL_MACHINE;

  if not CryptProtectData( @dataIn, nil, pentropy, nil, nil, flags, @dataOut ) then
    RaiseLastOsError;

  SetLength( Result, dataOut.cbData );
  move( dataOut.pbData^, Result[0], dataOut.cbData );
  LocalFree( HLOCAL( dataOut.pbData ) );
end;
{$ELSE}
begin
  raise ENotImplementedException.Create( 'Not Implemented' );
end;
{$ENDIF}

class function TProtectedData.Protect( const AUserData: TBytes; AScope: TDataProtectionScope ): TBytes;
var
  Entropy: TBytes;
begin
  Entropy := [];
  Result := TProtectedData.Protect( AUserData, Entropy, AScope );
end;

class function TProtectedData.Unprotect( const AEncryptedDataData, AOptionalEntropy: TBytes; AScope: TDataProtectionScope ): TBytes;
{$IFDEF MSWINDOWS}
var
  dataIn, dataOut, entropy: DATA_BLOB;
  pEntropy: PDATA_BLOB;
  flags: DWORD;
begin
  if Length( AEncryptedDataData ) = 0 then
    raise EArgumentException.Create( 'AEncryptedDataData' );

  dataOut.cbData := 0;
  dataOut.pbData := nil;

  dataIn.cbData := length( AEncryptedDataData );
  dataIn.pbData := @AEncryptedDataData[0];

  if Length( AOptionalEntropy ) > 0 then
  begin
    entropy.cbData := length( AOptionalEntropy );
    entropy.pbData := @AOptionalEntropy[0];
    pEntropy := @entropy;
  end
  else
    pEntropy := nil;

  flags := CRYPTPROTECT_UI_FORBIDDEN;
  if AScope = TDataProtectionScope.LocalMachine then
    flags := flags or CRYPTPROTECT_LOCAL_MACHINE;

  if not CryptUnprotectData( @dataIn, nil, pentropy, nil, nil, flags, @dataOut ) then
    RaiseLastOsError;

  SetLength( Result, dataOut.cbData );
  move( dataOut.pbData^, Result[0], dataOut.cbData );
  LocalFree( HLOCAL( dataOut.pbData ) );
end;
{$ELSE}
begin
  raise ENotImplementedException.Create( 'Not Implemented' );
end;
{$ENDIF}

class function TProtectedData.Unprotect( const AEncryptedDataData: TBytes; AScope: TDataProtectionScope ): TBytes;
var
  Entropy: TBytes;
begin
  Entropy := [];
  Result := Unprotect( AEncryptedDataData, Entropy, AScope );
end;

end.
Delphi-Quellcode:
unit Winapi.DataProtectApi;

interface

uses
  Winapi.Windows;

const
  CRYPTPROTECT_UI_FORBIDDEN = UINT( $01 );
{$EXTERNALSYM CRYPTPROTECT_UI_FORBIDDEN}
  CRYPTPROTECT_LOCAL_MACHINE = UINT( $04 );
{$EXTERNALSYM CRYPTPROTECT_LOCAL_MACHINE}
  CRYPTPROTECT_CRED_SYNC = UINT( $08 );
{$EXTERNALSYM CRYPTPROTECT_CRED_SYNC}
  CRYPTPROTECT_AUDIT = UINT( $010 );
{$EXTERNALSYM CRYPTPROTECT_AUDIT}
  CRYPTPROTECT_NO_RECOVERY = UINT( $020 );
{$EXTERNALSYM CRYPTPROTECT_NO_RECOVERY}
  CRYPTPROTECT_VERIFY_PROTECTION = UINT( $040 );
{$EXTERNALSYM CRYPTPROTECT_VERIFY_PROTECTION}

type
  PCryptoApiBlob = ^TCrypeoApiBlob;
  _CRYPTOAPI_BLOB = record
    cbData: DWORD;
    pbData: PBYTE;
  end;
{$EXTERNALSYM _CRYPTOAPI_BLOB}

  TCrypeoApiBlob = _CRYPTOAPI_BLOB;

  CRYPT_INTEGER_BLOB = _CRYPTOAPI_BLOB;
{$EXTERNALSYM CRYPT_INTEGER_BLOB}
  PCRYPT_INTEGER_BLOB = PCryptoApiBlob;
{$EXTERNALSYM PCRYPT_INTEGER_BLOB}
  CRYPT_UINT_BLOB = _CRYPTOAPI_BLOB;
{$EXTERNALSYM CRYPT_UINT_BLOB}
  PCRYPT_UINT_BLOB = PCryptoApiBlob;
{$EXTERNALSYM PCRYPT_UINT_BLOB}
  CRYPT_OBJID_BLOB = _CRYPTOAPI_BLOB;
{$EXTERNALSYM CRYPT_OBJID_BLOB}
  PCRYPT_OBJID_BLOB = PCryptoApiBlob;
{$EXTERNALSYM PCRYPT_OBJID_BLOB}
  CERT_NAME_BLOB = _CRYPTOAPI_BLOB;
{$EXTERNALSYM CERT_NAME_BLOB}
  PCERT_NAME_BLOB = PCryptoApiBlob;
{$EXTERNALSYM PCERT_NAME_BLOB}
  CERT_RDN_VALUE_BLOB = _CRYPTOAPI_BLOB;
{$EXTERNALSYM CERT_RDN_VALUE_BLOB}
  PCERT_RDN_VALUE_BLOB = PCryptoApiBlob;
{$EXTERNALSYM PCERT_RDN_VALUE_BLOB}
  CERT_BLOB = _CRYPTOAPI_BLOB;
{$EXTERNALSYM CERT_BLOB}
  PCERT_BLOB = PCryptoApiBlob;
{$EXTERNALSYM PCERT_BLOB}
  CRL_BLOB = _CRYPTOAPI_BLOB;
{$EXTERNALSYM CRL_BLOB}
  PCRL_BLOB = PCryptoApiBlob;
{$EXTERNALSYM PCRL_BLOB}
  DATA_BLOB = _CRYPTOAPI_BLOB;
{$EXTERNALSYM DATA_BLOB}
  PDATA_BLOB = PCryptoApiBlob;
{$EXTERNALSYM PDATA_BLOB}
  CRYPT_DATA_BLOB = _CRYPTOAPI_BLOB;
{$EXTERNALSYM CRYPT_DATA_BLOB}
  PCRYPT_DATA_BLOB = PCryptoApiBlob;
{$EXTERNALSYM PCRYPT_DATA_BLOB}
  CRYPT_HASH_BLOB = _CRYPTOAPI_BLOB;
{$EXTERNALSYM CRYPT_HASH_BLOB}
  PCRYPT_HASH_BLOB = PCryptoApiBlob;
{$EXTERNALSYM PCRYPT_HASH_BLOB}
  CRYPT_DIGEST_BLOB = _CRYPTOAPI_BLOB;
{$EXTERNALSYM CRYPT_DIGEST_BLOB}
  PCRYPT_DIGEST_BLOB = PCryptoApiBlob;
{$EXTERNALSYM PCRYPT_DIGEST_BLOB}
  CRYPT_DER_BLOB = _CRYPTOAPI_BLOB;
{$EXTERNALSYM CRYPT_DER_BLOB}
  PCRYPT_DER_BLOB = PCryptoApiBlob;
{$EXTERNALSYM PCRYPT_DER_BLOB}
  CRYPT_ATTR_BLOB = _CRYPTOAPI_BLOB;
{$EXTERNALSYM CRYPT_ATTR_BLOB}
  PCRYPT_ATTR_BLOB = PCryptoApiBlob;
{$EXTERNALSYM PCRYPT_ATTR_BLOB}

  PCryptProtectPromptStruct = ^TCryptProtectPromptStruct;
  _CRYPTPROTECT_PROMPTSTRUCT = record
    cbSize: DWORD;
    dwPromptFlags: DWORD;
    hwndApp: HWND;
    szPrompt: LPCWSTR;
  end;
{$EXTERNALSYM _CRYPTPROTECT_PROMPTSTRUCT}

  TCryptProtectPromptStruct = _CRYPTPROTECT_PROMPTSTRUCT;

  PCRYPTPROTECT_PROMPTSTRUCT = ^_CRYPTPROTECT_PROMPTSTRUCT;
{$EXTERNALSYM PCRYPTPROTECT_PROMPTSTRUCT}
  CRYPTPROTECT_PROMPTSTRUCT = _CRYPTPROTECT_PROMPTSTRUCT;
{$EXTERNALSYM CRYPTPROTECT_PROMPTSTRUCT}

function CryptProtectData( pDataIn: PDATA_BLOB; szDataDescr: LPCWSTR; pOptionalEntropy: PDATA_BLOB; pvReserved: PVOID; pPromptStruct: PCRYPTPROTECT_PROMPTSTRUCT; dwFlags: DWORD; pDataOut: PDATA_BLOB ): BOOL; stdcall;
{$EXTERNALSYM CryptProtectData}
function CryptUnprotectData( pDataIn: PDATA_BLOB; szDataDescr: LPCWSTR; pOptionalEntropy: PDATA_BLOB; pvReserved: PVOID; pPromptStruct: PCRYPTPROTECT_PROMPTSTRUCT; dwFlags: DWORD; pDataOut: PDATA_BLOB ): BOOL; stdcall;
{$EXTERNALSYM CryptUnprotectData}

const
  crypt32 = 'crypt32.dll';

implementation

function CryptProtectData; external crypt32 name 'CryptProtectData';
function CryptUnprotectData; external crypt32 name 'CryptUnprotectData';

end.
  Mit Zitat antworten Zitat
Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#6

AW: Shop-Zugangsdaten verschlüsseln

  Alt 25. Aug 2018, 20:35
Danke Schokohase für Deinen Wrapper.
Ich musste zwei Änderungen durchführen damit es funktioniert, deshalb Frage: Sind Änderungen so Okay? (in Demo erscheint 2x gleicher Text)

Änderung #1: PVOID in Pointer
Änderung #2: Entropy := []; in SetLength(Entropy, 0);
Danke für Mühe, ich kannte das noch nicht.
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat
Schokohase
(Gast)

n/a Beiträge
 
#7

AW: Shop-Zugangsdaten verschlüsseln

  Alt 25. Aug 2018, 20:44
Geschrieben habe ich das unter 10.2.3.

Da befindet sich in der Winapi.Windows
Delphi-Quellcode:
  PVOID = Pointer;
  {$EXTERNALSYM PVOID}
Und genau diesen Eintrag würde ich an deiner Stelle in der Unit hinzufügen anstatt in der Deklaration PVOID durch Pointer zu ersetzen. Dann sieht es exakt so aus wie in der API-Dokumentation.

Entropy = []; kann man ab Delphi Version X verwenden und ist das gleiche wie SetLength(Entropy,0);

Geändert von Schokohase (25. Aug 2018 um 20:49 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#8

AW: Shop-Zugangsdaten verschlüsseln

  Alt 25. Aug 2018, 20:48
Dann ist es also Okay für mein Delphi, vielen Dank für Antwort!

edit
Delphi-Quellcode:
  PVOID = Pointer;
  {$EXTERNALSYM PVOID}
Und genau diesen Eintrag würde ich an deiner Stelle in der Unit hinzufügen
Habe es so durchgeführt, Danke nochmal für flotte Antwort.
Gruß vom KodeZwerg

Geändert von KodeZwerg (25. Aug 2018 um 22:07 Uhr)
  Mit Zitat antworten Zitat
freimatz

Registriert seit: 20. Mai 2010
1.383 Beiträge
 
Delphi 11 Alexandria
 
#9

AW: Shop-Zugangsdaten verschlüsseln

  Alt 4. Sep 2018, 13:21
So, jetzt kam ich mal dazu das auszuprobieren. Danke erstmal für den Hinweis und den Code.
Diese API kannte ich noch gar nicht. Die hätte ich vor 20 Jahren schon brauchen können.
Der Code hier läuft bei mir hier ohne Probleme durch. (Berlin)

Ein Frage hätte ich noch. Ich beziehe mich auf den Beispielscode:
encrypted := TProtectedData.Protect( inbuffer ); // verschlüsseln

Das encrypted kann da doch Nullen enthalten. Das ist meine Erkenntnis beim Debuggen. Wenn ich das nun in einer INI-Datei o.ä. speichern will, muss ich das nochmals codieren z.B. mit base64.
Sehe ich das so richtig?
(Nein ich brauche kein Beispiel wie base64 geht)
  Mit Zitat antworten Zitat
Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.021 Beiträge
 
Delphi 12 Athens
 
#10

AW: Shop-Zugangsdaten verschlüsseln

  Alt 4. Sep 2018, 13:38
encrypted := TProtectedData.Protect( inbuffer ); // verschlüsseln

Das encrypted kann da doch Nullen enthalten. Das ist meine Erkenntnis beim Debuggen. Wenn ich das nun in einer INI-Datei o.ä. speichern will, muss ich das nochmals codieren z.B. mit base64.
Sehe ich das so richtig?
Korrekt.

Wie ich das sehe, ist encrypted als TBytes deklariert. Das kann so direkt gar nicht in eine INI-Datei geschrieben werden, sondern muss erst in einen String umgewandelt werden. Da kein TEncoding das für jede beliebige Byte-Sequenz hinbekommt (siehe z.B. Nullen), bietet sich hier sowas wie Base64 geradezu an.
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 3  1 23      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 09:57 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz