How to encode url in base64 that no body can decode it - php

I want to encode my urls in base64 and decode it in my functions and no body can decode it how can i do this.
<?php
$somestring = 'here is my some string';
$url = 'http://google.com/'.$somestring;
base64_encode($url);
?>

Base64 is not meant for encrypting sensible data. It's merely a means of representing data in another way. What it does, it takes binary data and converts it to ASCII, so that binary data can be represented with ASCII characters.
If you want to encrypt strings like your URLs, you have to choose an encryption algorithm like Blowfish, which is available in the PHP bcrypt extension.

Related

PHP Hex Code read as Hex strings when encoding to base64

I have an array of hex code in this format
FF8FFE00+++++
The above example is just one string, with + representing the rest of the over 60k long hex code (no use in hogging your broswer). The format looks exactly like that. So let's say
$a = 'FFD8FF++++ string';
echo base64_encode($a);
When printing the above, it takes the hex code as a string and generates the base64 out of the string instead of the hexdec
Looked all over, but it just seems that there are either conversions that make the hex code get hex encoded as well.
In NPP+ i converted a the string to ASCII then encoded in base64 and the result was the expected one (base64 that i can use for an image).
Any idea how I can tell php that the string is actually hex node, not a string?
With hex2bin you can transform a string containing hexadecimals into binary data (stored as a string in PHP). After that encode the binary string to base64 format.
$hex = 'FFD8FF'; // and much more hex values as string as in your example
$bin = hex2bin($hex); // convert the hex values to binary data stored as a PHP string
$b64 = base64_encode($bin); // this contains the base64 representation of the binary data
string(4) "/9j/"

How to fully decode a base64 string in PHP?

I'm trying to implement the Google Safebrowsing update API v4 in PHP.
But I can't figure how to correctly decode the rawHashes.
(The rawHashes are 4-bytes-truncated sha256 hashes and then concatenated).
I am trying the native base64_decode of PHP but I can't fully decode the string, and I don't know what the next step is.
According to the API documentation here's how the rawhashes are encoded :
string (bytes format)
The hashes, in binary format, concatenated into one long string. Hashes are sorted in lexicographic order. For JSON API users, hashes are base64-encoded.
A base64-encoded string.
I an very simply decoding the string like so:
$decoded = base64_decode($rawHashes);
The base64 encoded string look like this:
"AAAIYAAAC90AABOxAAAjDgAALZIAAEbKAABIHwAA..."
And the base64 decoded string look like this:
b"\x00\x00\x08`\x00\x00\vÝ\x00\x00\x13±\x00\x00#\x0E\x00\x00-’\x00\x00FÊ\x00\x00H\x1F\x00\x00^\x06\x00\x00bF\x00\x00h²"
As you can see something is not right and I must have missed a step but I can't figure which one.
As Mjh said in the discussion nothing is wrong about base64_decode and nothing else is needed.
Nothing's wrong. You just aren't reading carefully. Here, read what it says: The hashes, in binary format. It says binary format. After decoding, you got binary representation of the data. Using bin2hex should return a human-readable hash. $hash = bin2hex(base64_decode($your_encoded_hash)); - Mjh
The decoded string was looking weird as it is binary data (Raw SHA256 hash) although it is totally correct. To get the hashes in a more convenient encoding it's possible to convert the binary represented data to hex represented data with the php function bin2hex
$hash = bin2hex(base64_decode($your_encoded_hash));
From what I know of base64_decode, it just works. Something must be wrong in your $rawHashes string. If you have line breaks in your string, you need to get rid of them by replacing them with an empty string. The hash that base64_decode needs should be one long line of base64 encoded string. It is not uncommon to receive a hash that is broken into multiple lines.
Try this ...
$decoded = base64_decode(str_replace(PHP_EOL, "", $rawHashes));

How to encode a string in base64 to be used in a URL?

I need to attach a confirmation code that can hold several parts and almost random characters, to an email.
The idea is to print the URL with that code in the message body (in HTML).
Is the base64_encode() function enough to make it safe to be parsed by the browser ?
Base64 encoding is to take binary data and make it suitable for being treated as text.
That isn't your problem (you said you had random characters) so you shouldn't use Base64.
You have text and want to insert it into a URL. You need to URL encode it. That is what the urlencode() function is for.
You then want to insert that URL in an HTML document. That is what the htmlspecialchars() function is for.
$data = function_to_get_random_data();
$url_safe_data = urlencode($data);
$url = "http://example.com/$url_safe_data";
$html_safe_url = htmlspecialchars($url);
$html = "$html_safe_url";

PHP Base64 encode with salt string

How can I encode string using PHP Base64_encode function with passing some Salt String
I don't want others to hack my string. If some one is having little knowledge in php, he can easily hack if I directly encode string. Because this is the only function allow you to decode encoded content. As far as I know.
In the tutorials I see above function will accept only one parameter.
Any Help ?
You should use mcrypt php extension.

Is there any alpha numeric string encoder for PHP other than Hex?

I want to encode some binary strings with something like base64 but only with alpha numeric chars. I know bin2hex could do this, but it makes the encoded string much longer (i tries gzcompress in the strings but didn't make much difference).
Is there any other existing encoding method to do this?
http://en.wikipedia.org/wiki/Binary-to-text_encoding
The most used forms of binary-to-text encodings are:
hexadecimal
base64
quoted-printable
uuencoding
yEnc
Ascii85
BinHex
Percent encoding

Categories