Einzelnen Beitrag anzeigen

Angel4585

Registriert seit: 4. Okt 2005
Ort: i.d.N.v. Freiburg im Breisgau
2.199 Beiträge
 
Delphi 2010 Professional
 
#16

Re: Mit PHP verschlüsseln

  Alt 31. Jul 2008, 14:28
ES GEEEEHT!!!

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? ) zur Encrypt Methode dazu:

Code:
<?php


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


?>
Martin Weber
Ich bin ein Rüsselmops
  Mit Zitat antworten Zitat