encrypted by php does not decrypt in lc

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
makeshyft
Posts: 220
Joined: Mon Apr 15, 2013 4:41 am
Contact:

encrypted by php does not decrypt in lc

Post by makeshyft » Tue Nov 01, 2016 10:35 pm

Hi everyone,

I have a file that is generated by a PHP server.

Before I save the php variable I encrypt it using aes-256-cbc with a 256 key password,256 bit sat, and a init vector

I use OPENSSL_RAW_DATA to create binary output and not base64

I decrypt it to test, and display it ... so i know it works

then i save it... this is my code

$myfile = fopen($filename, "w") or die("Unable to open file!");
fwrite($myfile, $encrypted_variable);
fclose($myfile);


I put the same cipher, key,salt, and iv into lc's decrypt function...and I get an empty result, no errror.

Decrypt LoadedFile using "aes-256-cbc" with password MyPass and salt MySalt and iv MyIV

How can I dignose this problem?

Thanks in advance for any help.
Founder & Developer @ MakeShyft R.D.A - https://www.makeshyft.com
Build Software with AppStarterStack for Livecode - https://www.AppStarterStack.com
Save Time with The Time Saver's Toolbox - https://www.TimeSaversToolbox.com

makeshyft
Posts: 220
Joined: Mon Apr 15, 2013 4:41 am
Contact:

Re: encrypted by php does not decrypt in lc

Post by makeshyft » Tue Nov 01, 2016 11:57 pm

I read in the dictionary to check the result and not it for the error....and the error was invalid cipher name...hmmmmm..maybe syntax is different from php and lc
then the error became "invalid end of block"
so i tried aes256 with no cbc as the new cipher

and now the error is invalid decrypt :( ... encrypt decrypt combo still works on PHP page

So now i'm even more stuck than before..... I am certain that my keys and salts and iv are correct..i've triple checked.
Founder & Developer @ MakeShyft R.D.A - https://www.makeshyft.com
Build Software with AppStarterStack for Livecode - https://www.AppStarterStack.com
Save Time with The Time Saver's Toolbox - https://www.TimeSaversToolbox.com

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: encrypted by php does not decrypt in lc

Post by MaxV » Wed Nov 02, 2016 11:55 am

May you show file examples? Both original files and encrypted one. Sometime some ecrypting libraries add some file informations at the beggining of the file and other not. This usually generate errors.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

makeshyft
Posts: 220
Joined: Mon Apr 15, 2013 4:41 am
Contact:

Re: encrypted by php does not decrypt in lc

Post by makeshyft » Wed Nov 02, 2016 6:06 pm

Hi Max, thanks for replying.

I've tried using the compress function to see if its the save file function that is not writing the file correctly, and it appears that this is the case...since the gzcompress function from php is also generating error when i try to decompress in lc. so i think i will work on making sure php is saving the file without modifying the data in any way. i ill returnj to this thread with a result either way. thank you.
Founder & Developer @ MakeShyft R.D.A - https://www.makeshyft.com
Build Software with AppStarterStack for Livecode - https://www.AppStarterStack.com
Save Time with The Time Saver's Toolbox - https://www.TimeSaversToolbox.com

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: encrypted by php does not decrypt in lc

Post by capellan » Sun Nov 13, 2016 2:59 am

Hi Tom,

Many, many years ago, I posted this information in the mail list
about the LiveCode compress function. I have reposted this here
with some additional explanations.
Please check the result of Php compress using a hex editor and
verify if the structure is identical to LiveCode compress:
-----------------------------------------------

The (LiveCode) compress function returns a gzip compressed string of
binary data structured in the following format:

1) A 10-byte header, containing:

A magic number, composed by two bytes:
1F 8B (Hexadecimal) or 31 139 (ASCII Numbers of both characters)

A version number (always 08), that specify compression compression method used in the file.
In this case, LiveCode write the byte 08.
This single character correspond to DEFLATE compression.

Additional information like timestamp, optional extra headers, (as the original file name) are not included and in their place, Livecode writes six null bytes: 00 00 00 00 00 00

Last character of 10 byte header correspond to the type of file system on which compression took place. LiveCode writes the single byte 03, that correspond to Unix.

For example: First Ten characters of a gzipped binary string (Hexadecimal):
1F 8B 08 00 00 00 00 00 00 03

(ASCII Characters numbers)
31 139 08 00 00 00 00 00 00 03

2) a body, containing DEFLATE-compressed binary data

3) Last 8 characters contains a CRC-32 checksum and length of the original uncompressed data, that should be read from Right to Left:
For example: Last 8 characters of gzipped binary string (Hexadecimal)

8C 72 E5 F5 1A 34 02 00

Last four bytes: 1A 34 02 00 = 0002341A = 144,410 represent the file length (142 k)
Bytes 8C 72 E5 F5 correspond to CRC-32 of this file: F5E5728C

If you need to use only the binary string of DEFLATE-compressed data, without the gzip header and footer, use a script like this:

put compress(myData) into myGzippedData
put char 11 to -9 of myGzippedData into myDeflateData

or the short version:

put char 11 to -9 of compress(myData) into myDeflateData

This binary string of DEFLATE-compressed data is useful if you need to write a Flate Encoded stream in a PDF file like this:

put "xÚ" & char 11 to -9 of myGzippedData into myFlateEncodedStream
----------------------------------------------------

Have a nice weekend!

Al

makeshyft
Posts: 220
Joined: Mon Apr 15, 2013 4:41 am
Contact:

Re: encrypted by php does not decrypt in lc

Post by makeshyft » Mon Nov 14, 2016 3:22 am

Thank you for finding and reposting the info ...it was a curious thing which i ended up not investigating.

the truth is all I needed was to scramble the info, didn't needto either compress or encrypt it ...i just thought might as well its a good way to scramble.

so i cost myself a bunch of time thinking that things will just work.... hahahaha.....

I ended up base encoding it. that seems to work well.

This is good to know for future projects as well.....

Thank you for taking the time
Founder & Developer @ MakeShyft R.D.A - https://www.makeshyft.com
Build Software with AppStarterStack for Livecode - https://www.AppStarterStack.com
Save Time with The Time Saver's Toolbox - https://www.TimeSaversToolbox.com

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

Re: encrypted by php does not decrypt in lc

Post by FourthWorld » Mon Nov 14, 2016 5:14 am

If base encoding fixes the issue it would seem the root cause was likely the wrong data type in the http header.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

makeshyft
Posts: 220
Joined: Mon Apr 15, 2013 4:41 am
Contact:

Re: encrypted by php does not decrypt in lc

Post by makeshyft » Wed Nov 16, 2016 7:01 pm

10-4 .... thats my suspicion as well..I'll tacle this next time I actually need to do this in pph...but i'm working on switching to lc server for anything else i may need to do in the future ....so i hope if someone has this same problem that they can find this post and save themselves some time. thanks as always
Founder & Developer @ MakeShyft R.D.A - https://www.makeshyft.com
Build Software with AppStarterStack for Livecode - https://www.AppStarterStack.com
Save Time with The Time Saver's Toolbox - https://www.TimeSaversToolbox.com

dpatterson
Posts: 24
Joined: Wed Jan 18, 2017 5:38 pm

Re: encrypted by php does not decrypt in lc

Post by dpatterson » Wed Jan 18, 2017 6:46 pm

If I recall correctly, the encryption functions can return binary strings.
Don't know if this is the actual cause, but you might try adding the binary option to the PHP fopen() statement.
$myfile = fopen($filename, "wb") or die("Unable to open file!");

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”