search
HomeBackend DevelopmentPHP TutorialBase64编码原理分析与PHP实现

Base64是一种基于64个可打印字符来表示二进制数据的表示方法。由于2的6次方等于64,所以每6个为一个单元,对应某个可打印字符。

三个bites有24个比特,对应于4个Base64单元,即3个字节需要用4个可打印字符来表示。它可用来作为电子邮件的传输编码。

在Base64中的可打印字符包括字母A-Z、a-z、数字0-9,这样共有62个字符,此外两个可打印符号在不同的系统中而不同。

如在mime(多用途邮件扩展)中,Base64的使用的64个可打印字符

A-Za-z:大小写字母各26个

0-9:加上10个数字

+:加号

/:斜杠

一共64个字符,等号“=”用来作为后缀用途

对应的转换关系为

0-63:A-Za-z0-9+/


转换的时候,将三个byte的数据,先后放入一个24bit的缓冲区中,先来的byte占高位。数据不足3byte的话,于缓冲器中剩下的bit用0补足。然后,每次取出6(因为26=64)个bit,按照其值选择ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/中的字符作为编码后的输出。不断进行,直到全部输入数据转换完成。

当原数据长度不是3的整数倍时, 如果最后剩下一个输入数据,在编码结果后加2个“=”;如果最后剩下两个输入数据,编码结果后加1个“=”;如果没有剩下任何数据,就什么都不要加,这样才可以保证数据还原的正确性。

实例分析:

编码:"Lailaiji"

通过查ASCII表找到对应关系

L:0x4c |  a:0x61  | i:0x69   |  l:0x6C    |  j:0x6A

因此转换成二进制为:0100 1100 , 0110 0001 , 0110 1001 , 0110 1100 ,  0110 0001 , 0110 1001 , 0110 1010 ,  0110 1001

第一步:先取三个字节的数据即:0100 1100 , 0110 0001 , 0110 1001,然后从这个三字节中取出6位即010011,在最高位补充两个位00使其成为1个byte,即0001 0011,剩余的18位也如此循环,最终这三个字节将扩展成为4个字节即:0001 0011, 0000 0110, 0000 0101, 0010 1001

第二步,从剩余的字节序列中再重复第一步,走到小于3个字节

第三步,这时剩余字节为:0110 1010,0110 1001不足3个字节,需要在从低位以0进行补充,即成0110 1010,0110 1001,0000 0000重复第一步,得到:0001 1010,0010 0110, 0010 0100, 0000 0000,

经过以后的运算后,我们将得到一组位序列:

0001 0011, 0000 0110, 0000 0101, 0010 1001

0001 1011,0000 0110, 0000 0101, 0010 1001

0001 1010,0010 0110, 0010 0100, 0000 0000 

转换成十进制为:19,6,5,41,27,6,5,41,26,38,36,0

对应的字符为:T,G,F,p,b,G,F,p,a,m,k,A

特别注重的是,最后一个字节0000 0000即0x00,通过查表为A,由于最后这8位是补充的,所以它应当被转换成=号,而不是A

因此:最终结果为:TGFpbGFpamk=

PHP实现:

<?php$input = '赖来基';$obj = new MyBase64();$output = $obj->encode($input);echo "Encode:",$output.PHP_EOL;$output = $obj->decode($output);echo "Decode:",$output.PHP_EOL;class MyBase64{	private $_table = array();	private $_revtable = array();	public function __construct(){		$this->_initTable();	}	public function decode($string)	{		$orign_len = strlen($string);		$j = 0;		$ret = null;		for($i=0; $i<$orign_len; $i+=4)		{			$chr1 = $this->getRevChr($string[$i]);			$chr2 = $this->getRevChr($string[$i+1]);			$chr3 = $this->getRevChr($string[$i+2]);			$chr4 = $this->getRevChr($string[$i+3]);			$_chr1 = $chr1<<2 | ($chr2&0x3F) >>4;			$_chr2 = ($chr2&0x0F)<<4 | ($chr3&0xFC) >>2;			$_chr3 = ($chr3&0x03)<<6 | $chr4;			$ret .= chr($_chr1);			$ret .= chr($_chr2);			$ret .= chr($_chr3);		}		$ret = rtrim($ret);		return $ret;	}	private function getRevChr($chr)	{		if(isset($this->_revtable[$chr]))		{			return $this->_revtable[$chr];		}else{			return 0;		}	}	public function _decode($string)	{		$orign_len = strlen($string);		$de = null;		$kv = array_flip($this->_table);		$b  = null;		for($i = 0 ;$i < $orign_len;$i++)		{			$chr = $string[$i];			if($chr != '='){				$c = $kv[$chr];			}else{				$c = chr(0);			}			printf("%x",$c);			$b[] = pack('C',$c);			echo PHP_EOL;		}		for($i = 0 ;$i < count($b);$i+=3){			$ch1 = ($b[$i]<<2) | ($b[$i+1]>>4);			$ch2 = ($b[$i+1]<<4) | ($b[$i+2]>>2);			$ch3 = ($b[$i+2]<<6) | ($b[$i+3]);			printf('%08b,%08b,%08b',$ch1,$ch2,$ch3);			echo PHP_EOL;			printf('%08b,%08b',($b[$i]<<2) , ($b[$i+1]>>4));			echo PHP_EOL;		}	}	public function encode($string)	{		$orign_len = strlen($string);		$len       = intval(ceil($orign_len/3)*3);		$bin       = pack('a'.$len,$string);		$gen       = null;		for($i=0; $i<$len; $i+=3)		{			$ch1 = ord($bin[$i]) >> 2;			$ch2 = ((ord($bin[$i]) & 0x03) << 4) | (ord($bin[$i+1]) >> 4);			$ch3 = ((ord($bin[$i+1]) & 0x0F) << 2) | ((ord($bin[$i+2]) & 0xC0) >> 6);			$ch4 = ord($bin[$i+2]) & 0x3F;			$gen.= $this->_table[$ch1];			$gen.= $this->_table[$ch2];			$gen.= $this->_table[$ch3];			$gen.= $this->_table[$ch4];		}		if($orign_len-$len){			$gen = substr($gen,0, -abs($orign_len-$len));			for($i=0;$i<$len-$orign_len;$i++)			{				$gen .= '=';			}					}		return $gen;	}	private function  _initTable()	{		$tbl = array();		for($i=ord('A');$i<=ord('Z');$i++)		{			$tbl[] = chr($i);		}		for($i=ord('a');$i<=ord('z');$i++)		{			$tbl[] = chr($i);		}		for($i=ord('0');$i<=ord('9');$i++)		{			$tbl[] = chr($i);		}		$tbl[]           = '+';		$tbl[]           = '/';		$reverse         = array_flip($tbl);		$this->_table    = $tbl;		$this->_revtable = $reverse;	}}


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use