PHP and MATLAB base64_decode works differently - php

I am trying to write a base64 decode function in MATLAB.
In PHP, if I call built-in base64 decode function
$decodedStr = base64_decode($encodedStr)
$decodedStr is the result I want.
Now, I need to pass $encodedStr from PHP to MATLAB (as a parameter), and in MATLAB, I decode the string, same as PHP code above.
function base64_decode_matlab(x)
%x is same value as $encodedStr
base64 = org.apache.commons.codec.binary.Base64;
decodedStr1 = base64.decode(uint8(x));
end
However, it seems like the result is different than the result from PHP. What's wrong with my MATLAB code and how can I make the MATLAB base64 decode function works same as the PHP base64_decode function?

Related

Compress string in php and decompress in python

I have the following code in PHP:
$text = gzcompress($text);
// send $text to a python server
Then in python I have the following code:
text = request.POST.get('text', '')
new_text = zlib.decompress(text)
But this fails because the decompress function needs bytes and I am passing it a string. Does anyone know how to decompress a string in python that was compressed in php?
PS: I am not particularly interested in a specific algorithm or function.

output from GDAL ogr2ogr

I want to convert a .shp file to .geojson format using ogr2ogr, which I can do from the (linux) command line.
However, rather than a command line conversion, I want to invoke the conversion from a php script (using e.g. exec(..) ) and send the output directly to a variable (as a character string) instead of writing to a file.
Is this possible?
You can use shell_exec() method available in PHP.
public function shptogeojson($shpfilepath,$output,$srsno,$srsold){
$query="ogr2ogr -f GeoJSON -t_srs EPSG:$srsno -s_srs EPSG:$srsold $output.shp $shpfilepath";
shell_exec($query);
}

Equivalent PHP GZIP Compression like in VB.NET

I'm migrating a web service that was developed in VB.NET to PHP
I explain:
In VB. NET I have a method that compresses a single string with GZIP. ("Hello world!")
The method in the web service returns an array of bytes.
Then the array of bytes is received on a device with android, decompressed and converted to a string, this process works perfect.
the method in VB.NET, is this:
<WebMethod(Description:="GZIP Test")> _
Public Function GZIP() As Byte()
Dim vTest As String = "Hello world!"
Dim vBuffer1() As Byte = StrToByteArray(vTest)
Dim vBuffer2() As Byte = Compress(vBuffer1)
Return vBuffer2
End Function
Private Function StrToByteArray(ByVal str As String) As Byte()
Dim encoding As New System.Text.UTF8Encoding()
Return encoding.GetBytes(str)
End Function
Private Function Compress(ByVal Bits() As Byte) As Byte()
On Error Resume Next
Using ms As New MemoryStream(), zipMem As New GZipStream(ms, CompressionMode.Compress, True)
zipMem.Write(Bits, 0, Bits.Length)
zipMem.Close()
Return ms.ToArray
End Using
End Function
this method returns me the following value:
<base64Binary>H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/Ir6dl2WVXlV1Oftd/x+VGYUbDAAAAA==</base64Binary>
I want PHP return me the SAME VALUE.
the tests I've done in PHP returns me the following.
function GZIP() {
ob_start ( 'ob_gzhandler' );
return base64_encode(gzdeflate('Hello world!', 9));
}
the value returned in PHP is:
80jNyclXKM8vyklRBAA=
Why ? There is an example that returns the same ?
Thanks in advance for all.
You are using the wrong de-/compression algorithm. Use phps gzcompress() and gzuncompress() instead.
First off, you can't require the exact same result. All you can require of a lossless compressor is that it reproduce exactly the same input when decompressed.
Second, you want to use gzencode to produce gzip streams. Neither gzdeflate nor gzcompress will do that. The former produces raw deflate streams, and the second zlib streams. (Don't get me started about the misleading names and the messed up PHP documentation about them.)

Something missing in iPhone/PHP encryption/decryption

I have been trying to implement some encryption between an iPhone app and a PHP web service. It's not working however. It seems like the first half of the text is NOT decrypted while the second half is decrypted just fine. What should I do?
The PHP encryption method is as follows:
function decrypt($str, $iv) {
$iv .= "00000000";
$str = base64_decode($str);
return self::decrypt_data($str, $iv, self::secret_key);
}
The iPhone stuff that encrypts the text uses a CryptoHelper class like this:
NSString *encrypted = [[CryptoHelper sharedInstance] encryptString:dataString];
The CryptoHelper class can be seen at http://pastie.org/1267796.
Try a simple example where you send a known Base64 encoded string from the iPhone app to PHP.
Compare the known valid string to what PHP is getting. I know recently, when trying to do an Ajax post from a script to PHP, we were having trouble with some characters (specifically +) being converted to spaces by PHP because it was doing a URL decode automatically. We had to switch all + to their % (URL-encoded %2B) equivalent. This fixed the problem for us.

Mask Hash Function - php

I'd like to hash a file using php's hash_file(), but obfuscate it so it is not easily detected by a text string search in a text editor. Any advice? Possible?
You could use base64_encode/base64_decode to mask the name of the command being executed.
$command = "hash_file";
$encodedcommand = base64_encode($command); //aGFzaF9maWxl
Now you know that the base64 encoding of 'hash_file' is aGFzaF9maWxl.
So in your real script, just decode and execute that string:
$maskedcommand = base64_decode("aGFzaF9maWxl");
print $maskedcommand("md5",$filename);
So the string 'hash_file' isn't in the two lines of code above, but it still executes the 'hash_file' command.

Categories