Best way  to use PHP base64 encode and decode password in php


Hi friends, today we will learn how to base64 encode and decode the password.

Basically, we use this method to secure our password.

1- base64_encode 


Base64 encoding converts triples of eight-bit symbols into quadruples of six-bit symbols. Reading the input file in chunks that are a multiple of three bytes in length results in a chunk that can be encoded independently of the rest of the input file. MIME additionally enforces a line length of 76 characters plus the CRLF. 76 characters are enough for 19 quadruples of six-bit symbols thus representing 19 triples of eight-bit symbols. Reading 57 eight-bit symbols provides exactly enough data for a complete MIME-formatted line. Finally, PHP's default buffer size is 8192 bytes - enough for 143 MIME lines' worth of input.

So if you read from the input file in chunks of 8151 (=57*143) bytes you will get (up to) 8151 eight-bit symbols, which encode as exactly 10868 six-bit symbols, which then wrap to exactly 143 MIME-formatted lines. There is no need to retain left-over symbols (either six- or eight-bit) from one chunk to the next. Just read a chunk, encode it, write it out, and go on to the next chunk. Obviously, the last chunk will probably be shorter, but encoding it is still independent of the rest.

Note - Base64-encoded data takes about 33% more space than the original data.


Parameters

  •       dataThe data to encode.

Return Value

  •       The encoded data, as a string.

 base64_encode ( $data ) 


Example 



<?php


  $pwd = 'shortutroialspoints';

  echo base64_encode($pwd);


?>

  Output - c2hvcnR1dHJvaWFsc3BvaW50cw==



2 - base64_decode 

The base64-decoding function is a homomorphism between modulo 4 and modulo 3-length segmented strings. That motivates a divide and conquer approach: Split the encoded string into substrings counting modulo 4 chars, then decode each substring and concatenate all of them.

Decodes data encoded with MIME base64


Parameters 

  • data: The encoded data.

strict

If the strict parameter is set to TRUE then the base64_decode() function will return FALSE if the input contains a character from outside the base64 alphabet. Otherwise invalid characters will be silently discarded.


Return Values

  • Returns the decoded data or FALSE on failure. The returned data may be binary.
base64_decode ( $data)

Example -


We will convert the above string which we made base64_encode.



<?php


  $pwd = 'c2hvcnR1dHJvaWFsc3BvaW50cw==';

  echo base64_decode($pwd);


?>

  Output - shortutroialspoints



I hope you understood this and like this article. if you like it so, please give feedback in the comment section.

If any mistakes I made here, so please let me know and improve me.
If you have any query, feel free to ask me  ЁЯШК

Recommended post

Post a Comment

Please do not enter any spam link in the comment section.

Previous Post Next Post