当前位置:网站首页>php base64加密
php base64加密
2022-04-21 06:38:00 【Mars慕容】
<?php
/**
* Base64 编码/解码
* @author liruixing
*/
class Base64{
private $_base64hash = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; /*这是Base64编码使用的标准字典*/
private $_DecodeTable = array( /* 这是php源码中使用的解码表,包含了256个字符对应的编码 */
-2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2,
-2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2,
-2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
);
private $_encode_data = 0xfc0000;
private $_debug = false;
private $_encode_result = '';
public function encode($str) {
$len = strlen($str);
$num = 0;
$bin = 0;
$arr = array();
if($len >= 3) {
for($i=0;$i<$len;$i++) {
$bin = $bin << 8;
if($this->_debug) {
echo '$bin = ',decbin($bin),"\n";
echo 'binary = ', decbin(ord($str[$i])),"\n";
}
$bin = $bin | ord($str{$i});
if(($i+1)%3 == 0) {
$this->_encode_func($bin,3);
$bin = 0;
}
}
}
if($len%3 == 1) {
$bin = ord($str[$len-1]);
$bin = $bin << 4;
$this->_encode_func($bin,1);
$this->_encode_result .= '==';
} else if($len%3 == 2) {
$bin = ord($str[$len-2]);
$bin = $bin << 8;
$bin = $bin | ord($str[$len-1]);
$bin = $bin << 2;
$this->_encode_func($bin,2);
$this->_encode_result .= '=';
}
return $this->_encode_result;
}
private function _encode_func($bin,$bytes = 3) {
$num = 3;
$matches = 0;
$bits1 = ($num - $bytes) * 6;
$bits2 = $bytes * 6;
$matches = $this->_encode_data >> $bits1;
while( $matches ) {
$result = $bin & $matches;
$result = $result >> $bits2;
$bytes--;
$bits1 = ($num - $bytes) * 6;
$bits2 = $bytes * 6;
$matches = $this->_encode_data >> $bits1;
if($this->_debug) {
echo '$result = ',$result,' binary = ',decbin($result),"\n";
}
$this->_encode_result .= $this->_base64hash[$result];
}
}
public function decode($str) {
$bin = 0;
$length = strlen($str)-1;
$_decode_result = '';
$len = 0;
$i = 0;
while( ($len <= $length) ) {
$ch = $str[$len++];
if ($ch == '=') { // 当前一个字符是“=”号
/*
先说明一个概念:在解码时,4个字符为一组进行一轮字符匹配。
如果某一轮匹配的第二个是“=”且第三个字符不是“=”,说明这个带解析字符串不合法,直接返回空
*/
if ($str[$len] != '=' && (($i % 4) == 1)) {
return NULL;
}
continue;
}
$ch = $this->_DecodeTable[ord($ch)];
// 下面这连个条件,只有 ch < 0 会起作用,ch == -2 永远不会起作用,即所有不合法的字符均跳过。
if ($ch < 0 || $ch == -1) { /* a space or some other separator character, we simply skip over */
continue;
} else if ($ch == -2) {
return NULL;
}
switch($i % 4) {
case 0:
$bin = intval($ch) << 2;
break;
case 1:
$bin = intval($bin) | intval($ch) >> 4;
$_decode_result .= chr($bin);
$bin = ( intval($ch) & 0x0f ) << 4;
break;
case 2:
$bin = intval($bin) | intval($ch) >> 2;
$_decode_result .= chr($bin);
$bin = ( intval($ch) & 0x03 ) << 6;
break;
case 3:
$bin = intval($bin) | intval($ch);
$_decode_result .= chr($bin);
break;
}
$i++;
}
return $_decode_result;
}
public function debug($open = true) {
$this->_debug = $open;
}
}
版权声明
本文为[Mars慕容]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38324424/article/details/114820381
边栏推荐
- About DM Dameng database, SQL to obtain user table information, data table structure, data table creation statement, primary key and other information
- GeoServer 2.20.1 解决跨域问题 CORS
- db2相关操作知识点积累及WINDOWS环境DB2连接远程数据库实例
- Testing and Benchmarking
- 数据库死锁总结:(3.7-3.13)
- Login interface universal password bypass
- 空对象模式(3.14-3.20)
- Number
- JSON encoding and decoding
- URL Parsing
猜你喜欢

【C#】重塑矩阵(交错数组)

ELK日志分析系统的原理与介绍

【WPF】数据模板选择器DataTemplateSelector

Leetcode 1423.可获得的最大点数(Maximum Points You Can Obtain from Cards)

Use case diagram of umlet instructions

在vscode 中安装go插件并配置go环境以运行go

浏览器跨域问题小结

论文阅读:Supporting Early and Scalable Discovery of Disinformation Websites

命令模式(3.21-3.27)

2022.2.14-2.27 责任链模式
随机推荐
Accumulation of DB2 related operation knowledge points and instance of DB2 connecting to remote database in Windows Environment
禁用谷歌跨域的一个办法
Slice upload solves the problem of single file upload
Use case diagram of umlet instructions
线程安全的类的解析:(2.8-3.6)
Set up MySQL master-slave replication, read-write separation, one master and one slave
SHA256 Hashes
ELK日志分析系统的原理与介绍
使用C#,连接瀚高highgo数据库,获取用户数据表名、表结构、建表语名、主键
【WPF】数据模板选择器DataTemplateSelector
微信公众号查看uin,把它base64后即为biz
Sorting
远程唤醒服务器
@Slf4j注解中 log 报错
2020-12-21至2022-1-2:解析观察者模式
fiddler调换字体后界面缺少 恢复
空对象模式(3.14-3.20)
设置谷歌浏览器深色黑色背景
【C#】文件操作
IPV4-IGP