PHP's base64_encode
is returning a different string to the linux base64
command. Why is this?
PHP:
$ php <?php echo base64_encode('test'); ?> dGVzdA==
Linux base64:
$ echo 'test' | base64 dGVzdAo=
base64_encode() function can encode the given data with base64. This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean such as mail bodies. Base64-encoded data can take about 33% more space than original data.
The base64_decode() is an inbuilt function in PHP which is used to Decodes data which is encoded in MIME base64. Parameters: This function accepts two parameter as mentioned above and described below: $data: It is mandatory parameter which contains the encoded string. $strict: It is an optional parameter.
Base64 is a binary to text encoding scheme that is generally used to transfer content-based messages over the Internet. It works by dividing every three bits of binary data into six bit units. The newly created data is represented in a 64-radix numeral system and as seven-bit ASCII text.
Base64 is the industry standard format for SSL certificate content. The most common web servers will generate a certificate signing requests as well as accept SSL certificates in base-64 format. The size of the certificate content will depend on the encryption strength of the certificate.
echo
usually outputs a new line character at the end of the string, to suppress that use the -n
switch:
$ echo -n 'test' | base64 dGVzdA==
Similarly for PHP:
$ php <?php echo base64_encode("test\n"); ?> dGVzdAo=
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With