Preventing unwanted Line Breaks in a txt file

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
KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Preventing unwanted Line Breaks in a txt file

Post by KimD » Mon Feb 22, 2016 4:50 am

I'm using a pair of Encrypt & Decrypt functions that I found here - http://forums.livecode.com/viewtopic.php?f=11&t=22200

They are working fine; except that I have a problem when
- I encrypt a long (approx 100 char) string
- and write the encrypted version to a .txt file
- and then try to use the Decrypt function to read it.

The problem is occurring because the encrypted string is long (also approx 100 char with no spaces), and something is inserting a line break into it after approximately 72 characters. I know this because if I take the two encrypted strings that I'm being left with (on two separate lines), and join them together, then they Decrypt fine.

I've found lots of info on the forums on how to add line breaks, but how do I prevent unintended line breaks?

To initialize my file I'm using
- Put "file:" & ggDocumentsFolder & "/Puzzles.txt" into ggPuzzlesFile
- Put "MySetupParameters" into line 1 of URL (ggPuzzlesFile)

To write my encrypted strings into the file I'm using
- Put EncryptIt(StringIWantEncrypted) into line xyz of URL (ggPuzzlesFile)

I experimented with changing this to a binfile, but wasn't familiar enough with binfiles to find a solution.

LiveCode 7.1.0; developing on Windows 7; trying to build an app for Android.

Thanks in advance (and sorry for bothering everyone again)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Preventing unwanted Line Breaks in a txt file

Post by dunbarx » Mon Feb 22, 2016 5:24 am

Hi.

I am now going to be of little help.

I tried encrypting a random 100-char string:

Code: Select all

on mouseUp
   repeat 100
      put any char of "asddkjfkdjfjgekg" after temp
   end repeat
    put "1234567812345678" into tKeyHex
   put "8765432187654321" into tIVHex
   encrypt temp using "aes-128-cbc" with key tKeyHex and IV tIVHex at 128 bit
end mouseUp
I get three lines in "it".

BUT, if I do this:

Code: Select all

on mouseUp
   repeat 100
      put any char of "asddkjfkdjfjgekg" after temp
   end repeat
    put "1234567812345678" into tKey
   put "8765432187654321" into tIV
   encrypt temp using "aes-128-cbc" with key tKey and IV tIV at 128 bit
end mouseUp
I get one line. I do not know why the actual names of the passorKey and IV matter.

See? Of little help. But at least you are not crazy. You will have to wait for someone with real experience with encryption.

Craig Newman

KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Re: Preventing unwanted Line Breaks in a txt file

Post by KimD » Mon Feb 22, 2016 6:05 am

Thanks Craig

I've been experimenting some more. I couldn't reproduce your tKeyHex vs tKey result, but I did narrow things down a little.
- If I encrypt a string of length 47 ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstu"), then the resulting encrypted string is length 64, and everything is good.
- If I encrypt a string of length 48, then the resulting encrypted string is length 72, and I start to have problems when I try to read this from a txt file.

Note that I can still successfully send my encrypted long (over 48 characters) strings to the php programme that I've written to handle them, and get back sensible results. Its just ((string to be encrypted > 47 characters) and (string to be encrypted is being written to a txt file)) that is the problem.

Decryption ehhh.

Regards
Attachments
TestEncryptDecrypt.zip
(1.39 KiB) Downloaded 178 times

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

Re: Preventing unwanted Line Breaks in a txt file

Post by FourthWorld » Mon Feb 22, 2016 7:53 am

The ciphertext is binary, so use "binfile:" instead of "file:".
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Preventing unwanted Line Breaks in a txt file

Post by Klaus » Mon Feb 22, 2016 5:34 pm

Hi Kim,

what Richard says!

but since encrypted binary text can have more than one line, you cannot :
...
put EncryptIt(StringIWantEncrypted) into line xyz of URL (ggPuzzlesFile)
...
Use different files for each encrypted string or FORCE the binary stuff into one line like this,
then you can also use FILE instead of BINFILE::
...
put EncryptIt(StringIWantEncrypted) into tEncrypted
## Make ASCII string from binary:
put base64encode(tEncrypted) into tBase64
## Force to ONE line:
put urlencode(tBase64) into line xyz of URL (ggPuzzlesFile)
...
## Or in one "hardtogetatoneglance" step:
put urlencode(base64encode(EncryptIt(StringIWantEncrypted))) into line xyz of URL (ggPuzzlesFile)
...
To decrypt work vice versa of course :D


Best

Klaus

KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Re: Preventing unwanted Line Breaks in a txt file

Post by KimD » Mon Feb 22, 2016 8:58 pm

Thanks all. With your input - I seem to have it working now.

Short version:
- for strings with an unencrypted length 47 char and under - the EncryptIt(myunencryptedstring) and DecryptIt(myencryptedstring) functions described earlier work fine.
- but for strings with an unencrypted length 48 char and over - I needed to use urlencode(EncryptIt(myunencryptedstring)) and DecryptIt(urldecode(myencryptedstring)).
- urlencode & urldecode work fine with the short strings, its just that they seem to be optional for short strings

I can now store short encrypted strings, and any length urlencoded encrypted strings, in a multiple line .txt file

Working stack demonstrating the basics is attached.

Regards
Attachments
TestEncryptDecrypt.zip
(1.53 KiB) Downloaded 170 times

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”