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
Can function be used for ...
by
jamie
[
Today
at 10:53:10 pm]
(Solved) TIndexedAVLTree ...
by
lainz
[
Today
at 10:49:03 pm]
Parameter passing odditie...
by
Nitorami
[
Today
at 10:48:22 pm]
Demo Scene Picture sinwav...
by
KodeZwerg
[
Today
at 10:26:21 pm]
fkInternalCalc
by
zraja
[
Today
at 09:31:07 pm]
A fairly simple sound sol...
by
KodeZwerg
[
Today
at 09:23:37 pm]
add new targets to make
by
marcov
[
Today
at 08:38:42 pm]
how to tweek fpmake to co...
by
marcov
[
Today
at 08:29:16 pm]
Quirky windows
by
ASerge
[
Today
at 07:07:16 pm]
[Request] Demo Scene Sub ...
by
Gigatron
[
Today
at 06:29:39 pm]
Unlocking Files
by
KodeZwerg
[
Today
at 06:03:08 pm]
[SOLVED] trunk bug for mi...
by
Key-Real
[
Today
at 05:56:32 pm]
Demo Scene Bitmap Font Sc...
by
Gigatron
[
Today
at 05:56:27 pm]
Compile/Convert Delphi pr...
by
Martin_fr
[
Today
at 05:43:11 pm]
Generics - correct syntax
by
Thaddy
[
Today
at 05:40:59 pm]
Lazarus for Windows on aa...
by
msintle
[
Today
at 05:32:42 pm]
InstallAware Using Lazaru...
by
msintle
[
Today
at 05:26:36 pm]
Xcode Command Line Tools ...
by
Joseph
[
Today
at 05:16:39 pm]
Offical launch of the 1 B...
by
Hartmut
[
Today
at 04:49:29 pm]
what to do if my target M...
by
nickysn
[
Today
at 04:16:02 pm]
compiler error in unit
by
paule32
[
Today
at 03:49:50 pm]
Multithreading - synchron...
by
mika
[
Today
at 01:48:51 pm]
May be useful to somebody
by
KodeZwerg
[
Today
at 12:38:22 pm]
match automatic highlight...
by
Чебурашка
[
Today
at 11:35:14 am]
Tchart with dual numbers ...
by
wp
[
Today
at 10:48:18 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Encrypt in Node.js Decrypt in FPC (Read 3227 times)
lainz
Hero Member
Posts: 4473
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