zoukankan      html  css  js  c++  java
  • Base64中文不能加密问题

      最近用到了Base64.js来对url参数进行加密,字母和数字都可以很好地加密/解密。 但测试中文时发现不能进行转换,貌似Base64.js不支持中文字符。

      联想到encodeURI()对url的编码/解码,但encodeURI、decodeURI只能对汉字进行转换,不支持字母和数字。于是想能不能先用encodeURI编码 再用base64加密; 得到密钥先用base64解密,再用decodeURI解码。经过测试  成功了!从而实现了对数字、字母、汉字的加密解密。下面为测试代码:

    <script type="text/javascript" src="js/Base64.js"></script>
    <script type="text/javascript">
            
            function bian(){
                var val = document.getElementById("bb").value;
                var com= base64encode(encodeURI(val));
                var res = document.getElementById("result");
                res.value=com;
            }
            function jie(){
                var val = document.getElementById("bb").value;
                var com=  decodeURI(base64decode(val)) ;
                //document.write(com);
                var res = document.getElementById("result");
                res.value=com;
            }
        </script>
    <input type="text" id="bb"/>
    <input type="button" value="编码" onclick="bian()">
    <input type="button" value="解码" onclick="jie()"> <br/>
    <input type="text" id="result" >
    作者:oz
    出处:http://www.cnblogs.com/kakarottoz/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    with open 向文件的某一固定行,追加内容
    静态语言 与 动态语言 的区别
    ELK
    matplotlib绘图
    django用户认证
    django+uwsgi+nginx 部署生产环境
    图片验证码+session
    ajax
    form
    middleware中间件
  • 原文地址:https://www.cnblogs.com/kakarottoz/p/4986666.html
Copyright © 2011-2022 走看看