Forum > Packages and Libraries
Why do PHP and Lazarus use openssl_encrypt to encrypt, but get different results
paweld:
You can also use CryptoLib4Pascal: https://github.com/Xor-el/CryptoLib4Pascal , which padding the strings itself.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program project_en; {$mode objfpc}{$H+} uses Classes, SysUtils, Base64, ClpCipherUtilities, ClpParametersWithIV, ClpIParametersWithIV, ClpIBufferedCipher, ClpParameterUtilities; function BinStr2Hex(S: Ansistring): Ansistring; var i: Integer; begin Result := ''; for i := 1 to Length(S) do Result := Result + LowerCase(HexStr(Byte(S[i]), 2)); end; function my_encrypt(Data: String; key, IV: String): String; var KeyBytes, IVBytes, Buf, InBuf: TBytes; KeyParametersWithIV: IParametersWithIV; cipher: IBufferedCipher; LBlockSize, LBufStart, Count: Integer; DataString, encryption_key: Ansistring; begin encryption_key := key; if (Length(encryption_key) < 32) then while Length(encryption_key) < 32 do encryption_key := encryption_key + Ansichar(0); encryption_key := Copy(encryption_key, 1, 32); DataString := Data; SetLength(KeyBytes, Length(encryption_key)); Move(encryption_key[1], KeyBytes[0], Length(encryption_key)); SetLength(IVBytes, Length(IV)); Move(IV[1], IVBytes[0], Length(IV)); SetLength(InBuf, Length(DataString)); Move(DataString[1], InBuf[0], Length(DataString)); cipher := TCipherUtilities.GetCipher('RIJNDAEL/CBC/PKCS7'); KeyParametersWithIV := TParametersWithIV.Create(TParameterUtilities.CreateKeyParameter('AES', KeyBytes), IVBytes); cipher.Init(True, KeyParametersWithIV); LBlockSize := cipher.GetBlockSize; SetLength(Buf, Length(InBuf) + LBlockSize); LBufStart := 0; Count := cipher.ProcessBytes(InBuf, 0, Length(DataString), Buf, LBufStart); Inc(LBufStart, Count); Count := cipher.DoFinal(Buf, LBufStart); Inc(LBufStart, Count); SetLength(Buf, LBufStart); SetLength(DataString, LBufStart); Move(Buf[0], DataString[1], LBufStart); Result := EncodeStringBase64(DataString); end; var Key: Ansistring; IV: Ansistring; Data: Ansistring; CBC: Ansistring;begin Data := 'Hello World_____'; Key := '1234567890______'; IV := '______1234567890'; CBC := my_encrypt(Data, Key, IV); WriteLn('KEY : ', Key, ' (', Length(Key), ')'); WriteLn('IV : ', IV, ' (', Length(IV), ')'); WriteLn('DATA : ', Data, ' (', Length(Data), ')'); WriteLn('CBC_BASE64 : ', CBC, ' (', Length(CBC), ')'); readln;end.
wp:
--- Quote from: Thaddy on July 12, 2024, 04:16:36 pm ---although I "offend" some people on purpose...
--- End quote ---
You should not do that.
bills:
--- Quote from: paweld on July 13, 2024, 09:06:15 am ---You can also use CryptoLib4Pascal: https://github.com/Xor-el/CryptoLib4Pascal , which padding the strings itself.
--- End quote ---
Thank you!
I tested your code and got the correct result.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program project_en; {$mode objfpc}{$H+} uses {$IFDEF UNIX} cthreads, {$ENDIF} Classes, SysUtils, CustApp, StrUtils, //IdMultipartFormData, //IdGlobal, IdCoderMIME, idCoder, //IdSSLOpenSSL, IdGlobalProtocols, LazUTF8Classes, //DCPrijndael, Base64, DCPsha1, DCPsha256, DCPcrypt2, DCPrijndael, DCPbase64, base64, DCPconst, ClpEncoders, ClpCipherUtilities, ClpParametersWithIV, ClpIParametersWithIV, ClpIBufferedCipher, ClpParameterUtilities, OpenSSL { you can add units after this }; // encrypt function 1function my_encrypt1(Data: string; key, IV: string): string;var Cipher: TDCP_rijndael; DataString, encryption_key: ansistring; l, i: Integer;begin l := 16 - (Length(Data) mod 16); for i := 1 to l do Data := Data + Chr(l); DataString := Data; encryption_key := key; if (Length(encryption_key) < 32) then while Length(encryption_key) < 32 do encryption_key := encryption_key + ansichar(0); encryption_key := Copy(encryption_key, 1, 32); Cipher := TDCP_rijndael.Create(nil); Cipher.CipherMode := cmCBC; Cipher.Init(encryption_key[1], Length(encryption_key) * 8, @IV[1]); Cipher.EncryptCBC(DataString[1], DataString[1], Length(DataString)); Result := EncodeStringBase64(DataString); Cipher.Free;end; // encrypt function 2function my_encrypt2(Data: String; key, IV: String): String;var KeyBytes, IVBytes, Buf, InBuf: TBytes; KeyParametersWithIV: IParametersWithIV; cipher: IBufferedCipher; LBlockSize, LBufStart, Count: Integer; DataString, encryption_key: Ansistring;begin encryption_key := key; if (Length(encryption_key) < 32) then while Length(encryption_key) < 32 do encryption_key := encryption_key + Ansichar(0); encryption_key := Copy(encryption_key, 1, 32); DataString := Data; SetLength(KeyBytes, Length(encryption_key)); Move(encryption_key[1], KeyBytes[0], Length(encryption_key)); SetLength(IVBytes, Length(IV)); Move(IV[1], IVBytes[0], Length(IV)); SetLength(InBuf, Length(DataString)); Move(DataString[1], InBuf[0], Length(DataString)); cipher := TCipherUtilities.GetCipher('RIJNDAEL/CBC/PKCS7'); KeyParametersWithIV := TParametersWithIV.Create(TParameterUtilities.CreateKeyParameter('AES', KeyBytes), IVBytes); cipher.Init(True, KeyParametersWithIV); LBlockSize := cipher.GetBlockSize; SetLength(Buf, Length(InBuf) + LBlockSize); LBufStart := 0; Count := cipher.ProcessBytes(InBuf, 0, Length(DataString), Buf, LBufStart); Inc(LBufStart, Count); Count := cipher.DoFinal(Buf, LBufStart); Inc(LBufStart, Count); SetLength(Buf, LBufStart); SetLength(DataString, LBufStart); Move(Buf[0], DataString[1], LBufStart); Result := EncodeStringBase64(DataString);end; var Key : AnsiString; IV : AnsiString; Data : AnsiString; CBC1, CBC2 : AnsiString;begin // Data := 'Hello World'; Key := '1234567890______'; IV := '______1234567890'; CBC1 := my_encrypt1(Data, Key, IV); //EncodeUrl(my_encrypt CBC2 := my_encrypt2(Data, Key, IV); //EncodeUrl(my_encrypt //writeLn('加密:'); WriteLn('KEY : ', Key , ' (', Length(Key) , ')'); WriteLn('IV : ', IV , ' (', Length(IV) , ')'); WriteLn('DATA : ', Data , ' (', Length(Data), ')'); //WriteLn('CBC : ', BinStr2Hex(CBC) , ' (', Length(CBC) , ')'); //CBC := (CBC); WriteLn('CBC1_BASE64 : ', CBC1 , ' (', Length(CBC1) , ')'); WriteLn('CBC2_BASE64 : ', CBC2 , ' (', Length(CBC2) , ')'); end.
--- Code: ---KEY : 1234567890______ (16)
IV : ______1234567890 (16)
DATA : Hello World (11)
CBC1_BASE64 : xxIF/qPw4Nz2FBumiTBH+Q== (24)
CBC2_BASE64 : xxIF/qPw4Nz2FBumiTBH+Q== (24)
--- End code ---
Navigation
[0] Message Index
[*] Previous page