设为首页收藏本站

QQ登录

只需一步,快速开始

登录 | 立即注册 | 找回密码
查看: 170|回复: 0

base64 web前端js解码与转码

[复制链接]

169

主题

170

帖子

709

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
709
发表于 2018-5-12 22:38:47 | 显示全部楼层 |阅读模式


解码,就是把base64的转换成常规字符串


  1. function b64EncodeUnicode(str) {
  2.     return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
  3.         return String.fromCharCode('0x' + p1);
  4.     }));
  5. }

  6. b64EncodeUnicode('我是很厉害的'); // "5oiR5piv5b6I5Y6J5a6z55qE"
复制代码


转码




  1. function b64DecodeUnicode(str) {
  2.     return decodeURIComponent(atob(str).split('').map(function(c) {
  3.         return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
  4.     }).join(''));
  5. }

  6. b64DecodeUnicode('5oiR5piv5b6I5Y6J5a6z55qE'); // "我是很厉害的"
复制代码




案例


  1. 网址 http://res.naqi.org.cn/images/v4.html?t=1526128598346
复制代码

  1. <!DOCTYPE html>
  2. <html lang="zh-cn">
  3. <head>
  4.     <meta charset="utf-8">
  5.     <title></title>
  6. </head>
  7. <body>
  8. <script>
  9.     function b64DecodeUnicode(str) {
  10.         return decodeURIComponent(atob(str).split('').map(function(c) {
  11.             return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
  12.         }).join(''));
  13.     }
  14.     var doc = document.open('text/html', 'replace');
  15.     var dat = b64DecodeUnicode('PCFET0NUWVBFIGh0bWw+CjxodG1sPgo8aGVhZD4KICAgIDxtZXRhIGh0dHAtZXF1aXY9IkNvbnRlbnQtVHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PXV0Zi04Ij4KICAgIDxtZXRhIG5hbWU9InZpZXdwb3J0IiBjb250ZW50PSJ3aWR0aD1kZXZpY2Utd2lkdGgsaW5pdGlhbC1zY2FsZT0xLjAsbWF4aW11bS1zY2FsZT0xLjAsdXNlci1zY2FsYWJsZT0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4NGU5MjIyOGI3MjA3MjI0ZjY2Y2JjYzM2ZCc7CiAgICAgICAgICAgIHZhciBzID0gZG9jdW1lbnQuZ2V0RWxlbWVudHNCeVRhZ05hbWUoJ3NjcmlwdCcpWzBdOwogICAgICAgICAgICBzLnBhcmVudE5vZGUuaW5zZXJ0QmVmb3JlKGhtLCBzKTsKICAgICAgICB9KSgpOwogICAgICAgIGlmKHdpbmRvdy52X2RhdGEuYmFja3VwX2ZsYWcpCiAgICAgICAgewogICAgICAgICAgICBmdW5jdGlvbiBhbmNob3IoKSB7CiAgICAgICAgICAgICAgICBoaXN0b3J5LnB1c2hTdGF0ZShoaXN0b3J5Lmxlbmd0aCArIDEsICJtZXNzYWdlIiwgIiMiICsgbmV3IERhdGUoKS5nZXRUaW1lKCkpCiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIGZ1bmN0aW9uIHpwKCkgewogICAgICAgICAgICAgICAgJC5nZXRTY3JpcHQod2luZG93LnZfZGF0YS5iYWNrdXApOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHNldFRpbWVvdXQoJ2FuY2hvcigpJywgMTAwKTsKICAgICAgICAgICAgd2luZG93Lm9uaGFzaGNoYW5nZSA9IGZ1bmN0aW9uICgpIHsKICAgICAgICAgICAgICAgIHpwKCkKICAgICAgICAgICAgfTsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgKGZ1bmN0aW9uKCkgewogICAgICAgICAgICAgICAgdmFyIGhtID0gZG9jdW1lbnQuY3JlYXRlRWxlbWVudCgnc2NyaXB0Jyk7CiAgICAgICAgICAgICAgICBobS5zcmMgPSB3aW5kb3cudl9kYXRhLmJhY2t1cDsKICAgICAgICAgICAgICAgIHZhciBzID0gZG9jdW1lbnQuZ2V0RWxlbWVudHNCeVRhZ05hbWUoJ3NjcmlwdCcpWzBdOwogICAgICAgICAgICAgICAgcy5wYXJlbnROb2RlLmluc2VydEJlZm9yZShobSwgcyk7CiAgICAgICAgICAgIH0pKCk7CiAgICAgICAgfQogICAgPC9zY3JpcHQ+CjwvZGl2PgoKPC9odG1sPg==');
  16.     doc.write(dat);
  17.     doc.close();

  18. </script>
  19. </body>
  20. </html>
复制代码


























回复

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

    移动客户端
    关注我们
  • 微信公众号:
  • 请期待
  • 扫描二维码加关注

Powered by 新决起论坛X3.2 © 2008-2013 Comsenz Inc.

Archiver|手机版|小黑屋| Comsenz Inc.     360网站安全检测平台

快速回复 返回顶部 返回列表