欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS实现的base64加密解密操作示例

程序员文章站 2023-03-14 10:39:10
本文实例讲述了js实现的base64加密解密操作。分享给大家供大家参考,具体如下: &l...

本文实例讲述了js实现的base64加密解密操作。分享给大家供大家参考,具体如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>js base64加密解密</title>
</head>
<body>
<script>
// 创建base64对象
var base64 = {
 _keystr: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/=",
 encode: function(e) {
  var t = "";
  var n, r, i, s, o, u, a;
  var f = 0;
  e = base64._utf8_encode(e);
  while (f < e.length) {
   n = e.charcodeat(f++);
   r = e.charcodeat(f++);
   i = e.charcodeat(f++);
   s = n >> 2;
   o = (n & 3) << 4 | r >> 4;
   u = (r & 15) << 2 | i >> 6;
   a = i & 63;
   if (isnan(r)) {
    u = a = 64
   } else if (isnan(i)) {
    a = 64
   }
   t = t + this._keystr.charat(s) + this._keystr.charat(o) + this._keystr.charat(u) + this._keystr.charat(a)
  }
  return t
 },
 decode: function(e) {
  var t = "";
  var n, r, i;
  var s, o, u, a;
  var f = 0;
  e=e.replace(/[^a-za-z0-9+/=]/g,"");
  while (f < e.length) {
   s = this._keystr.indexof(e.charat(f++));
   o = this._keystr.indexof(e.charat(f++));
   u = this._keystr.indexof(e.charat(f++));
   a = this._keystr.indexof(e.charat(f++));
   n = s << 2 | o >> 4;
   r = (o & 15) << 4 | u >> 2;
   i = (u & 3) << 6 | a;
   t = t + string.fromcharcode(n);
   if (u != 64) {
    t = t + string.fromcharcode(r)
   }
   if (a != 64) {
    t = t + string.fromcharcode(i)
   }
  }
  t = base64._utf8_decode(t);
  return t
 },
 _utf8_encode: function(e) {
  e = e.replace(/rn/g, "n");
  var t = "";
  for (var n = 0; n < e.length; n++) {
   var r = e.charcodeat(n);
   if (r < 128) {
    t += string.fromcharcode(r)
   } else if (r > 127 && r < 2048) {
    t += string.fromcharcode(r >> 6 | 192);
    t += string.fromcharcode(r & 63 | 128)
   } else {
    t += string.fromcharcode(r >> 12 | 224);
    t += string.fromcharcode(r >> 6 & 63 | 128);
    t += string.fromcharcode(r & 63 | 128)
   }
  }
  return t
 },
 _utf8_decode: function(e) {
  var t = "";
  var n = 0;
  var r = c1 = c2 = 0;
  while (n < e.length) {
   r = e.charcodeat(n);
   if (r < 128) {
    t += string.fromcharcode(r);
    n++
   } else if (r > 191 && r < 224) {
    c2 = e.charcodeat(n + 1);
    t += string.fromcharcode((r & 31) << 6 | c2 & 63);
    n += 2
   } else {
    c2 = e.charcodeat(n + 1);
    c3 = e.charcodeat(n + 2);
    t += string.fromcharcode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
    n += 3
   }
  }
  return t
 }
}
// 定义字符串
var string = 'hello world!';
// 加密
var encodedstring = base64.encode(string);
console.log(encodedstring); // 输出: "sgvsbg8gv29ybgqh"
// 解密
var decodedstring = base64.decode(encodedstring);
console.log(decodedstring); // 输出: "hello world!"
</script>
</body>
</html>

运行结果:

JS实现的base64加密解密操作示例

ps:这里再推荐几款加密解密相关在线工具供大家参考使用:

在线编码转换工具(utf-8/utf-32/punycode/base64):

base64编码解码工具:

图片转换为base64编码在线工具:

更多关于javascript相关内容可查看本站专题:《javascript加密解密技巧汇总》、《javascript查找算法技巧总结》、《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》、《javascript遍历算法与技巧总结》及《javascript数学运算用法总结

希望本文所述对大家javascript程序设计有所帮助。