base64

PEAR, Smarty, ADOdb, OOP, PHP 5, XML, UML, Şabloane de proiectare, PHP-GTK.

Moderatori: coditza, Emil, Moderatori

theramore
PHPRomania Supporter
Mesaje: 12
Membru din: Lun Aug 22, 2011 4:04 pm
Localitate: Slobozia
Contact:

base64

Mesajde theramore » Dum Aug 11, 2013 2:32 am

Ma intrebam daca are cineva sau ma poate ajuta cu un algoritm pentru base64 . stiu ca e functie predefinita dar vreau algoritmul sa il pot studia amantuntit pentru a face propria functie de decodare...sa invat :)



MarPlo
Senior Member
Mesaje: 885
Membru din: Mie Sep 24, 2008 9:32 am
Localitate: Braila
Contact:

Re: base64

Mesajde MarPlo » Dum Aug 11, 2013 10:04 am

Salut
Daca stii JavaScript, poti studia functiile de codificare, decodare base64 de la: JavaScript base64 codificare decodare .

tedy
Average Member
Mesaje: 95
Membru din: Vin Ian 06, 2012 7:28 pm
Localitate: Ramnicu Valcea
Contact:

Re: base64

Mesajde tedy » Dum Aug 11, 2013 2:23 pm

Sau daca stii C, asta e sursa. Si sursa din PHP tot in C e scrisa cel mai probabil.

theramore
PHPRomania Supporter
Mesaje: 12
Membru din: Lun Aug 22, 2011 4:04 pm
Localitate: Slobozia
Contact:

Re: base64

Mesajde theramore » Dum Aug 11, 2013 4:32 pm

Multumesc amandurora pt ajutor , am gasit si eu o sursa php , cei care mai au nevoie o gasesc mai jos





Cod: Selectaţi tot

<?php

class Base64 {
   
    /**
     * I have changed letter placement (P <=> x, S <=> 9) and the cases
     * You can completely redo the mapping table
     */

    private static $BinaryMap = array(
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', //  7
        'i', 'j', 'k', 'l', 'm', 'n', 'o', 'x', // 15
        'q', 'r', '9', 't', 'u', 'v', 'w', 'x', // 23
        'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', // 31
        'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', // 39
        'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', // 47
        'W', 'P', 'Y', 'Z', '0', '1', '2', '3', // 55
        '4', '5', '6', '7', '8', 'S', '+', '/', // 63
        '=',  // padding char
    );
   
    public function __construct() {}
   
    public function base64_encode($input) {
       
        $output = "";
        $chr1 = $chr2 = $chr3 = $enc1 = $enc2 = $enc3 = $enc4 = null;
        $i = 0;
       
//        $input = self::utf8_encode($input);
       
        while($i < strlen($input)) {
            $chr1 = ord($input[$i++]);
            $chr2 = ord($input[$i++]);
            $chr3 = ord($input[$i++]);
           
            $enc1 = $chr1 >> 2;
            $enc2 = (($chr1 & 3) << 4) | ($chr2 >> 4);
            $enc3 = (($chr2 & 15) << 2) | ($chr3 >> 6);
            $enc4 = $chr3 & 63;
           
            if (is_nan($chr2)) {
                $enc3 = $enc4 = 64;
            } else if (is_nan($chr3)) {
                $enc4 = 64;
            }
           
            $output .=  self::$BinaryMap[$enc1]
                      . self::$BinaryMap[$enc2]
                      . self::$BinaryMap[$enc3]
                      . self::$BinaryMap[$enc4];
        }
       
        return $output;
    }
   
    public function utf8_encode($input) {
        $utftext = null;
       
        for ($n = 0; $n < strlen($input); $n++) {

            $c = ord($input[$n]);
           
            if ($c < 128) {
                $utftext .= chr($c);
            } else if (($c > 128) && ($c < 2048)) {
                $utftext .= chr(($c >> 6) | 192);
                $utftext .= chr(($c & 63) | 128);
            } else {
                $utftext .= chr(($c >> 12) | 224);
                $utftext .= chr((($c & 6) & 63) | 128);
                $utftext .= chr(($c & 63) | 128);
            }
        }
       
        return $utftext;
    }
}

?>

and the usage as follows:

<?php
$string = pack('H*', "31c85c5aaa56c1f0102301ea497d0ab010e4e131af261787"); // HEX to binary
echo Base64::base64_encode($string);
echo "<br />";
echo base64_encode($string);
?>

and the output will be:

mCHCwQPwWFaqiWhQ9x0kSbdK4tgVjHEh   //  with custom mapping
MchcWqpWwfAQIwHqSX0KsBDk4TGvJheH   //  the base64_encode()


Înapoi la “PHP Avansat”

Cine este conectat

Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 14 vizitatori