什么是Base64?
Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。可查看RFC2045~RFC2049,上面有MIME的详细规范。 Base64编码是从二进制到字符的过程,可用于在HTTP环境下传递较长的标识信息。采用Base64编码具有不可读性,需要解码后才能阅读。
标准的Base64并不适合直接放在URL里传输,因为URL编码器会把标准Base64中的“/”和“+”字符变为形如“%XX”的形式,而这些“%”号在存入数据库时还需要再进行转换,因为ANSI SQL中已将“%”号用作通配符。
为解决此问题,可采用一种用于URL的改进Base64编码,它在末尾填充'='号,并将标准Base64中的“+”和“/”分别改成了“-”和“_”,这样就免去了在URL编解码和数据库存储时所要作的转换,避免了编码信息长度在此过程中的增加,并统一了数据库、表单等处对象标识符的格式。
另有一种用于正则表达式的改进Base64变种,它将“+”和“/”改成了“!”和“-”,因为“+”,“*”以及前面在IRCu中用到的“[”和“]”在正则表达式中都可能具有特殊含义。
Base64要求把每三个8Bit的字节转换为四个6Bit的字节(3*8 = 4*6 = 24),然后把6Bit再添两位高位0,组成四个8Bit的字节,也就是说,转换后的字符串理论上将要比原来的长1/3。
规则
关于这个编码的规则:
①.把3个字节变成4个字节。
②每76个字符加一个换行符。
③.最后的结束符也要处理。
JavaScript版实现
if (!Shotgun)
var Shotgun = {};
if (!Shotgun.Js)
Shotgun.Js = {};
Shotgun.Js.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', '+', '/'
],
encode: function (bin) {
var codes = [];
var un = 0;
un = bin.length % 3;
if (un == 1)
bin.push(0, 0);
else if (un == 2)
bin.push(0);
for (var i = 2; i < bin.length; i += 3) {
var c = bin[i - 2] << 16;
c |= bin[i - 1] << 8;
c |= bin[i];
codes.push(this._table[c >> 18 & 0x3f]);
codes.push(this._table[c >> 12 & 0x3f]);
codes.push(this._table[c >> 6 & 0x3f]);
codes.push(this._table[c & 0x3f]);
}
if (un >= 1) {
codes[codes.length - 1] = "=";
bin.pop();
}
if (un == 1) {
codes[codes.length - 2] = "=";
bin.pop();
}
return codes.join("");
},
decode: function (base64Str) {
var i = 0;
var bin = [];
var x = 0, code = 0, eq = 0;
while (i < base64Str.length) {
var c = base64Str.charAt(i++);
var idx = this._table.indexOf(c);
if (idx == -1) {
switch (c) {
case '=': idx = 0; eq++; break;
case ' ':
case '\n':
case "\r":
case '\t':
continue;
default:
throw { "message": "\u0062\u0061\u0073\u0065\u0036\u0034\u002E\u0074\u0068\u0065\u002D\u0078\u002E\u0063\u006E\u0020\u0045\u0072\u0072\u006F\u0072\u003A\u65E0\u6548\u7F16\u7801\uFF1A" + c };
}
}
if (eq > 0 && idx != 0)
throw { "message": "\u0062\u0061\u0073\u0065\u0036\u0034\u002E\u0074\u0068\u0065\u002D\u0078\u002E\u0063\u006E\u0020\u0045\u0072\u0072\u006F\u0072\u003A\u7F16\u7801\u683C\u5F0F\u9519\u8BEF\uFF01" };
code = code << 6 | idx;
if (++x != 4)
continue;
bin.push(code >> 16);
bin.push(code >> 8 & 0xff);
bin.push(code & 0xff)
code = x = 0;
}
if (code != 0)
throw { "message": "\u0062\u0061\u0073\u0065\u0036\u0034\u002E\u0074\u0068\u0065\u002D\u0078\u002E\u0063\u006E\u0020\u0045\u0072\u0072\u006F\u0072\u003A\u7F16\u7801\u6570\u636E\u957F\u5EA6\u9519\u8BEF" };
if (eq == 1)
bin.pop();
else if (eq == 2) {
bin.pop();
bin.pop();
} else if (eq > 2)
throw { "message": "\u0062\u0061\u0073\u0065\u0036\u0034\u002E\u0074\u0068\u0065\u002D\u0078\u002E\u0063\u006E\u0020\u0045\u0072\u0072\u006F\u0072\u003A\u7F16\u7801\u683C\u5F0F\u9519\u8BEF\uFF01" };
return bin;
}
};
Java版实现
import java.util.Base64;
//对于标准的Base64:
//加密为字符串使用
Base64.getEncoder().encodeToString();
//加密为字节数组使用
Base64.getEncoder().encode();
//解密使用
Base64.getDecoder().decode();
python版实现
def base(string:str)->str:
oldstr = ''
newstr = []
base = ''
base64_list = ['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', '+', '/']
#把原始字符串转换为二进制,用bin转换后是0b开头的,所以把b替换了,首位补0补齐8位
for i in string:
oldstr += '{:08}'.format(int(str(bin(ord(i))).replace('0b', '')))
#把转换好的二进制按照6位一组分好,最后一组不足6位的后面补0
for j in range(0, len(oldstr), 6):
newstr.append('{:<06}'.format(oldstr[j:j + 6]))
#在base_list中找到对应的字符,拼接
for l in range(len(newstr)):
base += base64_list[int(newstr[l], 2)]
#判断base字符结尾补几个‘=’
if len(string) % 3 == 1:
base += '=='
elif len(string) % 3 == 2:
base += '='
return base
C#版实现
直接使用.NET中的的库类函数
方法:
///<summary>
///Base64加密
///</summary>
///<paramname="Message"></param>
///<returns></returns>
publicstringBase64Code(stringMessage)
{
byte[]bytes=Encoding.Default.GetBytes(Message);
returnConvert.ToBase64String(bytes);
}
///<summary>
///Base64解密
///</summary>
///<paramname="Message"></param>
///<returns></returns>
publicstringBase64Decode(stringMessage)
{
byte[]bytes=Convert.FromBase64String(Message);
returnEncoding.Default.GetString(bytes);
}
BASH版实现
base64Table=(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 + /);
function str2binary() {
idx=0;
for((i=0; i<${#str}; i++)); do
dividend=$(printf "%d" "'${str:i:1}");
for((j=0;j<8;j++)); do
let idx=8*i+7-j;
let bin[$idx]=$dividend%2;
dividend=$dividend/2;
done;
done;
let idx=${#str}*8;
for((i=0; i<appendEqualCnt*2; i++)); do
let bin[$idx]=0;
let idx++;
done;
}
function calcBase64() {
for((i=0; i<${#bin[*]}/6; i++)); do
sum=0;
for((j=0; j<6; j++)); do
let idx=i*6+j;
let n=6-1-j;
let sum=sum+${bin[$idx]}*2**n;
done;
echo -n ${base64Table[$sum]};
done
}
declare -a bin
function base64Encode() {
read -p "please enter ASCII string:" str;
let appendZero=${#str}*8%6;
let bits=${#str}*8;
appendEqualCnt=0;
if [[ $appendZero -ne 0 ]]; then
let appendEqualCnt=(6-$appendZero)/2;
fi
str2binary;
calcBase64;
if [[ $appendEqualCnt -eq 2 ]]; then
echo -n "==";
elif [[ $appendEqualCnt -eq 1 ]]; then
echo -n "=";
fi
echo;
}
base64编码使用场景:
比如将图片等资源文件以Base64编码形式直接放于代码中,使用的时候反Base64后转换成Image对象使用;有些文本协议不支持不可见字符的传递,只能转换成可见字符来传递信息。有时在一些特殊的场合,大多数消息是纯文本的,偶尔需要用这条纯文本通道传一张图片之类的情况发生的时候,就会用到Base64,比如多功能Internet 邮件扩充服务(MIME)就是用Base64对邮件的附件进行编码的。
个人理解,base64这个加密是为了传输,而不是安全,可以用点小聪明在base64一次后前后再接点自己算法的字符串,加作一次或两次base64。那么一般人破译也有点难度。欣赏下base64加解密如下: