Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
General
»
Encrypt in Node.js Decrypt in FPC
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
IRC channel
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
Assign (textfile) not com...
by
Bart
[
Today
at 11:35:37 am]
Warning: Source OS Redefi...
by
Laksen
[
Today
at 11:32:46 am]
[SOLVED] how to see what ...
by
Laksen
[
Today
at 11:30:25 am]
Slow copying of small str...
by
alpine
[
Today
at 11:25:10 am]
assembler name is longer ...
by
Key-Real
[
Today
at 11:15:20 am]
Question for people who h...
by
BrassGear
[
Today
at 11:12:50 am]
I just released a commerc...
by
BrassGear
[
Today
at 11:05:12 am]
How to: create DLL file f...
by
KodeZwerg
[
Today
at 10:53:20 am]
Text orientation in TMemo...
by
wp
[
Today
at 10:27:54 am]
InstallAware Using Lazaru...
by
marcov
[
Today
at 10:05:26 am]
Linux Workspaces -- StayO...
by
AmatCoder
[
Today
at 08:21:27 am]
Demoscene The Champs Crac...
by
Lulu
[
Today
at 08:15:12 am]
Azure sql database will n...
by
paweld
[
Today
at 07:24:47 am]
Find child controls by na...
by
Handoko
[
Today
at 04:17:05 am]
location of non visual co...
by
VisualLab
[April 17, 2024, 11:58:55 pm]
For-in loop over constant...
by
Joanna
[April 17, 2024, 11:57:15 pm]
Lazarus editor feature re...
by
440bx
[April 17, 2024, 11:49:08 pm]
How would you define a fi...
by
wp
[April 17, 2024, 11:04:00 pm]
Directx9 with mylogo insi...
by
KodeZwerg
[April 17, 2024, 11:03:54 pm]
Lazarus for Windows on aa...
by
Wallaby
[April 17, 2024, 10:25:46 pm]
[SOLVED] TDbf table, Leve...
by
1HuntnMan
[April 17, 2024, 10:03:16 pm]
Doomed name
by
Martin_fr
[April 17, 2024, 09:09:52 pm]
lazarus-project_3.2.0-0_a...
by
midigark
[April 17, 2024, 06:09:40 pm]
TBGRABitmap.TextSize retu...
by
circular
[April 17, 2024, 05:24:04 pm]
BUG REPORT: Unable to gen...
by
gasensor
[April 17, 2024, 05:10:20 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Encrypt in Node.js Decrypt in FPC (Read 3205 times)
lainz
Hero Member
Posts: 4468
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.
Logged
https://lainz.github.io/
skalogryz
Global Moderator
Hero Member
Posts: 2770
Re: Encrypt in Node.js Decrypt in FPC
«
Reply #1 on:
August 03, 2021, 06:52:24 pm »
base64
node.js
FPC
Logged
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]
[+]
[-]
program
encode64
;
var
a
,
b
,
L
,
filename
:
ansistring
;
Function
CryptBinaryToStringA
(
p
:
pchar
;
i
:
integer
;
j
:
integer
;
p2
:
pchar
;
p3
:
pointer
)
:
integer
;
cdecl
external
'Crypt32.dll'
;
Function
CryptStringToBinaryA
(
p
:
pchar
;
l1
:
integer
;
L5
:
integer
;
b
:
pchar
;
L2
:
pointer
;
L3
:
pointer
;
l4
:
pointer
)
:
integer
;
cdecl
external
'Crypt32.dll'
;
Function
Base64Decode
(
ss
:
ansistring
)
:
ansistring
;
var
lngth
,
n
:
longint
;
answer
:
ansistring
;
b
:
array
of
byte
;
panswer
:
pchar
;
begin
lngth
:
=
length
(
ss
)
;
SetLength
(
b
,
lngth
)
;
CryptStringToBinaryA
(
@
ss
[
1
]
,
Lngth
,
1
,
@
b
[
0
]
,
@
Lngth
,
nil
,
nil
)
;
setlength
(
answer
,
lngth
)
;
panswer
:
=
@
answer
[
1
]
;
for
n
:
=
0
to
lngth
do
begin
ord
(
panswer
[
n
]
)
:
=
(
b
[
n
]
)
;
end
;
exit
(
answer
)
;
End
;
Function
Base64Encode
(
p
:
ansistring
)
:
ansistring
;
var
L
:
longint
;
s
:
array
of
byte
;
begin
L
:
=
length
(
p
)
*
2
;
SetLength
(
s
,
L
)
;
CryptBinaryToStringA
(
@
p
[
1
]
,
Length
(
p
)
,
1
,
@
s
[
0
]
,
@
L
)
;
exit
(
pchar
(
@
s
[
0
]
)
)
;
End
;
var
s
,
ec
,
dc
:
ansistring
;
sp
:
pchar
;
i
:
longint
;
bool
:
boolean
;
begin
randomize
;
Setlength
(
a
,
1000
)
;
sp
:
=
@
a
[
1
]
;
for
i
:
=
0
to
1000
-
1
do
sp
[
i
]
:
=
char
(
random
(
128
)
)
;
writeln
(
a
)
;
writeln
;
ec
:
=
base64encode
(
a
)
;
writeln
(
ec
)
;
writeln
;
dc
:
=
base64decode
(
ec
)
;
writeln
(
dc
)
;
writeln
;
bool
:
=
(
dc
=
a
)
;
writeln
(
bool
)
;
readln
;
end
.
Logged
Fantablup
Full Member
Posts: 171
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.
Logged
Okoba
Hero Member
Posts: 533
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.
Logged
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.
Logged
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
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.
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
General
»
Encrypt in Node.js Decrypt in FPC
TinyPortal
© 2005-2018