Search code examples
phpfunctionbase64decodeencode

How to base64 encode php in this format?


can you guide me how to base64 encode php by base64 decode below? I have php code that decodes base64 in below encoding way:

function fn_one($a){$c='';for ($i=0;$i < strlen($a);$i++) {$b=ord($a[$i]);$c.=chr(--$b);}return $c;}$decode=call_user_func("base64_decode",call_user_func("fn_one","cnGu[R>>"));echo $decode; // Deconde "cnGu[R>>" to "name"

In php the content is encoded as "cnGu[R>>" with the decoding code as below, the output after base64 decoding is "name".

Can i encode in below way but result is not "cnGu[R>>"?

$encode=call_user_func("base64_encode",call_user_func("fn_one",$decode));echo $encode;// Encode "name" to "cnGu[R>>"

So how do I base64 encode "name" in reverse in the format below which results in "name" as "cnGu[R>>" ? Any correct answer is appreciated! Thank you.


Solution

  • Reverse it, i.e change chr(--$b) to chr(++$b)

    <?php
    function fn_two($a){$c='';for ($i=0;$i < strlen($a);$i++) {$b=ord($a[$i]);$c.=chr(++$b);}return $c;}
    
    echo fn_two(base64_encode('name'));
    

    Result

    cnGu[R>>