Livecode storing encryption with extra lines

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
cusingerBUSCw5N
Posts: 339
Joined: Wed Jul 11, 2012 9:24 pm

Livecode storing encryption with extra lines

Post by cusingerBUSCw5N » Thu Jul 25, 2019 8:10 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7421
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Livecode storing encryption with extra lines

Post by FourthWorld » Thu Jul 25, 2019 8:25 pm

Try writing in binary mode:

Code: Select all

open file specialFolderPath("documents") & "/adminemail.txt" for binary write
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Klaus
Posts: 11560
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Livecode storing encryption with extra lines

Post by Klaus » Thu Jul 25, 2019 8:26 pm

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...
...

cusingerBUSCw5N
Posts: 339
Joined: Wed Jul 11, 2012 9:24 pm

Re: Livecode storing encryption with extra lines

Post by cusingerBUSCw5N » Mon Aug 05, 2019 9:51 pm

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?
Attachments
adminemailpicture2.png
adminemailpicture2.png (2.47 KiB) Viewed 786 times
adminemailpicture.png
adminemailpicture.png (2.88 KiB) Viewed 786 times

Klaus
Posts: 11560
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Livecode storing encryption with extra lines

Post by Klaus » Fri Aug 09, 2019 3:41 pm

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
...

Klaus
Posts: 11560
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Livecode storing encryption with extra lines

Post by Klaus » Fri Aug 09, 2019 3:58 pm

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!

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”