Page 1 of 1

[SOLVED] Encrypt / Decrypt

Posted: Wed Oct 02, 2019 9:34 pm
by redfield
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 :?:

Re: Encrypt / Decrypt

Posted: Wed Oct 02, 2019 9:43 pm
by FourthWorld
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.

Re: Encrypt / Decrypt

Posted: Wed Oct 02, 2019 9:48 pm
by Klaus
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

Re: Encrypt / Decrypt

Posted: Wed Oct 02, 2019 9:55 pm
by richmond62
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)

Re: Encrypt / Decrypt

Posted: Thu Oct 03, 2019 4:50 pm
by jabedbd
Thanks a lot. it's working fine now :)

Re: Encrypt / Decrypt

Posted: Thu Oct 03, 2019 8:35 pm
by FourthWorld
jabedbd wrote:
Thu Oct 03, 2019 4:50 pm
Thanks a lot. it's working fine now :)
What changed?

Re: Encrypt / Decrypt

Posted: Thu Oct 03, 2019 9:04 pm
by Klaus
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

Re: [SOLVED] Encrypt / Decrypt

Posted: Sun Oct 13, 2019 10:35 am
by redfield
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 :? .