开发者

PHP Function to Delphi Function

I am trying to create a PHP script, and a Delphi program to "talk" with it. In order to keep it secure, I want to encrypt the outgoing text from both sides, so it makes to use the same encryption function on both ends.

This is the function I found for PHP:

function convert($str,$ky=''){
  if($ky=='')return $str;
  $ky=str_replace(chr(32),'',$ky);
  if(strlen($ky)<8)exit('key error');
  $kl=strlen($ky)<32?strlen($ky):32;
  $k=array();
  for($i=0;$i<$kl;$i++){
    $k[$i]=ord($ky{$i})&0x1F;
  }
  $j=0;
  for($i=0;$i<strlen($str);$i++){
    $e=ord($str{$i});
    $str{$i}=$e&0xE0?chr($e^$k[$j]):chr($e);
    $j++;
    $j=$j==$kl?0:$j;
  }
  return $str;
} 

I cant seem to be able to convert it to Delphi. Help is grea开发者_如何转开发tly apreciated! Thanks, Jeff


It's a function that receives two strings and returns another string. I'll include variable declarations in comments as they're introduced in the code; put them at the top of the function.

function convert(str: AnsiString; const key: AnsiString = ''): AnsiString;

If the key is empty, then the result is simply str:

begin
  if key = '' then
    Exit(str);

The first parameter is the value to be "encrypted," and the second is the key to use for that encryption. The key needs to be at least eight non-space characters long; anything beyond 32 is ignored. If the key is too short, the PHP script would terminate; we'll use Delphi's Assert statement instead since it's clear that the code should never even have executed if the key is wrong. (Script-termination is not a recoverable error that the user would be expected to fix.) The PHP code uses the ?: operator to select the desired value for the length, but Delphi's Min function (from the Math unit) expresses the desire more clearly.

  // var ky: AnsiString;
  ky := StringReplace(key, ' ', '', [rfReplaceAll]);
  Assert(Length(ky) >= 8, 'key error');
  // var kl: Integer;
  kl := Min(Length(ky), 32);

The array k is used to hold numbers representing the lower five bits of each character in the key. In PHP, an array will automatically grow to whatever size it needs based on the index used. In Delphi, we need to allocate the space in advance. Since it's set in a loop that goes over each character of the key, we know the array will be the same length.

  // var k: array of Byte;
  SetLength(k, kl);
  // var i: Integer;
  for i := 0 to Pred(kl) do
    k[i] := Ord(ky[i+1]) and $1f;

Next, each character in the string that has its seventh bit set gets modified according to each successive byte in the k array. The j variable keeps track of which key byte we'll use next.

  // var j: Integer;
  j := 0;
  for i := 1 to Length(str) do begin
    // var e: Byte;
    e := Ord(str[i]);
    if (e and $e0) <> 0 then
      str[i] := AnsiChar(e xor k[j]);
    Inc(j);
    if j = kl then
      j := 0;
    // The previous three lines can also be written j := (j + 1) mod kl
  end;

Finally, we return the new value of str:

  Result := str;
end;


Have at the Delphi Cryptography Package by David Barton. There is an small example which shows how to get PHP and Delphi encryption working together.


I think you really need to bridge your app, then take a look at this, 'cause it is a litte bit complex to try explain here. Actually it is usefull to integrate app PHP/Java via bridge, but I think exists something on that way to integrate PHP/Delphi also via bridge.

ZEND Server

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜