[SOLVED] Encrypt / Decrypt

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

[SOLVED] Encrypt / Decrypt

Post by redfield » Wed Oct 02, 2019 9:34 pm

Hey guys,
I would like to store some passwords in a database and for that encrypt them first. I think it would be better (-> safer) to store only their hashes, but the passwords shall later be used, without having to ask the user for them. So I think I need to go with encrypt/decrypt. Therefore I am starting to play with the related commands, totally failing though in the case of decryption:

Code: Select all

on mouseDown
   put fld "fEncrypt" into varEncrypt
   decrypt varEncrypt using "aes-256" with password "abc"
   put it into fld "fDecrypt"
end mouseDown
The encryption seems to work, at least it gives me something like 'Salted__@™AkKÐW=«8þæãÕ4ºf', which looks encrypted enough to me :shock: . But the above code for decrypting does nothing - 'it' seems to be empty. But why :?:
Last edited by redfield on Sun Oct 13, 2019 10:26 am, edited 1 time in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Encrypt / Decrypt

Post by FourthWorld » Wed Oct 02, 2019 9:43 pm

Does "the result" tell you anything?

Also, storing binary data in fields can be dicey. Better to use a variable, or if it needs to be bound to an object use a custom property.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Klaus
Posts: 14196
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Encrypt / Decrypt

Post by Klaus » Wed Oct 02, 2019 9:48 pm

Hi redfield,

ENCRYPT returns BINARY data in IT, so putting this into a (text) field may result in loss of data!
Maybe that is the problem.

Put IT into a custom property and try again:

Code: Select all

...
## Enrypt
put fld "data to be encrypted" into tData
encrypt tData ...
set the cEncryptedData of this stack to IT
...

Code: Select all

...
## Decrypt
put the cEncryptedData of this stack into tData
DEcrypt tData ...
put IT into fld "decrypted data"
...
Best

Klaus

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10099
Joined: Fri Feb 19, 2010 10:17 am

Re: Encrypt / Decrypt

Post by richmond62 » Wed Oct 02, 2019 9:55 pm

Spy.jpg
-
Works alright:

Button "Encrypt"

Code: Select all

on mouseUp
   put empty into fld "fPWen"
   put fld "fPW" into PW
   encrypt PW using "aes-256-cbc" with password "fromage frais"
   put it into fld "fPWen"
end mouseUp
Button "Decrypt"

Code: Select all

on mouseUp
   put empty into fld "fPWdc"
   put fld "fPWen" into DEKR
   decrypt DEKR using "aes-256-cbc" with password "fromage frais"
   put it into fld "fPWdc"
end mouseUp
Everything in fields. 8)
Attachments
SPY.livecode.zip
Here's the stack.
(130.38 KiB) Downloaded 263 times

jabedbd
Posts: 8
Joined: Wed Sep 04, 2019 12:40 pm

Re: Encrypt / Decrypt

Post by jabedbd » Thu Oct 03, 2019 4:50 pm

Thanks a lot. it's working fine now :)

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Encrypt / Decrypt

Post by FourthWorld » Thu Oct 03, 2019 8:35 pm

jabedbd wrote:
Thu Oct 03, 2019 4:50 pm
Thanks a lot. it's working fine now :)
What changed?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Klaus
Posts: 14196
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Encrypt / Decrypt

Post by Klaus » Thu Oct 03, 2019 9:04 pm

FourthWorld wrote:
Thu Oct 03, 2019 8:35 pm
jabedbd wrote:
Thu Oct 03, 2019 4:50 pm
Thanks a lot. it's working fine now :)
What changed?
Yes, please, tell us what solved the problem!
Maybe Richmond's empirical "field" observations? :D

redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Re: [SOLVED] Encrypt / Decrypt

Post by redfield » Sun Oct 13, 2019 10:35 am

Finally I get to provide a feedback. It's working now, but unfortunately I don't exactly know what was wrong.
I started with "the result", it was empty, so then I tried the custom property solution, but it still didn't work. Then I downloaded the SPY.livecode stack and it worked. The code seemed identical to mine, nevertheless I copied it and replaced the variable names with my variable names. It worked. I can only assume that I had a typo or so in my original code :? .

Post Reply