[SOLVED]who have xxtea.pas for lazarus? lazarus / 21-General ---------------------------------------------------------------------- wylton · 2021-03-31 16:21 i found https://github.com/xxtea/xxtea-delphi, but it not work for lazarus [Edited to fix typo in topic: SLOVED -> SOLVED :] ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-03-31 17:28 Hey Wilton, What does xxtea have that DCPCrypt doesn't? Or even the stuff from xor-el: HashLib4Pascal, CryptoLib4Pascal or BaseNcodingPascal? Cheers, Gus ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-03-31 17:40 Hey Wilton, Quote from: wylton on March 31, 2021, 06:21:36 pmi found https://github.com/xxtea/xxtea-delphi, but it not work for lazarus I'll have to disagree with you since I just made it work with no change at all, as you can see from the attached example application. Cheers, Gus ---------------------------------------------------------------------- dsiders · 2021-03-31 18:07 Quote from: Gustavo 'Gus' Carreno on March 31, 2021, 07:28:11 pmHey Wilton, What does xxtea have that DCPCrypt doesn't? Or even the stuff from xor-el: HashLib4Pascal, CryptoLib4Pascal or BaseNcodingPascal? Cheers, Gus Umm... the hardened XXTEA algorithm? https://en.wikipedia.org/wiki/XXTEA ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-03-31 18:12 Hey dsiders, Quote from: dsiders on March 31, 2021, 08:07:29 pmUmm... the hardened XXTEA algorithm? It appears that I have egg on my face, LOL!! Thanks for the link Cheers, Gus ---------------------------------------------------------------------- ASBzone · 2021-03-31 18:19 Quote from: dsiders on March 31, 2021, 08:07:29 pmQuote from: Gustavo 'Gus' Carreno on March 31, 2021, 07:28:11 pmHey Wilton, What does xxtea have that DCPCrypt doesn't? Or even the stuff from xor-el: HashLib4Pascal, CryptoLib4Pascal or BaseNcodingPascal? Cheers, Gus Umm... the hardened XXTEA algorithm? https://en.wikipedia.org/wiki/XXTEA I wouldn't call it "hardened".    Sure, it is an improvement over the original algorithm, but the article (which I was going to link myself) indicates that in 2010, it was shown to have a vulnerability that was relatively trivial to exploit at that time.   You can only imagine its weakness in 2021. If this were for a non-cryptographic hash, I would say, "so what?"   But there's no real point in aiming for a secure cryptographic hash, and then then picking a flawed one. I'd strongly reconsider using this algorithm in favor of one of the ones Gus mentioned above. Umm... the hardened XXTEA algorithm? ---------------------------------------------------------------------- dsiders · 2021-03-31 18:30 Quote from: ASBzone on March 31, 2021, 08:19:36 pmQuote from: dsiders on March 31, 2021, 08:07:29 pmQuote from: Gustavo 'Gus' Carreno on March 31, 2021, 07:28:11 pmHey Wilton, What does xxtea have that DCPCrypt doesn't? Or even the stuff from xor-el: HashLib4Pascal, CryptoLib4Pascal or BaseNcodingPascal? Cheers, Gus Umm... the hardened XXTEA algorithm? https://en.wikipedia.org/wiki/XXTEA I wouldn't call it "hardened".    Sure, it is an improvement over the original algorithm, but the article (which I was going to link myself) indicates that in 2010, it was shown to have a vulnerability that was relatively trivial to exploit at that time.   You can only imagine its weakness in 2021. If this were for a non-cryptographic hash, I would say, "so what?"   But there's no real point in aiming for a secure cryptographic hash, and then then picking a flawed one. I'd strongly reconsider using this algorithm in favor of one of the ones Gus mentioned above. Umm... the hardened XXTEA algorithm? Well, I don't have a dog in this fight. The article said it's a hardened algorithm. Let the product placement ads resume. ---------------------------------------------------------------------- wylton · 2021-03-31 18:43 Quote from: Gustavo 'Gus' Carreno on March 31, 2021, 07:40:14 pmHey Wilton, Quote from: wylton on March 31, 2021, 06:21:36 pmi found https://github.com/xxtea/xxtea-delphi, but it not work for lazarus I'll have to disagree with you since I just made it work with no change at all, as you can see from the attached example application. Cheers, Gus Are you trid decrypt? It is not get result. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-03-31 19:00 Hey Wylton, Quote from: wylton on March 31, 2021, 08:43:41 pmAre you trid decrypt? It is not get result. Have you tried the *.zip file I included on my reply? I used the example that's present in the xxtea repo. It compiles, no errors and when it runs, something comes out on the WriteLN(). What am I missing? Cheers, Gus ---------------------------------------------------------------------- wylton · 2021-04-01 14:31 Quote from: Gustavo 'Gus' Carreno on March 31, 2021, 09:00:10 pmHey Wylton, Quote from: wylton on March 31, 2021, 08:43:41 pmAre you trid decrypt? It is not get result. Have you tried the *.zip file I included on my reply? I used the example that's present in the xxtea repo. It compiles, no errors and when it runs, something comes out on the WriteLN(). What am I missing? Cheers, Gus with delphi it work well. with Lazarus is error. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-04-01 16:43 Hey Wylton, Yeah, ouch, I really did miss something, rather huge I'm afraid. I'm really sorry about that!! Sorry, but a deep dive is kinda needed into the code to see what's happening. And that's pretty much out of my depth, unfortunately. I'll leave it to someone more savvy in manipulating bits in a crypto context. Cheers, Gus ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-04-01 21:20 Hey Jamie, Quote from: jamie on April 01, 2021, 10:38:15 pmI see you are not using {$Mode objFpc}{$H+} at the top of your source code.. You're absolutely right, I'm not. But funny enough, from the screenshot, Wylton is. I've now added the missing directives to Example.lpr, XXXTEA.pas and Base64.pas but it still doesn't spit out any text on the decrypted string. Cheers, Gus ---------------------------------------------------------------------- jamie · 2021-04-01 21:47 Delphi assumes UNICODESTRING these days which are double wide but fpc is UTF8 and its obvious its being forced to that just to make sure...  But here is the issue, if XXTEA.PAS assumes also UNICODE for what ever reason things may not code the same...  On top of that, one system using UTF8 and the other UNICODE are not going to produce the same encrypt so if two different programs expect to exchange this data it isn't going to work is it?  I guess the proper thing to do to start with is to case the string to a UnicodeString instead of a UTF8String. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-04-01 22:28 Hey Jamie, The encodings is rather insightful indeed. How can we modify the source to take that into account? Would we not need to look into ByteOf()? But before that, while debugging, the whole session ends abruptly at the last line of function XXTEADecrypt(var V, K: TUint32Array): TUint32Array;: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function XXTEADecrypt(var V, K: TUint32Array): TUint32Array;begin// The whole decrypt process  Result:= V;end; No error, no exception, just ends, period. Which is rather intriguing since the XXTEAEncrypt() function spits put the array with no problems!! What's with that? Cheers, Gus ---------------------------------------------------------------------- wylton · 2021-04-01 22:51 i found error in funtion ToUint32Array() when use K :=  BytesOf('1234567890'); have to add  for I := 0 to N do   begin      Result := 0;   end; i will test tomorrow...sleep now。。 Perhaps It is lazarus bug... ---------------------------------------------------------------------- wylton · 2021-04-02 05:28 Quote from: jamie on April 02, 2021, 04:11:44 amI don't know who to blame here, Fpc or the coder ? It appears Delphi always zeros the managed array upon entry to the function that has a managed array also for the RETURN.. In this case here FPC is not nulling the RESULT array so it can have garbage and the way the code is handled in the Function ToUint32Array(……):TUint32ARray;  it uses this junk data and creates the wrong values.. At the start of Function ToUint32Array, insert this.. Begin  Setlength(Result, 0); …. end; Personally I think this could be a bug in fpc but it's always been good practice to initialize your variables to something and in this case setting it to 0 to ensure its nothing incase the code does not fill in all the indices which is what is happening here in the function.  The Array should be zer0 length up on entry, its not and when you resize it the array preserves what is in there. you are rigth. it is a bug. final i changed like this: function ToUint32Array(const Data: TBytes; IncludeLength: Boolean): TUint32Array; var   N, I, L: TUint32;   s, x: string;   k: dword;   v: TUint32Array; begin   L := Length(Data);   if ((L and 3) = 0) then N := L shr 2 else N := (L shr 2) + 1;   if (IncludeLength) then begin     SetLength(v, N + 1);     v[N] := L;   end   else SetLength(v, N);   for I := 0 to L - 1 do   begin     v[I shr 2] := v[I shr 2] or (uint32( Data) shl ((I and 3) shl 3));   end;     result:=v; end;  other: if use  Begin  Setlength(Result, 0); …. end; I get Memory overflow。。。 ---------------------------------------------------------------------- marcov · 2021-04-02 11:50 Showing a page in Delphi help that this is in fact part of the langauge is a good way. ---------------------------------------------------------------------- bytebites · 2021-04-02 13:10 Quote from: wylton on April 02, 2021, 12:51:40 ami found error in funtion ToUint32Array() when use K :=  BytesOf('1234567890'); have to add  for I := 0 to N do   begin      Result := 0;   end; i will test tomorrow...sleep now。。 Perhaps It is lazarus bug... Your code appears wrong, use code tags. (Result := 0;) ---------------------------------------------------------------------- PascalDragon · 2021-04-02 14:04 Quote from: jamie on April 02, 2021, 01:16:49 pmThis of course presents a problem if you are not aware of it.. I don't know if Delphi sets the length to 0 upon entry or not or it just got lucky with null memory each time and there for provides a zero length array on return upon entry.. Delphi doesn't do this either. It totally depends upon the generated code. You'll be "lucky" in more cases than in FPC, but you can trigger the same problems in Delphi. The following example prints 3 with Delphi 10.2 on Win32: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program tarr; {$APPTYPE CONSOLE} function Test1: TArray;begin  SetLength(Result, 3);end; function Test2: TArray;begin  Writeln(Length(Result));end; procedure Test;var  arr: TArray;begin  arr := Test1;  arr := Test2;end; begin  Test;  Readln;end.  So, no, this is not a bug. (And unlike Delphi FPC 3.2.0 and newer at least warn here) If you want to avoid such problems, explicitely use Result := Nil at the start of such a function. Note: the same is also true for the managed string types. ---------------------------------------------------------------------- marcov · 2021-04-02 14:42 We should document such things in the wiki. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-04-02 17:14 Hey all, According to Jamie's suggestion(I think it was him), I created a pull request on the project with these changes: Added compiler directives for FPCFixed a bug under FPCRe-arranged the Example to be both Delphi and Lazarus compatible The mentioned bug fix was just to add Result:= nil; at the beginning of ToUint32Array. Let's see if the maintainers accept it. Cheers, Gus ---------------------------------------------------------------------- wylton · 2021-04-03 04:28 Quote from: Gustavo 'Gus' Carreno on April 02, 2021, 07:14:19 pmHey all, According to Jamie's suggestion(I think it was him), I created a pull request on the project with these changes: Added compiler directives for FPCFixed a bug under FPCRe-arranged the Example to be both Delphi and Lazarus compatible The mentioned bug fix was just to add Result:= nil; at the beginning of ToUint32Array. Let's see if the maintainers accept it. Cheers, Gus put Result:nil at the beginning of function, this is a good idea. thank very mucho for all ! ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-04-03 04:32 Hey Wylton, Quote from: wylton on April 03, 2021, 06:28:21 amput Result:nil at the beginning of function, this is a good idea. Indeed it was. Jamie as a fine eye If the maintainer of the repo on GitHub accepts my Pull Request(PR), you can use the code from there with the example for both Delphi and Linux. Quote from: wylton on April 03, 2021, 06:28:21 amthank very mucho for all ! De nada Saludos, Gus ---------------------------------------------------------------------- PascalDragon · 2021-04-03 09:13 Quote from: marcov on April 02, 2021, 04:42:29 pmWe should document such things in the wiki. More correct would be the Managed types section of the Language Reference Guide. ---------------------------------------------------------------------- marcov · 2021-04-03 11:47 Quote from: PascalDragon on April 03, 2021, 11:13:07 amQuote from: marcov on April 02, 2021, 04:42:29 pmWe should document such things in the wiki. More correct would be the Managed types section of the Language Reference Guide. Then it is distributed again. I'm more thinking about a "common gotchas" kind of page. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-04-06 02:21 Hey all, Just to let you know that the maintainer finally merged my pull request. This means it's now useable with FPC The example, now, also has both a *.dproj and an *.lpi. Cheers, Gus