PHP encryption algorithm conversion delphi problems

Source: Internet
Author: User
PHP encryption algorithm conversion delphi problems
Function encrypt ($ string, $ operation, $ key = '') {$ key = md5 ($ key); $ key_length = strlen ($ key ); $ string = $ operation = 'D '? Base64_decode ($ string): substr (md5 ($ string. $ key), 0, 8 ). $ string; $ string_length = strlen ($ string); $ rndkey = $ box = array (); $ result = ''; for ($ I = 0; $ I <= 255; $ I ++) {$ rndkey [$ I] = ord ($ key [$ I % $ key_length]); $ box [$ I] = $ I;} for ($ j = $ I = 0; $ I <256; $ I ++) {$ j = ($ j + $ box [$ I] + $ rndkey [$ I]) % 256; $ tmp = $ box [$ I]; $ box [$ I] = $ box [$ j]; $ box [$ j] = $ tmp;} for ($ a = $ j = $ I = 0; $ I <$ string_length; $ I ++) {$ a = ($ a + 1) % 256; $ j = ($ j + $ box [$ a]) % 256; $ tmp = $ box [$ a]; $ box [$ a] = $ box [$ j]; $ box [$ j] = $ tmp; $ result. = chr (ord ($ string [$ I]) ^ ($ box [($ box [$ a] + $ box [$ j]) % 256]);} if ($ operation = 'd') {if (substr ($ result,) = substr (md5 (substr ($ result, 8 ). $ key),) {return substr ($ result, 8) ;}else {return '';}} else {return str_replace ('= ','', base64_encode ($ result); }}$ id = '123sdfsdf '; $ key = 'aasd1234567985fdgdfdfgdg'; $ token = encrypt ($ id, 'e', $ key ); echo 'encryption :'. encrypt ($ id, 'e', $ key); echo'
'; Echo' decryption: '. encrypt ($ token, 'D', $ key );



function encrypt(str, operation, key: string): string;  function StrToMD5(s: string): string;  var    Md5Encode: TIdHashMessageDigest5;  begin         Md5Encode := TIdHashMessageDigest5.Create;    Result := Md5Encode.AsHex(Md5Encode.HashValue(S));    Md5Encode.Free;       end;var  key_length: Integer;  string_length: Integer;  I, j, A: Integer;  rndkey: array[0..255] of Byte;  Box: array[0..255] of Byte;  tmp: Byte;begin  key := LowerCase(StrToMD5(key));  key_length := Length(key);  if operation = 'D' then    str := DecodeString(str)  else    str := Copy(lowerCase(StrToMD5(str + key)), 1, 8) + str;  string_length := Length(str);  result := '';  for i := 0 to 255 do  begin    rndkey[i] := ord(key[(i mod key_length) + 1]);    Box[i] := i;  end;  j := 0;  for i := 0 to 255 do  begin    j := (j + Box[i] + rndkey[i]) mod 256;    tmp := Box[i];    Box[i] := Box[j];    Box[j] := tmp;  end;  a := 0;  j := 0;  for i := 1 to string_length do  begin    A := (A + 1) mod 256;    j := (j + Box[A]) mod 256;    tmp := Box[A];    Box[A] := Box[j];    Box[j] := tmp;    result := result + chr(ord(str[i]) xor (Box[(Box[A] + Box[j]) mod 256]));  end;  if (operation = 'D') then  begin      if (copy(result, 1, 8) = copy(lowerCase(StrToMd5(copy(result, 9, Length(Result) - 8) + key)), 1, 8)) then    begin      Result := copy(result, 9, Length(Result) - 8);    end    else    begin      Result := '';    end;  end  else  begin    Result := StringReplace(EncodeString(result), '=', '', [rfReplaceAll]);  end; end;

I found my PHP encryption algorithm on the Internet, but when I converted it into DELPHI, only the correct encryption and decryption were incorrect.
Ask your predecessors for a perfect conversion


Reply to discussion (solution)

There is no technical difficulty at all. you just won't debug it.
Since the code can be transferred, it is clear which line corresponds to which line.
Take a line of php and output the variable. delphi outputs the variable. if the variable value is different, this line is wrong.
The program will be there to check which line is wrong, but it is impossible to compare a line.
Therefore, finding the problem is only a matter of time. it is time-consuming and labor-consuming to find the problem.

What is your encryption algorithm?

Is delphi's DecodeString corresponding to php's base64_decode?
So how does the DecodeString function handle the defective base64 string?
We recommend that you complete the = at the end

There is no technical difficulty at all. you just won't debug it.
Since the code can be transferred, it is clear which line corresponds to which line.
Take a line of php and output the variable. delphi outputs the variable. if the variable value is different, this line is wrong.
The program will be there to check which line is wrong, but it is impossible to compare a line.
Therefore, finding the problem is only a matter of time. it is time-consuming and labor-consuming to find the problem.


After debugging DecodeString, the decrypted string delphi is two fewer bits than php.

Is delphi's DecodeString corresponding to php's base64_decode?
So how does the DecodeString function handle the defective base64 string?
We recommend that you complete the = at the end



I'll try it later,

Is delphi's DecodeString corresponding to php's base64_decode?
So how does the DecodeString function handle the defective base64 string?
We recommend that you complete the = at the end


The = at the end of the bucket cannot be completed. now we find that the character decrypted by DecodeString has an error.

According to the parameter you provided, $ token has a = deleted at the end.
This is consistent with your statement that delphi has two fewer places than php.
Base64 converts the three characters into four bytes. after the two characters at the end of the string are expanded, = is required.
If the decoding is discarded due to a disability, it will be exactly 2 characters missing.

How did you make up? Post Code




According to the parameter you provided, $ token has a = deleted at the end.
This is consistent with your statement that delphi has two fewer places than php.
Base64 converts the three characters into four bytes. after the two characters at the end of the string are expanded, = is required.
If the decoding is discarded due to a disability, it will be exactly 2 characters missing.



According to your method, my delphi version is followed by =
Then the decrypted characters are converted to Asc code. By comparison, delphi and PHP are
Delphi edition
1571920612038416510212118647183621641859132545612373206113391
Php version
157192061203841651021211864718362164185913254561237320611339
1 is missing

If operation = 'd' then
Add (4-Length (str) % 4) % 4 = to str
Str: = DecodeString (str)
Else

Or you can see
DecodeString ("YWI = ")
And
DecodeString ("YWI ")
What are the results?

If operation = 'd' then
Add (4-Length (str) % 4) % 4 = to str
Str: = DecodeString (str)
Else



You can use your method. The four integers must be filled in. thank you, moderator.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.