[Solved] encrypt in Livecode - decrypt with PHP ?

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
Zax
Posts: 457
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

[Solved] encrypt in Livecode - decrypt with PHP ?

Post by Zax » Wed Apr 26, 2017 1:46 pm

Hello,

I've encrypted some strings in a LiveCode application with:

Code: Select all

encrypt myString using "blowfish" with password myKey
Encryted strings are stored in a XML file, and now I try to decrypt these strings with PHP, using:

Code: Select all

mcrypt_decrypt(MCRYPT_BLOWFISH, $myKey, $myCryptedString, MCRYPT_MODE_ECB, '0');
And it doesn't work :(
I tried different MCRYPT_MODE, MCRYPT_MODE_ECB seems the best.
I also put '0' as IV, but without knowing why.

PHP mcrypt-decrypt documentation:
http://php.net/manual/fr/function.mcrypt-decrypt.php


Thanks for your help.

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

Re: encrypt in Livecode - decrypt with PHP ?

Post by FourthWorld » Wed Apr 26, 2017 3:06 pm

How are you passing the data from LC to PHP? More specifically, how do you convert the binary data returned from LC's encrypt command into a form suitable for inclusion in an XML file?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Zax
Posts: 457
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: encrypt in Livecode - decrypt with PHP ?

Post by Zax » Wed Apr 26, 2017 3:38 pm

The encrypted strings are base64Encoded and stored in a XML text file with LiveCode.

With PHP, I read this XML file (with simplexml_load_file PHP class), then I use base64_decode. At this point, key and cryptedString in PHP and LiveCode are equals.
My problem is with PHP mcrypt_decrypt: it returns me garbage instead of decrypted string.

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

Re: encrypt in Livecode - decrypt with PHP ?

Post by FourthWorld » Wed Apr 26, 2017 5:07 pm

Just to rule this part out, have you been able to determine that the output from PHP reversing the base64 encoding is the same as what went in within LC?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Zax
Posts: 457
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: encrypt in Livecode - decrypt with PHP ?

Post by Zax » Thu Apr 27, 2017 9:08 am

You're right and I made some tests: base64 encoding and decoding are the same in LiveCode and PHP.

An example of the problem I encounter:

Code: Select all

on mouseUp
   put "testing encryption" into theString
   put "banana" into theKey
   
   put myCrypt(theKey,theString) into cryptedString
   
   put cryptedString // returns: "U2FsdGVkX1+iykvqj69YVLreBFyoS99plklLbDC2EiSbxZpJ6fZ/2wVYjof/0VNC"
end mouseUp

function myCrypt theKey,theString
   encrypt base64Encode(theString) using "blowfish" with password theKey -- default 128 bits
   return base64Encode(it)
end myCrypt
For testing purpose, I copy "U2FsdGVkX1+iykvqj69YVLreBFyoS99plklLbDC2EiSbxZpJ6fZ/2wVYjof/0VNC" (without quotes, of course), and paste it into the following PHP script, to be sure that XML text file part has nothing to do with my problem:

Code: Select all

$theKey = "banana";
$theString = "U2FsdGVkX1+iykvqj69YVLreBFyoS99plklLbDC2EiSbxZpJ6fZ/2wVYjof/0VNC";

echo myDeCrypt($theKey,$theString); // returns: "�}*�� �b "

function myDeCrypt($theKey,$theString) {
	$it = base64_decode($theString,false);
	$it = mcrypt_decrypt(MCRYPT_BLOWFISH,$theKey,$it,MCRYPT_MODE_ECB,'0');
	return base64_decode($it,false);
}
I perform a double base64 encoding in order to prevent some "strange" characters thta could cause XML strange behaviour. I tested the scripts with only one base64 encoding but th problem is the same.
I'm using LiveCode Community Edition 7.0.0

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

Re: encrypt in Livecode - decrypt with PHP ?

Post by MaxV » Fri Apr 28, 2017 1:57 pm

Please note that livecode add "Salted__" to the crypted string, do you remove it?
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

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

Re: encrypt in Livecode - decrypt with PHP ?

Post by capellan » Sat Apr 29, 2017 3:33 am

Another useful comparison test is create, store and display
a md5 or sha1 digest of every step while doing the encryption
within both environments: LiveCode and PHP

Zax
Posts: 457
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: encrypt in Livecode - decrypt with PHP ?

Post by Zax » Sat Apr 29, 2017 9:27 am

I tried removing "Salted__" but it doesn't seem better.

I looked at this post : http://forums.livecode.com/viewtopic.php?f=11&t=22200. It's not with blowfish encryption and it seems to work but I don't understand how to define IV parameter when having variable key.

Zax
Posts: 457
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: encrypt in Livecode - decrypt with PHP ?

Post by Zax » Sat Apr 29, 2017 9:47 am

Well, I finally found how to use this damned IV parameter and I will use the solution described here:
http://forums.livecode.com/viewtopic.php?f=11&t=22200

Of course, in order to manage accented characters, I had to add

Code: Select all

put base64Encode(textEncode(pTokenText,"UTF-8")) into pTokenText
in EncryptIt function.

Anyhow, thank you all for your help.

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

Re: [Solved] encrypt in Livecode - decrypt with PHP ?

Post by FourthWorld » Sat Apr 29, 2017 4:32 pm

Excellent sleuthing, Zax. Thanks for posting the link to the solution.

I've taken the liberty of marking this thread "Solved" so others looking for this type of solution will be able to find it easily.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”