Page 1 of 1

Livecode storing encryption with extra lines

Posted: Thu Jul 25, 2019 8:10 pm
by cusingerBUSCw5N
I have used encryption to store information in a file on a mobile device.

1) a person enters their email in a field - it is put into temail and then encrypted

The code is


encrypt temail using "aes192" with key "zSJmf1pHKa42+mdZfGEM+A=="
put it into myText
answer myText
When I do answer myText, it displays 2 lines of encrypted text

2) If I then decrypt it to see if it will give me my email address back. It does. Everything is good.

3) Then, I store the encrypted text as follows:
open file specialFolderPath("documents") & "/adminemail.txt" for write
write myText to file specialFolderPath("documents") & "/adminemail.txt"
close file specialFolderPath("documents") & "/adminemail.txt"
When open or read the file, it has 3 lines of encrypted text and fails the decryption:


put url ("binfile:" & specialFolderPath("documents") & "/adminemail.txt") into twhat
answer twhat
decrypt twhat using "aes192" with key "zSJmf1pHKa42+mdZfGEM+A=="
put it into gemail
answer gemail
This makes encryption/decryption completely worthless. Is there something I am doing wrong? Thanks

Re: Livecode storing encryption with extra lines

Posted: Thu Jul 25, 2019 8:25 pm
by FourthWorld
Try writing in binary mode:

Code: Select all

open file specialFolderPath("documents") & "/adminemail.txt" for binary write

Re: Livecode storing encryption with extra lines

Posted: Thu Jul 25, 2019 8:26 pm
by Klaus
I think ENCRYPT returns binary data, so you need to:

Code: Select all

...
encrypt temail using "aes192" with key "zSJmf1pHKa42+mdZfGEM+A==" 
put it into myText
put myText into url("binfile:" & specialFolderPath("documents") & "/adminemail.txt")
...
And of course you need to also read it back as binary:

Code: Select all

...
put url("binfile:" & specialFolderPath("documents") & "/adminemail.txt") into temail
decrypt temail...
...

Re: Livecode storing encryption with extra lines

Posted: Mon Aug 05, 2019 9:51 pm
by cusingerBUSCw5N
Hi. I went back to check my work and there is still a problem.

When I encrypt an email, it creates one line of "stuff" (see attachment adminemailpicture.png)

Code: Select all

open file specialFolderPath("documents") & "/adminemail.txt" for write 
         encrypt temail using "aes192" with key "zSJmf1pHKa42+mdZfGEM+A==" 
         put it into myText
         put myText into url("binfile:" & specialFolderPath("documents") & "/adminemail.txt")
         close file specialFolderPath("documents") & "/adminemail.txt"   

When I open the file in Livecode, it shows it as 2 lines and can't decrypt it (see attachment adminemailpicture2.png)

Code: Select all

 put url ("binfile:" & specialFolderPath("documents") & "/adminemail.txt") into twhat
answer twhat
replace RETURN with "" in twhat
decrypt twhat using "aes192" with key "zSJmf1pHKa42+mdZfGEM+A=="
put it into gemail


I'm thinking that the problem is with encrypting the @ sign in the email - but I haven't tested it elsewhere.


I used binfile for adding to the txt file and for reading it.


Any ideas?

Re: Livecode storing encryption with extra lines

Posted: Fri Aug 09, 2019 3:41 pm
by Klaus
When using the URL syntax, there is no need to OPEN and CLOSE the FILE, since that is a ONE-Liner!

Code: Select all

...
## open file specialFolderPath("documents") & "/adminemail.txt" for write 
## Correct: ... for BINARY WRITE

encrypt temail using "aes192" with key "zSJmf1pHKa42+mdZfGEM+A==" 
put it into myText
put myText into url("binfile:" & specialFolderPath("documents") & "/adminemail.txt")
## close file specialFolderPath("documents") & "/adminemail.txt"  
... 
So you say you cannot DECRYPT the resulting file "adminemail.txt" in LC anymore?
Just made a quick test with your settings and everything works as exspected in LC 9.5!
Encrypt:

Code: Select all

...
put fld 1 into myData
encrypt mydata using "aes192" with key "zSJmf1pHKa42+mdZfGEM+A=="
put it into myText
put myText into url("binfile:" & specialFolderPath("documents") & "/adminemail.txt")
...
Decrypt:

Code: Select all

...
put url("binfile:" & specialFolderPath("documents") & "/adminemail.txt") into myData
decrypt myData using "aes192" with key "zSJmf1pHKa42+mdZfGEM+A=="
put it into myText
put myText into fld 2
...

Re: Livecode storing encryption with extra lines

Posted: Fri Aug 09, 2019 3:58 pm
by Klaus
Good advice:
NEVER judge from the LOOK of your BINARY data in a text editor! 8)

The string inside of the BINARY file looks different when viewed in TextEdit and TextWrangler on my Mac!