微信小程序-base64加解密

思路:先创建一个base64.js的文件,这个文件可以作为专门加解密的文件模块,需要时就引用;创建好后,引用base64.js里的加解密函数。

注意:引用模块一定要引用正确的路径,否则会报错。

base64.js:

复制代码
// 实现Base64加密
function base64Encode(str) {
  let base64 = new Base64();
  return base64.encode(str);
}

// 实现Base64解密
function base64Decode(str) {
  let base64 = new Base64();
  return base64.decode(str);
}

// 定义Base64对象
function Base64() {

  // Base64字符集
  const base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

  // 编码函数
  this.encode = function (str) {
    let result = '';
    for (let i = 0; i < str.length; i += 3) {
      let a = str.charCodeAt(i);
      let b = i + 1 < str.length ? str.charCodeAt(i + 1) : 0;
      let c = i + 2 < str.length ? str.charCodeAt(i + 2) : 0;

      let a1 = a >> 2, a2 = ((a & 3) << 4) | (b >> 4), a3 = ((b & 15) << 2) | (c >> 6), a4 = c & 63;

      result += base64Chars[a1] + base64Chars[a2] + (i + 1 < str.length ? base64Chars[a3] : '=') + (i + 2 < str.length ? base64Chars[a4] : '=');
    }
    return result;
  }

  // 解码函数
  this.decode = function (str) {
    let result = '';
    let i = 0;
    while (i < str.length) {
      let a = base64Chars.indexOf(str.charAt(i++));
      let b = base64Chars.indexOf(str.charAt(i++));
      let c = base64Chars.indexOf(str.charAt(i++));
      let d = base64Chars.indexOf(str.charAt(i++));

      let a1 = (a << 2) | (b >> 4);
      let a2 = ((b & 15) << 4) | (c >> 2);
      let a3 = ((c & 3) << 6) | d;

      result += String.fromCharCode(a1);
      if (c != 64) {
        result += String.fromCharCode(a2);
      }
      if (d != 64) {
        result += String.fromCharCode(a3);
      }
    }
    return result;
  }
}

// 向外暴露方法
module.exports = {
  base64Encode: base64Encode,
  base64Decode: base64Decode
}

在待加解密文件中,引用base64.js模块

复制代码
const base64 = require('./base64');

      //从缓存中取出token
      let tokened = wx.getStorageSync('token');
      console.log("tokened:",tokened);
      //对token进行处理,解析token,因为设置原因,我的token解码位置特殊
      // 进行分割+格式化
      let userinfo = tokened.split('-')[1];
      console.log("userinfo-token》》》》》",userinfo)
      // 解码base64
      let rawStr= base64.base64Decode(userinfo);
      //var data= JSON.parse(rawStr);
      console.log('base64解码后的字符串: ',rawStr);
      //截取解码后的字符串
      let rawObj = rawStr.slice(0,-2);
      console.log('字符串转为数组: ',JSON.parse(rawObj));
      let QEUID = JSON.parse(rawObj).UID;
      console.log("用户ID:"+QEUID);

最终结果

参考:https://juejin.cn/post/7229512717135527991

PHP中 base64_decode与base64_encode加密解密函数

复制代码
base64_encode是加密
base64_encode    语法:string base64_encode(string data);
 
$string='www.zhix.net智昕网络'; //定义字符串
 
echo base64_encode($string);  // 输出编码后的内容为 d3d3LnpoaXgubmV05pm65piV572R57uc


base64_decode是解密 
base64_decode    语法:string base64_decode(string data);
 
$string='d3d3LnpoaXgubmV05pm65piV572R57uc';     //定义字符串
 
echo base64_decode($string); //输出解码后的内容 www.zhix.net智昕网络

参考:https://blog.csdn.net/fujian9544/article/details/111590073

相关推荐
WangHappy2 天前
不写 Canvas 也能搞定!小程序图片导出的 WebView 通信方案
前端·微信小程序
小时前端2 天前
微信小程序选不了本地文件?用 web-view + H5 一招搞定
前端·微信小程序·uni-app
icebreaker3 天前
Weapp-vite:原生模式之外,多一种 Vue SFC 选择
前端·vue.js·微信小程序
icebreaker3 天前
重走 Vue 长征路 Weapp-vite:编译链路与 Wevu 运行时原理拆解
前端·vue.js·微信小程序
大米饭消灭者6 天前
Taro是怎么实现一码多端的【底层原理】
微信小程序·taro
FliPPeDround7 天前
Vitest Environment UniApp:让 uni-app E2E 测试变得前所未有的简单
微信小程序·e2e·前端工程化
FliPPeDround7 天前
微信小程序自动化的 AI 新时代:wechat-devtools-mcp 智能方案
微信小程序·ai编程·mcp
吴声子夜歌7 天前
小程序——布局示例
小程序
码云数智-大飞7 天前
如何创建自己的小程序,码云数智与有赞平台对比
微信小程序
luffy54597 天前
微信小程序页面使用类似filter函数的wxs语法
微信小程序·小程序