0%

BASE64编码原理学习笔记

Base64编码原理解析

Base64编码是基于64个字符A-Z,a-z,0-9,+,/的编码方式,因为2的6次方正好为64,所以就用6bit就可以表示出64个字符

举个栗子

‘A’ 转变为Base64编码:

img

表格流程

字符 A
AIISC码 65
二进制 01000001
分组 010000 01000000 = =
十进制 16 16 = =
BASE64 Q Q = =

BASE64查询表

['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                'a', 'b', 'c', 'd','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
                '0', '1', '2', '3','4','5','6','7','8','9',
                '+', '/', '=']

从上方流程可以简单知道BASE64是先转换为AIISC码再转换为二进制进而将8位bit分组为6位bit最后不足补0,如组数不足4组,那么就用等于号(“=”)补充。最后将分组的各组再转换为10进制,按照换回来的数字当做base64编码表的形式进行查表得出结果。例如16就是在上述表中找第17位也就是数组的第16位。

Python实现BASE64编码

import re
base64_table = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                'a', 'b', 'c', 'd','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
                '0', '1', '2', '3','4','5','6','7','8','9',
                '+', '/', '=']
def num_10_to_2(numbers):
    result=[]
    tmp=[]
    return_str=''
    for number in numbers:
        while number/2!=0:
            result.append(int(number%2))
            number=int(number/2)    
        if(len(result)<8):
            for i in range(0,8-len(result)):
                result.append(0)
        number_2=''
        for i in range(0,len(result)):    
            nums=len(result)-i-1
            number_2=str(result[nums])+number_2
        tmp.append(number_2)
        result=[]
    for i in range(len(tmp)):
        return_str=return_str+tmp[i][::-1]
    return return_str
def num_2_to_10(number):
    number=str(number)
    nums=0
    for i  in number:
        nums=nums*2+int(i)
    return nums            

def str_to_aiisc(str_char):
    tmp=[]
    for i in str_char:
         tmp.append(int(ord(i)))    
    return tmp
def cut_string(strings,lenth):
    result =  re.findall('.{'+str(lenth)+'}',strings)
    result.append(strings[(len(result)*lenth):]) #把最后几位加上
    for i in range(0,6-len(result[len(result)-1])):
        result[len(result)-1]=result[len(result)-1]+str('0')
    return result
def main(strings):
    group_base = cut_string(num_10_to_2(str_to_aiisc(strings)),6)
    if(group_base[len(group_base)-1])=='000000':
        group_base.pop(len(group_base)-1)
    #print(group_base)
    for i in group_base:
        print(base64_table[num_2_to_10(i)], end='')
    #print(4-int((len(group_base)/4)/0.25)%4)    
    for i in range(4-int((len(group_base)/4)/0.25)%4):
        print("=", end='')
    print('\n')    
if __name__=='__main__':
    main('A')

PHP实现BASE64编码

<?php
function num_10_to_2($number){
    $result=[];
    $tmp=[];
    $number_2='';
    for($i=0;$i<count($number);$i++){
        while ($number[$i]/2!=0) {
        array_push($result,intval($number[$i]%2));
        $number[$i]=intval($number[$i]/2);
        }
        for($j=0;$j<count($result);$j++){
            $nums = count($result)-$j-1;
            $number_2 = strval($result[$nums]).$number_2;
        }
        ///echo $number_2.'<br>';
        array_push($tmp,$number_2);
        $number_2='';
        $result=[];
    }
    //var_dump($tmp);
    for($j=0;$j<count($tmp);$j++){
    if(strlen($tmp[$j])<8){
        for($i=0;$i<8-strlen($tmp[$j]);$i++){
            $tmp[$j]=$tmp[$j].'0';
        }
        $tmp[$j]=strrev($tmp[$j]);
        //echo $tmp[$j].'<br>';
    }
    }

```
return implode($tmp);
```

}
function num_2_to_10($number){
    $number = strval($number);
    $nums = 0;
    for($i=0;$i<strlen($number);$i++){
        $nums = $nums*2+intval(substr($number,$i,1));
    }
    return $nums;
}
function str_to_aiisc($str_char){
    $result=[];
    $str_char = strval($str_char);
    for($i=0;$i<strlen($str_char);$i++){
        array_push($result,intval(ord(substr($str_char,$i,1))));
    }
    return $result;
}
function cut_string($number,$lenth){
    $number = strval($number);
    $number =  str_split($number,intval($lenth));
    for($i=0;$i<count($number);$i++){
        if(strlen($number[$i])<6){
            $number[$i]=$number[$i].str_repeat('0', 6-strlen($number[$i]));
        }
    }
    return $number;
}
function base64Encode($strings){
    $base64_table = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                'a', 'b', 'c', 'd','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
                '0', '1', '2', '3','4','5','6','7','8','9',
                '+', '/', '='];
    $result='';
    $group_bas64 = cut_string(num_10_to_2(str_to_aiisc($strings)),6);
    for($i=0;$i<count($group_bas64);$i++){
        $result=$result.$base64_table[num_2_to_10($group_bas64[$i])];
    }
    for($i=0;$i<4-intval((count($group_bas64)/4)/0.25)%4;$i++){
        $result=$result."=";
    }
    return $result;
}
 echo base64Encode('ABaaa')
?>