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
Project fails on run but ...
by
Thaddy
[
Today
at 02:06:57 pm]
Controlling the behavior ...
by
1HuntnMan
[
Today
at 02:06:52 pm]
a Unit problem?
by
Thaddy
[
Today
at 01:56:12 pm]
Complex package installat...
by
parcel
[
Today
at 01:48:04 pm]
Understanding and adjusti...
by
ArchChem
[
Today
at 12:58:12 pm]
Any upgrades to fpWeb pag...
by
marcov
[
Today
at 12:50:25 pm]
Help needed with Cairo / ...
by
Roland57
[
Today
at 12:25:16 pm]
JEDI error but unit compi...
by
Roland57
[
Today
at 11:50:11 am]
Using KeyPress event, how...
by
Bart
[
Today
at 11:32:53 am]
Configure JCF messages
by
DomingoGP
[
Today
at 11:26:55 am]
IDE instability on Raspbe...
by
rvk
[
Today
at 10:37:14 am]
Boleeman's projects are p...
by
AlexTP
[
Today
at 10:18:04 am]
Your best UI design - con...
by
circular
[
Today
at 09:36:11 am]
ZeosDBO and querys using ...
by
Zvoni
[
Today
at 09:27:17 am]
STARS with line border st...
by
Boleeman
[
Today
at 09:13:29 am]
Zeos components. Had to c...
by
dbannon
[
Today
at 07:26:11 am]
A button require administ...
by
Fibonacci
[
Today
at 05:57:21 am]
How to detect one externa...
by
d2010
[
Today
at 12:24:32 am]
Center Lost Window featur...
by
Tony Stone
[
Today
at 12:06:16 am]
A "leisure" question on W...
by
EventHorizon
[March 27, 2024, 10:18:38 pm]
need advice about the fol...
by
jamie
[March 27, 2024, 09:56:39 pm]
[SOLVED] Completion box s...
by
Martin_fr
[March 27, 2024, 08:44:09 pm]
Dialog ShowModal and an i...
by
d7_2_laz
[March 27, 2024, 08:25:18 pm]
Offical launch of the 1 B...
by
Gustavo 'Gus' Carreno
[March 27, 2024, 07:41:48 pm]
Problem setting length of...
by
Remy Lebeau
[March 27, 2024, 05:41:10 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Encrypt in Node.js Decrypt in FPC (Read 3183 times)
lainz
Hero Member
Posts: 4460
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: 169
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: 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.
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