Encode and Decode without mbstring

cwarn23 0 Tallied Votes 323 Views Share

Hi, I have made an encryption and decryption script for those who want to encode data and decode the encoded data. This script may especially be useful for those who do not have the mbstring library installed as it uses a math grid. So for example you can store the encoded data in a text file where nobody can translate it then get php to translate it with the decode function. Enjoy.

<?php
function text_encode($input) {
$arr=str_split($input,1);
$result='';
foreach($arr AS $val) {
    $b=floor(ord($val)/4);
    $a=$b+$b+32+($val%4);
    $b+=64-(ord($val)%4);
    $c=64+(ord($val)%4);
    $result.=chr($a).chr($b).chr($c);
    }
return $result;
}
 
function text_decode($input) {
$arr=str_split($input,3);
$result='';
foreach ($arr AS $group) {
    $sub=str_split($group,1);
    $c=ord($sub[2]);
    $c-=64;
    $b=ord($sub[1]);
    $b-=(64-$c);
    //echo $b;
    $chr=$b*4;
    $chr+=$c;
    $result.=chr($chr);
    }
return $result;
}
 
$text=text_encode('Testing...');
echo text_decode($text);
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.