Preventing unwanted Line Breaks in a txt file
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Preventing unwanted Line Breaks in a txt file
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)
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)
-
- 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
Hi.
I am now going to be of little help.
I tried encrypting a random 100-char string:
I get three lines in "it".
BUT, if I do this:
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
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
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
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
Re: Preventing unwanted Line Breaks in a txt file
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
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
-
- 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
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Preventing unwanted Line Breaks in a txt file
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
Best
Klaus
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
Best
Klaus
Re: Preventing unwanted Line Breaks in a txt file
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
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