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
Poll: Watches and Display...
by
440bx
[
Today
at 07:13:51 pm]
Lazarus for Windows on aa...
by
msintle
[
Today
at 06:42:18 pm]
InstallAware Using Lazaru...
by
marcov
[
Today
at 04:46:46 pm]
Who is Indy mattias?
by
paweld
[
Today
at 04:17:53 pm]
[solved] how to get class...
by
Joanna
[
Today
at 03:53:05 pm]
I just released a commerc...
by
BrassGear
[
Today
at 03:17:28 pm]
Does anyone know why thes...
by
Laksen
[
Today
at 03:04:14 pm]
Database standards OR Am ...
by
gidesa
[
Today
at 02:37:56 pm]
How to: create DLL file f...
by
TRon
[
Today
at 02:26:53 pm]
A fairly simple sound sol...
by
paweld
[
Today
at 01:46:11 pm]
Demoscene The Champs Crac...
by
KodeZwerg
[
Today
at 01:45:18 pm]
Access violation when ope...
by
Чебурашка
[
Today
at 12:27:34 pm]
Step-into the field sette...
by
Martin_fr
[
Today
at 11:31:48 am]
Access violation when re-...
by
Chris Osborne
[
Today
at 11:31:25 am]
AI, NLP and CAI: Text Gen...
by
Dzandaa
[
Today
at 11:03:26 am]
dwindows for Android
by
PierceNg
[
Today
at 10:54:44 am]
create system unit from s...
by
Laksen
[
Today
at 10:53:57 am]
[Solved] Find child contr...
by
Joanna
[
Today
at 09:53:43 am]
FpDebug unexpected Assemb...
by
Marc
[
Today
at 08:46:38 am]
Offical launch of the 1 B...
by
Handoko
[
Today
at 07:59:07 am]
Big Numbers Math
by
iLya2IK
[
Today
at 07:13:20 am]
BUG REPORT: Unable to gen...
by
dbannon
[
Today
at 06:29:49 am]
what to do if my target M...
by
Laksen
[
Today
at 12:49:12 am]
For-in loop over constant...
by
ASerge
[
Today
at 12:36:09 am]
Converting a string/index...
by
TRon
[April 18, 2024, 11:56:37 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Encrypt in Node.js Decrypt in FPC (Read 3218 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