1

Hi my question is that does base64_encode does unique data every time we run the script?

Below is the code.

    <?php
    $id = 1;
    echo base64_encode($id);
    ?>

If it does not provide the unique data every time then what is the point in encoding the string and passing in url. Does that make url safe??

Amit
  • 3,251
  • 3
  • 20
  • 31

3 Answers3

2

Base64 encoding is not a method of encryption. It is used for encoding binary data into text, which makes it safer to transmit over the internet.

If you stream bits, some protocols may interpret it differently. Streaming text is much more reliable.

What is base 64 encoding used for?

If you need true encryption, you need to use something which hashes based on a salt you can hide from other users, such as the mcrypt library.

http://php.net/manual/en/book.mcrypt.php

Community
  • 1
  • 1
Jordan Glue
  • 279
  • 2
  • 3
  • **hase is not encryption also**. because the hash is not reversible. encryption hides (encrypts) the `clear text` by a `key` and make it "ciphertext", and also by a `key` it unhide (decrypt) that `ciphertext` to `clear text` back (if the encryption process uses the same `key` to hide and unhide it is called `symmetric encryption` and if it uses different `keys` for that it is called `asymmetric encryption`) – Ben.S Apr 24 '23 at 12:22
2

base64-encoding does not provide unique data. Its purpose is to provide a compact representation of binary data in string form. In your example, you are encoding non-binary data, so it is not very practical. However, if you wanted to encode a string containing a newline and punctuation and pass it via the URL, you cannot send the binary data directly.

For example, if you had the string Hello, World!!\n there would be three punctuation marks, a space and a newline that all need to be URL-encoded. Doing that gives the result:

Hello%2C+World%21%21%0A

Which is 23 bytes long.

On the other hand if you were to base64-encode the same string, the result would be:

SGVsbG8sIFdvcmxkISEK

Which is 20 characters, or about 13% shorter. This adds up quickly if you've got a lot of non-alphanumeric characters or a large amount of data.

So the primary advantage of base64 encoding is its slightly more compact representation of certain data.

Max
  • 913
  • 1
  • 7
  • 18
1

Base64 encoding is a way of representing data using only a limited set of characters. You use it when you need to store data in something such as a cookie that can't handle the data in its original format.

Mark
  • 2,792
  • 2
  • 18
  • 31