Page 1 of 1

encrypted by php does not decrypt in lc

Posted: Tue Nov 01, 2016 10:35 pm
by makeshyft
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);

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.

Re: encrypted by php does not decrypt in lc

Posted: Tue Nov 01, 2016 11:57 pm
by makeshyft
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.

Re: encrypted by php does not decrypt in lc

Posted: Wed Nov 02, 2016 11:55 am
by MaxV
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.

Re: encrypted by php does not decrypt in lc

Posted: Wed Nov 02, 2016 6:06 pm
by makeshyft
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.

Re: encrypted by php does not decrypt in lc

Posted: Sun Nov 13, 2016 2:59 am
by capellan
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!


Re: encrypted by php does not decrypt in lc

Posted: Mon Nov 14, 2016 3:22 am
by makeshyft
Thank you for finding and reposting the info 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

Re: encrypted by php does not decrypt in lc

Posted: Mon Nov 14, 2016 5:14 am
by FourthWorld
If base encoding fixes the issue it would seem the root cause was likely the wrong data type in the http header.

Re: encrypted by php does not decrypt in lc

Posted: Wed Nov 16, 2016 7:01 pm
by makeshyft
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 i hope if someone has this same problem that they can find this post and save themselves some time. thanks as always

Re: encrypted by php does not decrypt in lc

Posted: Wed Jan 18, 2017 6:46 pm
by dpatterson
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!");