php base64 原理
#include <stdio.h>
#include <stdlib.h>
#include <string.h> static const char 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'
}; unsigned char *base64_encode(const unsigned char *str)
{
static const char base64_pad = '=';
const unsigned char *current = str;
int length = strlen(str);
unsigned char *p, *result; //检测 result = (unsigned char *)malloc(((length + ) / ) * * sizeof(char) + );
p = result; while(length > ){
*p++ = base64_table[current[] >> ];
*p++ = base64_table[((current[] & 0x03) << ) + (current[] >> )];
*p++ = base64_table[((current[] & 0x0f) << ) + (current[] >> )];
*p++ = base64_table[current[] & 0x3f]; current += ;
length -= ;
} if(length == ){
*p++ = base64_table[current[] >> ];
*p++ = base64_table[((current[] & 0x03) << ) + (current[] >> )];
*p++ = base64_table[(current[] & 0x0f) << ];
*p++ = base64_pad;
}else if(length == ){
*p++ = base64_table[current[] >> ];
*p++ = base64_table[(current[] & 0x03) << ];
*p++ = base64_pad;
*p++ = base64_pad;
} *p = '\0';
return result;
} static int base64_decode_map[] = {
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, // 0 - 15
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, // 16 - 31
-, -, -, -, -, -, -, -, -, -, -, , -, -, -, , // 32 - 47
, , , , , , , , , , -, -, -, -, -, -, // 48 - 63
-, , , , , , , , , , , , , , , , // 64 - 79
, , , , , , , , , , , -, -, -, -, -, // 80 - 95
-, , , , , , , , , , , , , , , , // 96 - 111
, , , , , , , , , , , -, -, -, -, -, // 112 - 127
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, // 128 - 143
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, // 144 - 159
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, // 160 - 175
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, // 176 - 191
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, // 192 - 207
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, // 208 - 223
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, // 224 - 239
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, // 240 - 255
}; char *base64_decode(const char *input, char *output)
{
output[] = '\0'; int input_len = strlen(input);
if(input_len < || input_len % != ){
return output;
} char *p = (char *)input;
char *p_out = output;
char *p_end = (char *)input + input_len; for(; p < p_end; p += ){
*p_out++ = ((base64_decode_map[p[]] << ) & 0xFC) | ((base64_decode_map[p[]] >> ) & 0x03);
*p_out++ = ((base64_decode_map[p[]] << ) & 0xF0) | ((base64_decode_map[p[]] >> ) & 0x0F);
*p_out++ = ((base64_decode_map[p[]] << ) & 0xC0) | ((base64_decode_map[p[]]));
} if(*(input + input_len - ) == '='){
*(p_out - ) = '\0';
}else if(*(input + input_len - ) == '='){
*(p_out - ) = '\0';
} return output;
} int main(){
unsigned char *ps = "";
unsigned char *ret = base64_encode(ps);
char output[];
memset(output, , sizeof output);
base64_decode(ret, output);
printf("%s, %s\n", ret, output);
}
php base64 原理的更多相关文章
- Base64原理
一.Base64编码由来 为什么会有Base64编码呢?因为有些网络传送渠道并不支持所有的字节,例如传统的邮件只支持可见字符的传送,像ASCII码的控制字符就 不能通过邮件传送.这样用途就受到了很大的 ...
- Base64原理解析
一. Base64编码由来 为什么会有Base64编码呢?因为有些网络传送渠道并不支持所有的字节,例如传统的邮件只支持可见字符的传送,像ASCII码的控制字符就 不能通过邮件传送.这样用途就受到了很大 ...
- Base64原理解析与使用
一.Base64编码由来 为什么会有Base64编码呢?因为有些网络传送渠道并不支持所有的字节,例如传统的邮件只支持可见字符的传送,像ASCII码的控制字符就 不能通过邮件传送.这样用途就受到了很大的 ...
- 一份简明的 Base64 原理解析
书接上回,在 记一个 Base64 有关的 Bug 一文里,我们说到了 Base64 的编解码器有不同实现,交叉使用它们可能引发的问题等等. 这一回,我们来对 Base64 这一常用编解码技术的原理一 ...
- base64原理,使用场景
Base64编码,是我们程序开发中经常使用到的编码方法.它是一种基于用64个可打印字符来表示二进制数据的表示方法.它通常用作存储.传输一些二进制数据编码方法!也是MIME(多用途互联网邮件扩展,主要用 ...
- Base64原理简介
Base64是一种编码方式,通常用于将二进制数据转换成可见字符的形式,该过程可逆. 过程大致如下: 1. 对64个可见字符,进行一个索引编码.索引是二进制的值,对应找到一个可见字符. Base64 编 ...
- Base64原理与实现
Base64编码说明 Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之后在6位的前面补两个0,形成8位一个字节的形式. 如果剩下的字符不足3个字节,则用0填充 ...
- base64 原理
Base64编码之所以称为Base64,是因为其使用64个字符来对任意数据进行编码,同理有Base32.Base16编码.标准Base64编码使用的64个字符为: 这64个字符是各种字符编码(比如AS ...
- Base64原理 bits 3->4 8bits/byte-->6bits/byte
实践: window.btoa('a')a YQ==abcdef YWJjZGVmabc YWJjab YWI= https://en.wikipedia.org/wiki/Base64 The Ba ...
随机推荐
- 自定义控件设置属性并实时展现并预览在xib中
关键字: // @IBDesignable:实时看到xib设置后的效果 // @IBInspectable:给xib提供设置属性,可以xib中看到此属性 场景: 自定义一个UITextField,并提 ...
- iOS 设置系统屏幕亮度
// 设置系统屏幕亮度 // [UIScreen mainScreen].brightness = value; // 或者 [[UIScreen mainScreen] se ...
- UIView上的按钮跳转到一个控制器UIViewController上去
我现在有一个UIControllerView 里面addView了一个UIView,我在点击UIView的时候转到另一个UIControllerView,按上面的导航条上面的返回按钮返回第一个UICo ...
- 世道变了 – 你愿意成为微软认证Linux工程师吗?
随笔世道变了 – 你愿意成为微软认证Linux工程师吗? 世道变了 – 你愿意成为微软认证Linux工程师吗? leixu十二月 14, 2015随笔 2015年12月9日,微软发布了全新的MCS ...
- Linux 折腾汇集,实时更新
一.Linux教程 入门教程:http://www.92csz.com/study/linux/ 命令大全:http://man.linuxde.net/ 一.界面: 在Ubuntu.Linux Mi ...
- Java虚拟机内存管理原理基础入门
Jdk:Java程序设计语言.Java虚拟机.Java API类库. Jdk是用于支持Java程序开发的最小环境. Jre:Java API类库中的Java SE API子集.Java虚拟机. Jre ...
- sed实例精解--例说sed完整版
原文地址:sed实例精解--例说sed完整版 作者:xiaozhenggang 最近在学习shell,怕学了后面忘了前面的就把学习和实验的过程记录下来了.这里是关于sed的,前面有三四篇分开的,现在都 ...
- CPU卡与M1卡的区别
简单来讲CPU卡比M1卡更加安全.扩展性更好.支持更多应用 CPU卡 M1 操作系统 带有COS系统 无COS系统 硬件加密模块 硬件DES运算模块 无实现算法的硬件加密模块 算法支持 标准DES ...
- 0020 Java学习笔记-面向对象-变量
变量分为哪些 成员变量:类里面,方法外面定义的变量 实例变量:没有用static修饰的变量,属于对象:存在期:创建实例-销毁实例:作用域:与该实例的生存范围相同 类变量:用static修饰的变量,属于 ...
- Linux学习--------一
用户不能直接操作Kemel,所以需要通过Shell来操作Kemel(内核) Shell 分为CLI与GUI两种 CLI:Command Line Interface GUI:Graphical Use ...