Recent

Author Topic: Encrypt in Node.js Decrypt in FPC  (Read 3175 times)

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Encrypt in Node.js Decrypt in FPC
« on: August 03, 2021, 05:51:05 pm »
Hi, any example on how to encrypt a JSON file in node.js and decrypt it in FPC?

I can use any library so no problem with that.

The idea is to use just an [az-09] key of 5 or 6 digits.

The idea is basically to don't send the plain json to the desktop client.

Regards.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Encrypt in Node.js Decrypt in FPC
« Reply #1 on: August 03, 2021, 06:52:24 pm »

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: Encrypt in Node.js Decrypt in FPC
« Reply #2 on: August 03, 2021, 07:49:52 pm »

I don't know much about JSON, but here is encode64/decode64 an ansistring for Windows.
Code: Pascal  [Select][+][-]
  1.  
  2.  program encode64 ;
  3.  
  4.     var
  5.     a,b,L,filename:ansistring;
  6.  
  7.  Function CryptBinaryToStringA(p:pchar;i:integer;j:integer;p2:pchar;p3:pointer):integer ; cdecl external 'Crypt32.dll';
  8.  Function CryptStringToBinaryA(p:pchar;l1:integer;L5:integer;b:pchar;L2:pointer;L3:pointer;l4:pointer):integer ; cdecl external 'Crypt32.dll';
  9.  
  10.  
  11.  Function Base64Decode(ss:ansistring):ansistring;
  12.  var
  13.  lngth,n:longint;
  14.  answer:ansistring;
  15.  b:array of byte;
  16.  panswer:pchar;
  17.  begin
  18.     lngth:=length(ss);
  19.     SetLength(b,lngth);
  20.     CryptStringToBinaryA(@ss[1],Lngth,1,@b[0],@Lngth,nil,nil);
  21.     setlength(answer,lngth);
  22.      panswer:=@answer[1];
  23.     for n :=0 to lngth   do
  24.     begin
  25.         ord(panswer[n]):=(b[n]) ;
  26.       end;
  27.     exit(answer);
  28. End;
  29.  
  30. Function Base64Encode(p :ansistring ):ansistring;
  31. var
  32. L:longint;
  33. s:array of byte;
  34. begin
  35. L:=length(p)*2;
  36. SetLength(s,L);
  37.    CryptBinaryToStringA(@p[1],Length(p),1,@s[0],@L);
  38.     exit(pchar( @s[0]));
  39. End;
  40.  
  41.  
  42.         var
  43.         s,ec,dc:ansistring;
  44.         sp:pchar;
  45.         i:longint;
  46.         bool:boolean;
  47.  
  48.    begin
  49.    randomize;
  50.     Setlength(a,1000);
  51.     sp:=@a[1];
  52.     for i:=0 to 1000-1 do sp[i]:=char(random(128));
  53.      writeln(a);
  54.      writeln;
  55.      ec:=base64encode(a);
  56.      writeln(ec);
  57.      writeln;
  58.      dc:= base64decode(ec);
  59.      writeln(dc);
  60.      writeln;
  61.      bool:= (dc=a);
  62.      writeln(bool);
  63.  
  64.      readln;
  65.  
  66.    end.
  67.  
  68.  

Fantablup

  • Full Member
  • ***
  • Posts: 140
Re: Encrypt in Node.js Decrypt in FPC
« Reply #3 on: August 03, 2021, 08:21:24 pm »
Why not just encrypt the server connection?
Use SSL, and you don't need to manually encrypt.

I also used to do this manually in code, but after learning more on the server side, i begun to do it this way.

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: Encrypt in Node.js Decrypt in FPC
« Reply #4 on: August 04, 2021, 07:59:02 am »
Lainz,

You are probably are using SSL (eg HTTPS) for your site, so everything is already encrypted. Now you can send the JSON without any extra work.
If you are not using SSL (and you probably should) Base64 or Hex are the way to go. Encode it in JS and decode it in the FPC as skalogryz suggested.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Encrypt in Node.js Decrypt in FPC
« Reply #5 on: August 04, 2021, 09:20:13 am »
You're all confussing the issue a little. Both proposed solutions, Base64 or Hex serve just to (de)code to/from pure ASCII so that no mangling can happen if some host on the way doesn't like 8 bits strings.

To encrypt/decrypt using a key one has to use an appropiate symmetric cipher ranging from the ages old Caesar cipher (the simple unit shifting cipher) to modern ones like RSA and alike. For this the best way is to use any of the encyption libs out there, e.g. DCPCrypt. Then one can use Base64/Hex to ensure the ciphertext arrives untouched to the final end-point.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: Encrypt in Node.js Decrypt in FPC
« Reply #6 on: August 04, 2021, 10:39:26 am »

Sorry yes, I am mixing encryption with encoding.
I'll leave my encode there if it is of any use.

 

TinyPortal © 2005-2018