zoukankan      html  css  js  c++  java
  • base64解密加密中文乱码解决

    let Base64 = {
        encode(str) {
            // first we use encodeURIComponent to get percent-encoded UTF-8,
            // then we convert the percent encodings into raw bytes which
            // can be fed into btoa.
            return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
                function toSolidBytes(match, p1) {
                    return String.fromCharCode('0x' + p1);
                }));
        },
        decode(str) {
            // Going backwards: from bytestream, to percent-encoding, to original string.
            return decodeURIComponent(atob(str).split('').map(function (c) {
                return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
            }).join(''));
        }
    };
    
    let encoded = Base64.encode("哈ha"); // "5ZOIaGE="
    let decoded = Base64.decode(encoded); // "哈ha"
    

      react 中使用

    新建文件base.js
    
    // base64解密加密中文乱码解决
    let Base64 = {
        encode(str) {
            // first we use encodeURIComponent to get percent-encoded UTF-8,
            // then we convert the percent encodings into raw bytes which
            // can be fed into btoa.
            return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
                function toSolidBytes(match, p1) {
                    return String.fromCharCode('0x' + p1);
                }));
        },
        decode(str) {
            // Going backwards: from bytestream, to percent-encoding, to original string.
            return decodeURIComponent(atob(str).split('').map(function (c) {
                return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
            }).join(''));
        }
    };
    
    export default Base64
    

      在组件中使用

    //  手动引入base64解密加密中文乱码解决
    import Base64 from "../ways/basecode";


    let encoded = Base64.encode("哈ha"); // "5ZOIaGE="
    let decoded = Base64.decode(encoded); // "哈ha"

      

  • 相关阅读:
    Java通过JNI调用C/C++
    Using HTML5 audio and video
    vmstat输出项解释
    uva 11237
    NN优化方法对照:梯度下降、随机梯度下降和批量梯度下降
    认识与学习bash
    系统崩溃,大圣归来
    连载《一个程序员的生命周期》-25.到工业现场学习业务知识引发的思考
    ZOJ问题(2010浙江大学研究生复试上机题目[找规律] hdoj 3788)
    UIView的几个枚举定义
  • 原文地址:https://www.cnblogs.com/taxun/p/13397727.html
Copyright © 2011-2022 走看看