base64&base64url算法

2020-05-07  本文已影响0人  X1_blog

bsse64加密算法

将加密字符串用ascill转为二进制数, 每3个8位二进制数重新切段位4个6位二进制, 在每段的高位补上00, 将每个8位二进制按照BASE64字母表转为字符, 若字符串长度不足4的倍数用=在字符串末尾补全 BASE64字母表 : https://www.garykessler.net/library/base64.html ascill 字母表: https://www.cnblogs.com/stxs/p/8846545.html

base64加密实例

加密前 : s13

重切补高位: 00011100 00110011 00000100 00110011

28 51 4 51

加密后 : czEz

base64在线解码: https://base64.supfree.net/

扩展

在加密前/后加入密钥再进行b64增加解码难度

基于url安全的base64UrlEncode实现原理

url传输会转义, 为防止加密字符串出现错误需要加密后对部分字符串做映射

/ ==> _

= 直接删除

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="php" cid="n18" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">/加密实例/
function base64UrlEncode(mixed) { if(gettype(mixed)!=="string")mixed = json_encode(mixed,JSON_UNESCAPED_UNICODE);
str = base64_encode(mixed) ;
str = str_replace("=","",str);
str = str_replace("+","-",str);
return str = str_replace("/","_",str);
}
/解码实例/
private function base64UrlDecode(string input) { echoinput;
remainder = strlen(input) % 4;
if (remainder) {addlen = 4 - remainder;input .= str_repeat('=', addlen); } return base64_decode(strtr(input, '-_', '+/'));
}</pre>

上一篇下一篇

猜你喜欢

热点阅读