base64加密解密过程

Base64会使用一串固定编码: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

想要使用Base64加密,原文长度必须是3的倍数。如果原文不是3的倍数就填充其他符号来凑足成3的倍数;

base64加密解密实现

主要是对字符串的处理,所以对字符串相关的方法和属性一定要熟练;

javascript原生方法

手写封装

//在封装公共方法的文件夹下新建base.js文件
//我习惯写类(函数也可以)
export class Base64{
  constructor() {
    //固定编码
    this.keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  }
  //加密
  encode(str) {
    //先将字符串UTF-8编码
    str = this._utf8_encode(str)
    let res = ""
    let i = 0
    let char1, char2, char3, enc1, enc2, enc3, enc4;
    while (i < str.length) {
      char1 = str.charCodeAt(i++);
      char2 = str.charCodeAt(i++);
      char3 = str.charCodeAt(i++);
      enc1 = char1 >> 2;
      enc2 = ((char1 & 3) << 4) | (char2 >> 4);
      enc3 = ((char2 & 15) << 2) | (char3 >> 6);
      enc4 = char3 & 63;
      if (isNaN(char2)) {
        enc3 = enc4 = 64;
      } else if (isNaN(char3)) {
        enc4 = 64;
      }
      res = res +
        this.keyStr.charAt(enc1) + this.keyStr.charAt(enc2) +
        this.keyStr.charAt(enc3) + this.keyStr.charAt(enc4);
    }
    return res;
  }
  //解密
  decode(str) {
    str = str.replace(/[^A-Za-z0-9\+\/\=]/g, "");
    let res = ""
    let i = 0
    let char1, char2, char3, enc1, enc2, enc3, enc4;
    while (i < str.length) {
      enc1 = this.keyStr.indexOf(str.charAt(i++));
      enc2 = this.keyStr.indexOf(str.charAt(i++));
      enc3 = this.keyStr.indexOf(str.charAt(i++));
      enc4 = this.keyStr.indexOf(str.charAt(i++));
      char1 = (enc1 << 2) | (enc2 >> 4);
      char2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      char3 = ((enc3 & 3) << 6) | enc4;
      res = res + String.fromCharCode(char1);
      if (enc3 != 64) {
        res = res + String.fromCharCode(char2);
      }
      if (enc4 != 64) {
        res = res + String.fromCharCode(char3);
      }
    }
    //最后将字符串UTF-8解码
    res = this._utf8_decode(res)
    return res;
  }
  //UTF-8编码
  _utf8_encode(str) {
    //回车换行符替换为换行符
    str = str.replace(/\r\n/g, "\n")
    //加密后的返回结果是一个字符串
    let res = ""
    for (let i = 0; i < str.length; i++){
      //字符串方法 charCodeAt() 用于检索字符串中特定位置的字符的Unicode值,返回一个UTF-16值(一个介于 0-65535之间的16位整数;
      //字符串方法fromCharCode() 用于从给定的Unicode获取字符;
      //位运算
      // >> :(左移)各二进位全部左移若干位,高位丢弃,低位补0;
      // & :(与)两个位都为1时,结果才为1;
      // | :(或)两个位都为0时,结果才为0;
      let char = string.charCodeAt(i);
      switch (char) {
        case char < 128:
          res += String.fromCharCode(char)
          break;
        case ((char > 127) && (char <2048)) :
          res += String.fromCharCode((char >> 6) | 192)
          res += String.fromCharCode((char & 63) | 128)
          break;
        default:
          res += String.fromCharCode((char >> 12) | 224)
          res += String.fromCharCode(((char >> 6) & 63) | 128)
          res += String.fromCharCode((char & 63) | 128)
          break;
      }
    }
    return res
  }
  //UTF-8解码
  _utf8_decode(str) {
    //解密后的返回结果是一个字符串
    let res = ""
    let i = 0;
    let char = 0;
    let char1 = 0;
    let char2 = 0;
    while (i < str.length) {
      char = str.charCodeAt(i);
      switch (char) {
        case char < 128:
          res += String.fromCharCode(char);
          i++;
          break;
        case  ((char > 191) && (char < 224)):
          char1 = str.charCodeAt(i + 1);
          res += String.fromCharCode(((char & 31) << 6) | (char1 & 63));
          i += 2;
          break;
        default:
          char1 = str.charCodeAt(i + 1);
          char2 = str.charCodeAt(i + 2);
          res += String.fromCharCode(((char & 15) << 12) | ((char1 & 63) << 6) | (char2 & 63));
          i += 3;
          break;
      }
    }
    return res
  }
}
//在使用的地方引入
import { Base64 } from '../../utils/base.js'
//使用
let str = '是你好不是hello'
let base64 = new Base64()
let encodeRes = base64.encode(str);
let decodeRes = base64.decode(encodeRes);