Encrypt error

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
Krynn
Posts: 7
Joined: Thu Jul 05, 2012 1:11 am

Encrypt error

Post by Krynn » Wed Mar 13, 2019 11:57 pm

Hello,

I have a problem I am using the Encrypt and Decrypt command.

But instead of perfectly deciphering the text.
I have random characters randomly.

At first glance it comes from the fact that I write in a file. When I put the text encrypt in a field. I do not have this problem.

I opened the text file. I think it's due to the format of the text file where line breaks are not well converted.

Do you have an idea?

thanks
Krynn
Attachments
2019-03-13_23h50_05.png
bug return
2019-03-11_21h34_11.png
code encrypt
2019-03-11_21h33_40.png
bad decryption
2019-03-11_21h33_40.png (12.14 KiB) Viewed 150 times

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

Re: Encrypt error

Post by Klaus » Thu Mar 14, 2019 12:07 am

Bonsoir Krynn,

quick guess:
Do you use BINFILE when writing and reading your encrypted stuff to/from file?
If not, do so and try again.

Encryptring text results in BINARY data!


Best

Klaus

Krynn
Posts: 7
Joined: Thu Jul 05, 2012 1:11 am

Re: Encrypt error

Post by Krynn » Fri Mar 15, 2019 10:37 am

no, I do not use binfile
I'm doing the test tonight

Thank you

AxWald
Posts: 340
Joined: Thu Mar 06, 2014 2:57 pm

Re: Encrypt error

Post by AxWald » Fri Mar 15, 2019 11:26 am

Hi,

binary data (as in the result from 'encrypt') is a pest to handle/ display/ read. Converted to hex the data become much more amiable - check this:

Code: Select all

function B2H pString  --  from libHash-Hmac V 2.3, http://marksmith.on-rev.com/revstuff/
  repeat for each byte c in pString
    get bytetonum(c)
    put baseconvert(it,10,16) into tTemp
    if it < 16 then put "0" before tTemp
    put tTemp after tHex
  end repeat
  return tolower(tHex)
end B2H

function H2B pString  --  from libHash-Hmac V 2.3, http://marksmith.on-rev.com/revstuff/
   repeat with n = 1 to length(pString) - 1 step 2
      put numtobyte(baseconvert(byte n to n + 1 of pString, 16, 10)) after tBin
   end repeat
   return tBin
end H2B
The functions convert ugly binary data:

Code: Select all

Salted__40046503…,^D7ˆMWtCÄ…°‰§î†_žÌ˜Mà‰½_Áë÷
to beautiful hex data (B2H):

Code: Select all

53616c7465645f5f3430303436353033852c5e4437884d7f577443c485b089a7ee865f9ecc980f4de08905bd5fc1ebf7
or back (H2B). You see the difference?

For the encryption/ decryption you may use these functions:

Code: Select all

function encStrg theStrg, thePass
   put char -1 of the millisecs into MyChar
   set the randomSeed to MyChar & MyChar & MyChar
   put random(900000000) + 99999999 into MySalt
   delete char 1 of MySalt
   encrypt theStrg using "aes-256-cbc" with password thePass and salt MySalt
   get B2H(it)
   return it
end encStrg

function decStrg theStrg, thePass
   put H2B(theStrg) into MyVar
   decrypt MyVar using "aes-256-cbc" with password thePass
   return it
end decStrg
As a bonus they come with a salt, means the encrypted data will look different each time you'll encrypt the same base data ;-)

Another bonus: There's a citation link in the first examples for Mark Smith's web site. It's strongly recommended to have a look at it!

Have fun!
Livecode programming until the cat hits the fan ...

Krynn
Posts: 7
Joined: Thu Jul 05, 2012 1:11 am

Re: Encrypt error

Post by Krynn » Mon Mar 18, 2019 8:51 am

Hello AxWald,

Your appoche seems very relevant in order to get rid of all the special characters.
So I followed your advice and adapt my code.

And strength is found that it works great. I adopted it.

I have a question, I am not a specialist in the code, and I did not really understand the interest.

Code: Select all

put char-of-the-millisecs into MyChar
set the randomSeed to MyChar & MyChar & MyChar
put random (900000000) + 99999999 into MySalt
delete char 1 of MySalt

Hello Klaus,
I did not test your solution. But I will do it occasionally, I think it will be useful for other functions.


thank you

Post Reply

Return to “Talking LiveCode”