Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Mit PHP verschlüsseln (https://www.delphipraxis.net/117754-mit-php-verschluesseln.html)

Die Muhkuh 25. Jul 2008 11:41

Re: Mit PHP verschlüsseln
 
Hi,

schau mal beim Michael beim FileCrypter. Dort ist eine Datei Encrypt.pas. In der ist ein Beispiel, wie man das DEC richtig verwendet.

Angel4585 25. Jul 2008 14:50

Re: Mit PHP verschlüsseln
 
Also bei mir funzt das nicht :wall:
Wende ich das richtig an?
Delphi:
Delphi-Quellcode:
var d : TCipher_Blowfish ;
begin
d := TCipher_Blowfish.create;
d.Mode := cmCBCx;
d.Init('Passwort','12345678');
ShowMessage(Format('%10s: %s'+slinebreak+'%10s: %s',[
  'Delphi',
  d.EncodeBinary('Teststring',TFormat_HEX),
  'PHP',
  IdHTTP1.Get('http://localhost/crypt/mawe.php')]));
d.Free;
PHP:
Code:
<?php
$key = "Passwort";
$input = "Teststring";
$encrypted = mcrypt_cbc(MCRYPT_BLOWFISH,$key ,$input,MCRYPT_ENCRYPT,'12345678');
echo bin2hex($encrypted);
?>
Die Ausgabe:
---------------------------
Testprog
---------------------------
Delphi: E51A3C77A4D01A2BA9D3
PHP: e51a3c77a4d01a2b4540b5e1767b982b
---------------------------
OK
---------------------------

Eins iss megakurz das andere länger.. kp.. :(

gammatester 25. Jul 2008 18:31

Re: Mit PHP verschlüsseln
 
Zitat:

Zitat von Angel4585
---------------------------
Testprog
---------------------------
Delphi: E51A3C77A4D01A2BA9D3
PHP: e51a3c77a4d01a2b4540b5e1767b982b
---------------------------
OK
---------------------------

Eins iss megakurz das andere länger.. kp.. :(

Hagen's DEC Code kannst Du so vergessen, da er, wenn der Quelltext nicht Blocklänge (bei Blowfish 8 Byte) hat, den Rest mit CFB8 bearbeitet. PHP sieht sinnvoll aus: da 10 Zeichen verschlüsselt werden, kommen 2 Blocks = 16 Bytes = 32 Hexnibbels raus.

Auf PHP-Seite mußt Du jetzt "nur" noch zwei Sachen rauskriegen:

1. Welcher IV benutzt wird (random etc), und wie Du auf Delphiseite daran kommst.
2. Welches Padding benutzt wird, um auf volle Blocklänge aufzufüllen. Auf Delphiseite dann manuell einbauen.


Willkommen im Cryptodschungel!
Gruß Gammatester

gammatester 25. Jul 2008 18:59

Re: Mit PHP verschlüsseln
 
Zitat:

Zitat von gammatester
Auf PHP-Seite mußt Du jetzt "nur" noch zwei Sachen rauskriegen:

1. Welcher IV benutzt wird (random etc), und wie Du auf Delphiseite daran kommst.
2. Welches Padding benutzt wird, um auf volle Blocklänge aufzufüllen. Auf Delphiseite dann manuell einbauen.

Nachtrag zu den beiden Punkten:

Zu 1: Ich meine selbstverständlich den normalen Produktions-IV, bei Deinem Beispiel ist er ja '12345678'.
Zu 2: Wenn man Deine Angaben durch Crypto++ jagt, kann ich den Ciphertext reproduzieren, wenn PKCS#7-Padding benutzt wird.

Gruß Gammatester

Angel4585 31. Jul 2008 11:37

Re: Mit PHP verschlüsseln
 
Ich komm da nicht weiter :wall:
Habt ihr mir ein kleines funktionierendes Codebeispiel? Also PHP-Skript + Delphicode?
(Ja ich weis normalerweise wird sowas hier nicht gemacht aber ich komm schlichtweg nicht weiter :( )

BTW: Das einzige was bisher funktioniert hat war ne einfach XOR Verschlüsselnung(kann man das Verschlüsselung nennen? :gruebel: ) und Base64.
Beides aber ja nicht gerade sicher :pale:

Angel4585 31. Jul 2008 13:28

Re: Mit PHP verschlüsseln
 
:firejump::firejump::firejump::firejump: ES GEEEEHT!!! :firejump::firejump::firejump::firejump:

Delphi-Quellcode:
const
  KeySize = 32; // 32 bytes = 256 bits
  BlockSize = 8; // 16 bytes = 128 bits  
       
// Pad a string with zeros so that it is a multiple of size
function PadWithZeros(const str : string; size : integer) : string;
var
  origsize, i : integer;
begin
  Result := str;
  origsize := Length(Result);
  if ((origsize mod size) <> 0) or (origsize = 0) then
  begin
    SetLength(Result,((origsize div size)+1)*size);
    for i := origsize+1 to Length(Result) do
      Result[i] := #0;
  end;
end;

procedure Encrypt;
var
  Cipher : TDCP_blowfish;
  Data, Key, IV : string;
  tkey, tdata, tiv : string;
begin
  tkey := '12345678901234567890123456789012';
  tdata := 'Teststring';
  tiv := '12345678';
  // Pad Key, IV and Data with zeros as appropriate
  Key := PadWithZeros(tkey,KeySize);
  IV := PadWithZeros(tiv,BlockSize);
  Data := PadWithZeros(tdata,BlockSize);
  // Create the cipher and initialise according to the key length
  Cipher := TDCP_blowfish.Create(Self);
  if Length(tkey) <= 16 then
    Cipher.Init(Key[1],128,@IV[1])
  else if Length(tkey) <= 24 then
    Cipher.Init(Key[1],192,@IV[1])
  else
    Cipher.Init(Key[1],256,@IV[1]);
  // Encrypt the data
  Cipher.EncryptCBC(Data[1],Data[1],Length(Data));
  // Free the cipher and clear sensitive information
  Cipher.Free;
  FillChar(Key[1],Length(Key),0);
  // Display the Base64 encoded result
  ShowMessage(Base64EncodeStr( Data));
end;

procedure decrypt;
var
  Cipher : TDCP_blowfish;
  Data, Key, IV : string;

  tkey, tdata, tiv : string;
  get : string;
begin
  tkey := '12345678901234567890123456789012';
  tdata := 'asdf';
  tiv := '12345678';
  get := IdHTTP1.Get('http://localhost/crypt/mawe.php');
  // Pad Key and IV with zeros as appropriate
  Key := PadWithZeros(tkey,Keysize);
  IV := PadWithZeros(tiv,blocksize);
  // Decode the Base64 encoded string
  Data := Base64DecodeStr(get);
  // Create the cipher and initialise according to the key length
  Cipher := TDCP_blowfish.Create(Self);
  if Length(tkey) <= 16 then
    Cipher.Init(Key[1],128,@IV[1])
  else if Length(tkey) <= 24 then
    Cipher.Init(Key[1],192,@IV[1])
  else
    Cipher.Init(Key[1],256,@IV[1]);
  // Decrypt the data
  Cipher.DecryptCBC(Data[1],Data[1],Length(Data));
  // Free the cipher and clear sensitive information
  Cipher.Free;
  FillChar(Key[1],Length(Key),0);
  // Display the result
  ShowMessage( Data+sLineBreak+get);
end;
Und das PHP Pendant(Schreibt man das so? :gruebel: ) zur Encrypt Methode dazu:

Code:
<?php


$key = "12345678901234567890123456789012";
$input = "Teststring";
$encrypted = mcrypt_cbc(MCRYPT_BLOWFISH,$key ,$input,MCRYPT_ENCRYPT,"12345678");
echo base64_encode($encrypted);


?>

gammatester 31. Jul 2008 14:35

Re: Mit PHP verschlüsseln
 
Hallo Angel4585,

Inzwischen hast Du ja selbst eine Lösung gefunden. Ich wollte gerade nach einiger Zeit das (neben der Arbeit erstellte) Fragment unten abschicken. Bleibt jetzt eigentlich nur noch der Hinweis/Frage zum IV. Eigentlich sollte er ja nicht konstant sein, und müßte dann zwischen Delphi und PHP kommuniziert werden. Da er nicht geheim sein muss, kann man in zB als die ersten Zeichen versenden.

Zitat:

es ist eigentlich einfacher als ich gedacht hatte. Dein PHP-Codefragment benutzt nämlich 0-Padding (nicht wie ich irrtümlich geschrieben hatte PKCS). Also Du must nur auf Vielfaches der Blocklänge (=8) mit binär 0 auffüllen. Hier ein DEC-Bsp-Prog, daß die PHP-Ausgabe reproduziert:

Delphi-Quellcode:
program php_bf_demo;

{$apptype console}

uses
  SysUtils, Classes,DECCipher,decfmt;

var
  d: TCipher_Blowfish;
  s: string;
begin
  d := TCipher_Blowfish.create;
  d.Mode := cmCBCx;
  d.Init('Passwort','12345678');
  s := d.EncodeBinary('Teststring'#0#0#0#0#0#0,TFormat_HEX);
  writeln('DEC: ',s);
  writeln('PHP: ', 'E51A3C77A4D01A2B4540B5E1767B982B');
end.
Ausgabe:
DEC: E51A3C77A4D01A2B4540B5E1767B982B
PHP: E51A3C77A4D01A2B4540B5E1767B982B

Gruß Gammatester


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:15 Uhr.
Seite 2 von 2     12   

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