AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Netzwerke Delphi Delphi und Microsoft Authenticator

Delphi und Microsoft Authenticator

Ein Thema von Dumpfbacke · begonnen am 2. Okt 2025 · letzter Beitrag vom 5. Okt 2025
 
Dumpfbacke

Registriert seit: 10. Mär 2005
Ort: Mitten in Deutschland
343 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#1

Delphi und Microsoft Authenticator

  Alt 2. Okt 2025, 12:48
Hallo Leute,
ich wollte das ganze gerne in einen Anwendung von mir einbauen. Ich habe hierzu etwas Code gefunden. Als erstes erzeuge ich den QRCode. Diesen binde ich dann ins Handy ein nei dem Microsoft Authenticator und hier wird dann dalle 30 Sekunden ein neuer Key erzeugt. Mein Problem ist nun, das wenn ich den Key (die 6 Stellige Zahl) im Programm prüfe ist diese immer falsch. Hat jemadn ggf. einen Tip für mich ?

Delphi-Quellcode:
function TForm1.GenerateBase32Secret(ALength: Integer): string;
const
  Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
var
  i: Integer;
begin
  Randomize;
  Result := '';
  for i := 1 to ALength do
    Result := Result + Alphabet[Random(Length(Alphabet)) + 1];
end;

function TForm1.BuildOtpAuthURI(const User, Issuer, Secret: string): string;
begin
  Result := Format(
    'otpauth://totp/%s:%s?secret=%s&issuer=%s&digits=6&period=30',
    [Issuer, User, Secret, Issuer]);
end;

procedure TForm1.GenerateQRCode(const Text: string; Bitmap: TBitmap);
var
  QRCode: TDelphiZXingQRCode;
  x, y: Integer;
  Scale: Integer;
begin
  QRCode := TDelphiZXingQRCode.Create;
  try
    QRCode.Data := Text;
    Scale := 5;
    Bitmap.SetSize(QRCode.Rows * Scale, QRCode.Columns * Scale);
    Bitmap.Canvas.Brush.Color := clWhite;
    Bitmap.Canvas.FillRect(Rect(0, 0, Bitmap.Width, Bitmap.Height));
    for y := 0 to QRCode.Rows - 1 do
      for x := 0 to QRCode.Columns - 1 do
        if QRCode.IsBlack[y, x] then
        begin
          Bitmap.Canvas.Brush.Color := clBlack;
          Bitmap.Canvas.FillRect(Rect(x*Scale, y*Scale,
                                      (x+1)*Scale, (y+1)*Scale));
        end;
  finally
    QRCode.Free;
  end;
end;

function TForm1.GenerateTOTP(const Secret: string; TimeStep: Int64): string;
var
  Counter: Int64;
  Msg, Hash: TBytes;
  Offset, Binary, Code: Integer;
begin
  Counter := DateTimeToUnix(Now) div TimeStep;
  SetLength(Msg, 8);
  PInt64(@Msg[0])^ := Swap(Counter); // BigEndian

  Hash := THashSHA1.GetHMACAsBytes(Msg, TEncoding.ASCII.GetBytes(Secret));

  Offset := Hash[High(Hash)] and $0F;
  Binary := ((Hash[Offset] and $7F) shl 24) or
            ((Hash[Offset+1] and $FF) shl 16) or
            ((Hash[Offset+2] and $FF) shl 8) or
            (Hash[Offset+3] and $FF);

  Code := Binary mod 1000000;
  Result := Format('%.6d', [Code]);
end;

procedure TForm1.btnGenerate1Click(Sender: TObject);
var
  URI: string;
  bmp: TBitmap;
begin
 FSecret := GenerateBase32Secret;
 URI := BuildOtpAuthURI('user@example.com', 'MeineApp', FSecret);

  bmp := TBitmap.Create;
  try
    GenerateQRCode(URI, bmp);
    imgQR.Picture.Assign(bmp);
  finally
    bmp.Free;
  end;

  lblResult.Caption := 'QR-Code erzeugt. Bitte mit Authenticator scannen.';
end;


procedure TForm1.btnVerifyClick(Sender: TObject);
var
  Code: string;
begin
 Code := GenerateTOTP(FSecret);
  if edtCode.Text = Code then
    lblResult.Caption := '✅ Code korrekt!'+Code
  else
    lblResult.Caption := '❌ Ungültiger Code'+Code;
end;
Danke schon einmal allen die ggf. den Fehler hier finden werden. Ich bin nicht dazu in der laage.

Tanja
Tanja
  Mit Zitat antworten Zitat
 

Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

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 07:38 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz