工作需要,想弄一个加密的串,就想仿照base64的编码方式,写一个加密的方法,所以就有了下面这个用php实现的base64的代码

<?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;
}
}

上面有一个简单的debug开关,是刚开始写代码时自己调试用的,感觉还不错,就当是做个记录吧。

参考:

http://www.cnblogs.com/chengxiaohui/articles/3951129.html

php实现base64编码的更多相关文章

  1. URL安全的Base64编码

    Base64编码可用于在HTTP环境下传递较长的标识信息.在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式.此时,采用Base64编码不仅比较简短,同时也具有不可 ...

  2. Base64编码

    Base64编码 写在前面 今天在做一个Android app时遇到了一个问题:Android端采用ASE对称加密的数据在JavaWeb(jre1.8.0_7)后台解密时,居然解密失败了!经过测试后发 ...

  3. Android数据加密之Base64编码算法

    前言: 前面学习总结了平时开发中遇见的各种数据加密方式,最终都会对加密后的二进制数据进行Base64编码,起到一种二次加密的效果,其实呢Base64从严格意义上来说的话不是一种加密算法,而是一种编码算 ...

  4. 网络安全——Base64编码、MD5、SHA1-SHA512、HMAC(SHA1-SHA512)哈希

    据说今天520是个好日子,为什么我想起的是502.500.404这些?还好服务器没事! 一.Base64编码 Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之 ...

  5. Base64编码【转】

    转http://www.cnblogs.com/luguo3000/p/3940197.html 开发者对Base64编码肯定很熟悉,是否对它有很清晰的认识就不一定了.实际上Base64已经简单到不能 ...

  6. 【前端攻略】:玩转图片Base64编码

    引言 图片处理在前端工作中可谓占据了很重要的一壁江山.而图片的 base64 编码可能相对一些人而言比较陌生,本文不是从纯技术的角度去讨论图片的 base64 编码.标题略大,不过只是希望通过一些浅显 ...

  7. 为什么要用base64编码

    1.需求 了解为什么要使用base64对数据编码 2.理由 因为传输二进制数据的时候,网络中间的有些路由会把ascii码中的不可见字符删了,导致数据不一致.一般也会对url进行base64编码 Whe ...

  8. 图片Base64编码

    我们经常在做Jquery插件的时候需要插入一些自定义的控件,比如说按钮,而我们自己又觉着button标签很丑,而且不同浏览器显示的效果还不一样,这个时候我们需要用到图片,当然,我们可以通过img标签添 ...

  9. 浅析用Base64编码的图片优化网页加载速度

    想必大家都知道网页加载的过程,从开始请求,到加载页面,开始解析和显示网页,遇到图片就再次向服务器发送请求,加载图片.如果图片很多的话,就会产生大量的http请求,从而影响页面的加载速度.所以现在有一种 ...

  10. Base64编码原理分析

    Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,在了解Base64编码之前,先了解几个基本概念:位.字节. 位:"位(bit)"是计算机中最小的数据单位.每一位 ...

随机推荐

  1. python 写的http后台弱口令爆破工具

    今天来弄一个后台破解的Python小程序,哈哈,直接上代码吧,都有注释~~ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ...

  2. llvm學習(二)————llvm編譯與環境構建

    本文由博主原创,转载请注明出处(保留此处和链接): IT人生(http://blog.csdn.net/robinblog/article/details/17339027) 在2011十月份的时候, ...

  3. 【设计模式 - 11】之享元模式(FlyWeight)

    1      模式简介 当系统中存在大量对象时,非常容易造成内存溢出.为了解决这个问题,我们把这些对象中共有的部分抽象出来,如果有相同的业务请求,则直接返回在内存中已有的对象,避免重新创建,这就是享元 ...

  4. 【Android - 框架】之OkHttp的使用

    OkHttp是一个非常优秀的网络访问框架,当下非常火的Retrofit的底层就是使用OkHttp进行封装的.接下来介绍以下OkHttp的简单使用. 1.导入依赖 在Android Studio中,在M ...

  5. winform渐变窗口显示/关闭

    //渐渐的消失 for (int iNum = 10; iNum >= 0; iNum --) { //变更窗体的不透明度 this.Opacity = 0.1 * iNum; //暂停 Sys ...

  6. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(30)-本地化(多语言)

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(30)-本地化(多语言) 我们的系统有时要扩展到其他国家,或者地区,需要更多的语言环境,微软提供了一些解决 ...

  7. TFS(Team Foundation Server)介绍和入门

    在本文的两个部分中,我将介绍Team Foundation Server的一些核心特征,重点介绍在本产品的日常应用中是怎样将这些特性结合在一起使用的. 作为一名软件开发者,在我的职业生涯中,我常常会用 ...

  8. JavaCodeTra 猴子选猴王 约瑟夫循环

    之前用的是循环链表,java刚学,不知道怎么用链表.用个小算法吧 代码: import java.util.Scanner; /** * */ /** * @author john * @约瑟夫循环/ ...

  9. [AngularJS + Webpack] Uglifying your JavaScript

    Angular requires some careful consideration when uglifying your code because of how angular's depend ...

  10. Qt解析XML文件(QXmlStreamReader)

    (2013-08-03 10:53:53) 转载▼       如何使用QXmlStreamReader来解析格式良好的XML,Qt的文档中指出,它是一种更快.更方便的Qt自己的SAX解析器(QXml ...