Compress string in php and decompress in python - php

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.

Related

base64 decoding works differently in node than in php, perl or python

I need to decode a base64 token for an authentication string, and I found some working examples in Python, Perl and PHP, and I wrote the equivalent code in Node, but I ran into an issue. It seems the base64 decoder for Node doesn't work the same way as for the other 3 languages.
Running this in Python
token = 'BaSe64sTRiNghERe'
decoded_token = token.decode('base64')
print decoded_token
returns this string
???F#`?D^
Running this in Perl
my $token = 'BaSe64sTRiNghERe';
my $decoded_token = decode_base64($token);
print $decoded_token;
returns this string
???F#`?D^
Running this in PHP
$token = 'BaSe64sTRiNghERe';
$decoded_token = base64_decode($token, true);
echo $decoded_token;
returns this string
???F#`?D^
and finally, running this in a Node script
var token = 'BaSe64sTRiNghERe',
decoded_token = Buffer.from(token, 'base64').toString();
console.log(decoded_token);
returns this string
????F#`?D^
The question is, why the extra question mark in the decoded string? And how can I get the same result in Node as I get in Perl, Python and PHP?
UPDATE
running this in the command line
echo BaSe64sTRiNghERe | base64 --decode
gives me the same output as the perl, python and php scripts
but running the same command from node
var exec = require('child_process').exec;
exec('echo BaSe64sTRiNghERe | base64 --decode', function callback(error, stdout, stderr){
console.log(stdout);
});
I still get the wrong stuff.
The output is different since you have generated unprintable characters, and node seems to handle those unprintable characters differently from the other languages. You are also losing information:
>>> token = 'BaSe64sTRiNghERe'
>>> decoded_token = token.decode('base64')
>>> print decoded_token
???F#`?D^
>>> decoded_token[0] == decoded_token[1]
False
If you modify your python snippet to look like this:
import binascii
token = 'BaSe64sTRiNghERe'
decoded_token = binascii.hexlify(token.decode('base64'))
print(decoded_token)
Then modify your nodejs snippet to look like this:
var token = 'BaSe64sTRiNghERe',
decoded_token = Buffer.from(token, 'base64').toString('hex');
console.log(decoded_token);
You will avoid the differences in how they handle unprintable characters and see that the base64 decodes have the same byte values.

PHP and MATLAB base64_decode works differently

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?

Coverting Hex to Image in PHP?

I am developing mobile app which talks with server via PHP Webservice. This is my first time using PHP. I managed to upload data in to database. Now i need to send an image to store it in ftp server. For that i converted image->hex and sent from my app.
Server Side
I got the hex code but not sure how to convert it in to an image and store in in ftp server. I am really struggling here. I googled it but couldn't find exact one.
Any help is much appreciated.
Convert the HEX string to binary:
$binary = pack("H*", $hex);
pack("H*", ...) is equivalent to hex2bin, which is available since PHP 5.4.
Write it to disk:
file_put_contents("file.png", $binary);
Suppose you have received a hex string in a page where you want to convert this hex to real image. Please check this code snippet will help you or not.
<?php
$hexpic=".......................
.....................";
# convert the hex string to binary
$data = pack("H" . strlen($hexpic), $hexpic);
#write the binary string into an image file
file_put_contents("sample.png", $data);
?>

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.

Read an image to a byte array using php

Using php I need to read an image to a byte stream which has to be passed to a .NET web service. Can anyone provide me with a php code snippet to read an image to a byte array ? I am using using php 5.
thanks
I don't believe PHP natively supports byte arrays in the same sense that .NET does. However, you could try converting each character to its ASCII representation:
<?
$file = file_get_contents($_FILES['userfile']['tmp_name']);
$byteArr = str_split($file);
foreach ($byteArr as $key=>$val) { $byteArr[$key] = ord($val); }
?>
Source: http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23325692.html

Categories