Hi,
I need help in converting this portion of Delphi codes into PHP. My PHP application is supposed to retrieve an encoded password from the registry (already done so) and then decode the password for use in connecting to a database. The encoded password is stored into the registry by another application which is written in Delphi. To decode the password, I need to use the same encoding key and method as used in the Delphi app. That delphi application has a function called DecodePwd which is used to decoded the encoded passwords. I am trying to convert it into PHP so that my PHP application can correctly decode the password. This portion of the DecodePwd function written in Delphi is giving me headaches in converting to PHP. I wonder if anyone out there will be able to help me solve the problem?
- CODE SNIPPETS START HERE -
.......... Other portion of the codes which I have no problem in converting into PHP ...........
//locate memory
getmem(r,255);
s:=pchar(inSC);
R^:= S^;
for I := 1 to Length(S) do begin
// decoded
(R+I)^ := char(byte((S+I)^) xor (Key shr 8));
// get next key value
Key := (byte((S+I)^) + Key) * C1 + C2;
end;
// assigned the terminated char of string
(r+i)^:=chr(0);
// return the result
result:=string(r);
- CODE SNIPPETS END HERE -
Would appreciate urgent help from any of you out there.
Thanks.
Rgds,
Sharon
Help in converting a portion of Delphi codes into PHP
Moderator: General Moderators
a WORD is a 16bit unsigned value?
Thenmight be equvivalent.
Then
Code: Select all
<?php
/**
$S : input string
$Key : given key
$C1 : a constant
$C2 : a constant
$R : result, decoded string
*/
// for I := 1 to Length(S) do begin
for ($I=0; $I<strlen($S); $I++)
{
// (R+I)^ := char(byte((S+I)^) xor (Key shr 8));
// the "value" of the character at position I xor the high-byte of key
$R .= chr( ord($S{$I})^($Key >> 8) );
// Key := (byte((S+I)^) + Key) * C1 + C2;
// the "value" of the character at position I + Key * C1 + C2 as 16bit value (2^16=65536)
$Key = (int)((ord($S{$I}) + $Key)*$C1 + $C2) % 65536;
}
?>Thanks Volka. It looks correct. However, it just doesn't work fine for me.
The password encoded with key = 11 is "118|105|146|96|59|194|39|3|253|16|230|" and its corresponding plain text password is "visualtron".
However, when I try to decode the encoded password, I do not get "visualtron". Instead, I get something else.
The full Delphi code for DecodePwd is:
My trial PHP codes is:
I couldn't figure out what was wrong, any ideas?
The password encoded with key = 11 is "118|105|146|96|59|194|39|3|253|16|230|" and its corresponding plain text password is "visualtron".
However, when I try to decode the encoded password, I do not get "visualtron". Instead, I get something else.
The full Delphi code for DecodePwd is:
Code: Select all
const
C1=34567;
C2=45678;
function DecodePwd( inS:string; Key: Word): string;
var
I: integer;
r:pchar;
s:pchar;
str:string;
inSC:string;
begin
i:=pos('|',inS);
inSC:='';
while(i>0) do
begin
str:=copy(inS,1,i-1);
inS:=copy(inS,i+1,length(inS));
i:=pos('|',inS);
inSC:=inSC+chr(strtoint(str));
end;
getmem(r,255);
s:=pchar(inSC);
R^:= S^;
for I := 1 to Length(S) do begin
(R+I)^ := char(byte((S+I)^) xor (Key shr 8));
Key := (byte((S+I)^) + Key) * C1 + C2;
end;
(r+i)^:=chr(0);
result:=string(r);
end;Code: Select all
<?
$C1 = 34567;
$C2 = 45678;
function DecodePwd($inS, $key) {
global $C1, $C2;
$i=strpos($inS, "|");
$inSC="";
while($i!==false) {
$str = substr($inS, 0, $i);
$inS = substr($inS, $i+1, strlen($inS)-$i-1);
$i=strpos($inS, "|");
$inSC = $inSC . chr(intval($str));
}
$S = $inSC;
$R = $S;
for ($i=0; $i < strlen($S); $i++) {
// decoded
$R[$i] = chr(ord($S[$i]) ^ ($key >> 8));
// get next key value
$key = intval((ord($S[$i]) + $key)*$C1 + $C2) % 65536;
}
$result = $R;
return $result;
}
$dbpwd = "118|105|146|96|59|194|39|3|253|16|230|"; // visualtron
echo DecodePwd($dbpwd, 11);
?>I have finally solved the problem. Thanks Volka for your initial help. Without your help, I won't have gone far in solving the problem 
The correct code for this portion:
should be:
The correct code for this portion:
Code: Select all
$R = $S;
for ($i=0; $i < strlen($S); $i++) {
// decoded
$R[$i] = chr(ord($S[$i]) ^ ($key >> );
// get next key value
$key = intval((ord($S[$i]) + $key)*$C1 + $C2) % 65536;
}
$result = $R;
return $result;Code: Select all
$R = $S[0];
for ($i=1; $i < strlen($S); $i++) {
// decoded
$R .= chr(ord($S[$i]) ^ ($key >> ); // ord = byte
// get next key value
$key = intval((ord($S[$i]) + $key)*$C1 + $C2) % 65536;
}
$R .= chr(0);
$result = $R;
return $result;