Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Passwort in Ini-datei speichern (https://www.delphipraxis.net/211816-passwort-ini-datei-speichern.html)

lucy 6. Nov 2022 13:16

Passwort in Ini-datei speichern
 
Liste der Anhänge anzeigen (Anzahl: 1)
Folgendes Problem beim Verschlüsseln:

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  Ini: TIniFile;
    s: String[255];
    c: array[0..255] of Byte absolute s;
    i: Integer;
begin
  Ini:=TIniFile.Create(ExtractFilePath(ParamStr( 0 )) + '\settings.ini');
  try
     s := Edit4.Text;
     for i := 1 to Length(s) do s[i] := Char(23 xor Ord(c[i])); // das Problem ist Char
    Ini.WriteString('mySQl', 'Server', Edit1.Text);
    Ini.WriteString('mySQl', 'Port', Edit2.Text);
    Ini.WriteString('mySQl', 'User', Edit3.Text);
    Ini.WriteString('mySQl', 'Passwort', s);
    Ini.WriteString('mySQl', 'Datenbank', Edit5.Text);
  finally
    Ini.Free;
  end;
end;

Bernhard Geyer 6. Nov 2022 13:37

AW: Passwort in Ini-datei speichern
 
Unverschlüsselt speichern ist immer eine schlechte Idee!
XOR ist keine Verschlüsselung!

EdAdvokat 6. Nov 2022 14:22

AW: Passwort in Ini-datei speichern
 
mach mal aus dem s : string[255]; ein string.

himitsu 6. Nov 2022 14:46

AW: Passwort in Ini-datei speichern
 
Zitat:

Zitat von EdAdvokat (Beitrag 1514395)
mach mal aus dem s : string[255]; ein string.

Nein.

Man beachte das absolute.
Wenn, dann muß man noch mehr umbauen.


Problem ist hier, dass seit Delphi 2009 der String Unicode ist,
aber der Typ String[x] blieb weiterhin ANSI.

Auch bei der Ver-/Entschlüsselung von Zeichen über #127 (kein ASCII) kommt eventuell was Falsches raus, wenn man es einfach so auf Unicode umstellt. (2 Byte statt 1 Byte)

Delphi-Quellcode:
s := AnsiString(Edit4.Text);
for i := 1 to Length(s) do s[i] := AnsiChar(23 xor Ord(c[i])

lucy 6. Nov 2022 16:50

AW: Passwort in Ini-datei speichern
 
Zitat:

Nein.

Man beachte das absolute.
Wenn, dann muß man noch mehr umbauen.


Problem ist hier, dass seit Delphi 2009 der String Unicode ist,
aber der Typ String[x] blieb weiterhin ANSI.

Auch bei der Ver-/Entschlüsselung von Zeichen über #127 (kein ASCII) kommt eventuell was Falsches raus, wenn man es einfach so auf Unicode umstellt. (2 Byte statt 1 Byte)
es funktioniert, danke

DaCoda 8. Nov 2022 15:27

AW: Passwort in Ini-datei speichern
 
Also XOR ist nicht gerade schön und wirklich nicht zu empfehlen.
Wenn es minimale Sicherheit sein soll, dann würde ich das eventuell so machen:

Delphi-Quellcode:
const
  Code64 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/';

function Encode64(S: string): string;
function Decode64(S: string): string;

{-------------------------------------------------------------------------------}

function Encode64(S: string): string;
var
  I: Integer;
  a: Integer;
  x: Integer;
  b: Integer;
begin
  Result := '';
  a := 0;
  b := 0;
  for I := 1 to Length(S) do begin
      x := Ord(S[I]);
    b := b * 256 + x;
    a := a + 8;
    while a >= 6 do begin
      a := a - 6;
      x := b div (1 shl a);
      b := b mod (1 shl a);
      Result := Result + Code64[x + 1];
    end;
  end;
  if a > 0 then begin
      x := b shl (6 - a);
    Result := Result + Code64[x + 1];
  end;
end;

function Decode64(S: string): string;
var
  I: Integer;
  a: Integer;
  x: Integer;
  b: Integer;
begin
  Result := '';
  a := 0;
  b := 0;
  for I := 1 to Length(S) do begin
    x := Pos(S[I], Code64) - 1;
    if x >= 0 then begin
        b := b * 64 + x;
      a := a + 6;
      if a >= 8 then begin
          a := a - 8;
        x := b shr a;
        b := b mod (1 shl a);
        x := x mod 256;
        Result := Result + chr(x);
      end;
    end else
      Exit;
  end;
end;

stifflersmom 9. Nov 2022 11:09

AW: Passwort in Ini-datei speichern
 
Ich möchte hier auf Synopse verweisen.

Code:
unit uSimpleEncryption;


interface

uses SynCommons, SynCrypto;

function encrypt(aString:String):String;
function decrypt(aString:String):String;

implementation

const
  my_key = 'derVerschLuessElLungsKeyimmerWiederNett';

function encrypt(aString:String):String;
var
  key : TSHA256Digest;
  aes : TAESCFB;
  s:RawByteString;
begin
  SynCommons.HexToBin(Pointer(SHA256(my_key)), @key, 32);

  aes := TAESCFB.Create(key, 256);
  try
    s := StringToUTF8(aString);
    s := BinToBase64(aes.EncryptPKCS7(s, True));
    Result := UTF8ToString(s);
  finally
    aes.Free;
  end;
end;


function decrypt(aString:String):String;
var
  key : TSHA256Digest;
  aes : TAESCFB;
  s:RawByteString;
begin
  SynCommons.HexToBin(Pointer(SHA256(my_key)), @key, 32);

   aes := TAESCFB.Create(key, 256);
  try
    s := StringToUTF8(aString);
    s := aes.DecryptPKCS7(Base64ToBin(s), True);
    Result := UTF8ToString(s);
  finally
    aes.Free;
  end;
end;

end.
Und dann nutzen mit
Code:
 Encrypt(sApiPassword);
bzw.
Code:
Decrypt(sEncryptedString);

Andreas13 9. Nov 2022 12:27

AW: Passwort in Ini-datei speichern
 
Zitat:

Zitat von stifflersmom (Beitrag 1514524)
Ich möchte hier auf Synopse verweisen...

Du meinst bestimmt Synapse, nehme ich an: https://github.com/dimmaq/delphi-synapse. :-D
Grüße, Andreas

stifflersmom 9. Nov 2022 12:33

AW: Passwort in Ini-datei speichern
 
Uuups, man sollte die Brille beim Schreiben aufsetzen...

https://synopse.info

Andreas13 9. Nov 2022 12:46

AW: Passwort in Ini-datei speichern
 
Danke für die Richtigstellung und den korrekten Link! Ich habe nur die Version mit a gekannt. :oops:
Grüße, Andreas


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:49 Uhr.
Seite 1 von 2  1 2      

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