1

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 greatly apreciated! Thanks, Jeff

14
  • 4
    What have you got so far? Which lines specifically are you having trouble with? Commented Jan 26, 2011 at 17:31
  • 1
    That looks pretty darn secure to me Commented Jan 26, 2011 at 17:31
  • First step: Format the code so it's readable. Line break after each semicolon, indentations inside "if" and "for" blocks, spaces around binary operators. Commented Jan 26, 2011 at 17:33
  • @Rob, I did the code formatting before reading your comment. Commented Jan 26, 2011 at 17:40
  • @Rob , thats the code I found. @David , sarcasm? :P @webbiedave , the Array part. Commented Jan 26, 2011 at 17:41

3 Answers 3

8

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;
Sign up to request clarification or add additional context in comments.

28 Comments

+1 You earned some rep there. You still should have let Jeff try himself first though since that would have helped his learning.
@Rob Thanks! It wont compile though, I run D2010. Maybe I should have said that earlier huh?
@Jeff Are you really asking Rob to fix a couple of minor compilation problems? Surely you can do that?
@Jeff Not accept, up-vote! Be generous, Rob spent quite some time on that, and explained what he was doing. First of all you complained that it didn't compile. Then you made some joke about a virus. Really, if you show a bit of gratitude then it will encourage people to help you. I'm only trying to be constructively helpful myself, because I know that you are still young and impressionable.
@Jeff @Chris @Rob No I've got it, I'll edit the correct in just one second, it's to the line where k[i] is assigned. A classic case of 0-based arrays and 1-based strings, one Delphi design decision that I'm sure everyone at Embarcadero wish they could reverse with the aid of a time machine!
|
1

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.

Comments

-3

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

13 Comments

-1. I cannot see how this is a helpful answer at all. If you really think it answers the question, then please edit your answer to make it more clear what "bridging" an application means and why Zend Server would allow Jeff to do that. Also explain how he would use that in his Delphi program.
You've basically told him to "bridge the app", when he's basically already asking how to do that. "how do I tie my shoe?" "by tieing it"
All I want is to get that function converted to Delphi.
If I could vote you down again for that "figure it out" remark, I'd do it. The point of answering questions on Stack Overflow is to help others figure things out. If you can't be bothered with that, then I suggest you find something else to do.
I dont get why so many people down-repped this answer. He was just trying to help out..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.