0

Delphiの関数から同じ結果を返すPHPで関数を作成する必要があります。PHPの抜粋関数で、「/ /」とコメントされているのは、同じ結果を返す代わりに使用できなかった行です。

Delphiコード:

function Crypt_(Action, Src: String): String;
Label Fim;
var KeyLen : Integer;
  KeyPos : Integer;
  OffSet : Integer;
  Dest, Key : String;
  SrcPos : Integer;
  SrcAsc : Integer;
  TmpSrcAsc : Integer;
  Range : Integer;
begin
  if (Src = '') Then
  begin
    Result:= '';
    Goto Fim;
  end;
  Key :=
'YUQL23KL23DF90WI5E1JAS467NMCXXL6JAOAUWWMCL0AOMM4A4VZYW9KHJUI2347EJHJKDF3424SKL K3LAKDJSL9RTIKJ';
  Dest := '';
  KeyLen := Length(Key);
  KeyPos := 0;
  SrcPos := 0;
  SrcAsc := 0;
  Range := 256;
  if (Action = UpperCase('C')) then
  begin
    Randomize;
    OffSet := Random(Range);
    Dest := Format('%1.2x',[OffSet]);
    for SrcPos := 1 to Length(Src) do
    begin
      SrcAsc := (Ord(Src[SrcPos]) + OffSet) Mod 255;
      if KeyPos < KeyLen then KeyPos := KeyPos + 1 else KeyPos := 1;
      SrcAsc := SrcAsc Xor Ord(Key[KeyPos]);
      Dest := Dest + Format('%1.2x',[SrcAsc]);
      OffSet := SrcAsc;
    end;
  end
  Else if (Action = UpperCase('D')) then
  begin
    OffSet := StrToInt('$'+ copy(Src,1,2));
    SrcPos := 3;
  repeat
    SrcAsc := StrToInt('$'+ copy(Src,SrcPos,2));
    if (KeyPos < KeyLen) Then KeyPos := KeyPos + 1 else KeyPos := 1;
    TmpSrcAsc := SrcAsc Xor Ord(Key[KeyPos]);
    if TmpSrcAsc <= OffSet then TmpSrcAsc := 255 + TmpSrcAsc - OffSet
    else TmpSrcAsc := TmpSrcAsc - OffSet;
    Dest := Dest + Chr(TmpSrcAsc);
    OffSet := SrcAsc;
    SrcPos := SrcPos + 2;
  until (SrcPos >= Length(Src));
  end;
  Result:= Dest;
  Fim:
end;

PHPコード:

<?php
function Crypt_($Action, $Src) {
  $Key    = 'YUQL23KL23DF90WI5E1JAS467NMCXXL6JAOAUWWMCL0AOMM4A4VZYW9KHJUI2347EJHJKDF3424SKL K3LAKDJSL9RTIKJ';
  $KeyLen = strlen($Key);
  $KeyPos = 0;
  if ($Action == 'C') {
    $OffSet = rand(0,256);
//  Dest := Format('%1.2x',[OffSet]); //I tried to replace the function "sprintf", but the result is null
    $SrcPos = 1;
    while ($SrcPos <= strlen($Src)) {
      $SrcAsc = (ord($Src[$SrcPos]) + $OffSet) % 255;
      if ($KeyPos < $KeyLen) $KeyPos = $KeyPos + 1; else $KeyPos = 1;
      $SrcAsc = $SrcAsc xor ord($Key[$KeyPos]);
//    Dest := Dest + Format('%1.2x',[SrcAsc]); //I tried to replace the function "sprintf", but the result is null
      $OffSet = $SrcAsc;
      $SrcPos = $SrcPos + 1;
    }
  } else {
//  OffSet := StrToInt('$'+ copy(Src,1,2));
    $Dest   = '';
    $SrcPos = 3;
    while ($SrcPos >= strlen($Src)) {
//    SrcAsc := StrToInt('$'+ copy(Src,SrcPos,2));
      if ($KeyPos < $KeyLen) $KeyPos = $KeyPos + 1; else $KeyPos = 1;
      $TmpSrcAsc = $SrcAsc xor ord($Key[$KeyPos]);
      if ($TmpSrcAsc <= $OffSet) $TmpSrcAsc = 255 + $TmpSrcAsc - $OffSet;
      else $TmpSrcAsc = $TmpSrcAsc - $OffSet;
      $Dest = $Dest + chr($TmpSrcAsc);
      $OffSet = $SrcAsc;
      $SrcPos = $SrcPos + 2;
    }
  }
  return $Dest;
}
?>

また、「xor」、「ord」、「chr」のPHPがDelphiと同じ結果になるのではないかと思います。

皆様のご注目ありがとうございました!

4

1 に答える 1

0

デルファイ

Format引数を持つ関数は、%x整数を文字列の16進表現に変換します。この場合、1バイトです。たとえば、168はに変換さ'A8'ます。

で始まる引数を持つStrToInt関数'$'は、逆変換を実行し、から168を返し'$A8'ます。

PHP

dechexPHPでは、 andhexdec関数を呼び出すことで同じ結果を得ることができます。

呼び出しにプレフィックスhexdecが必要ないことを考慮してください。'$'

于 2013-02-07T17:06:01.223 に答える