zoukankan      html  css  js  c++  java
  • 利用JavaScript函数对字符串进行加密

    加密、解密问题对我来说一直是很神秘的,感到神奇无比。

    • 理论了解

    前段时间看到关于利用JavaScript函数unescape()和escape()对字符串进行替换处理。通过查资料得知,

    escape()函数会对非基本ASCII码的字符(即非数字、英文大小写)改为用%开头的十六进制码替换。

    同时的知,一个很重要的消息——

    “ECMAScript v3反对使用该方法,应用decodeURI()和decodeURIComponent()替代escape()”

    那么又有新的问题,decodeURI()和decodeURIComponent()又有什么区别???

    定义与用法

    encodeURIComponent()函数可把字符串作为URI组件进行编码;

    encodeURI()函数可把字符串作为URI进行编码;

    通过对比,我们可以得知,两个函数都是把字符串当作URI对象来进行编码,不同的是encodeURIComponent()假定它的进行编码的字符串是 URI 的一部分(比如协议、主机名、路径或查询字符串)。因此 encodeURIComponent() 函数将转义用于分隔 URI 各个部分的标点符号。

    • 实际应用

    对网页源文件进行加密与解密。

    加密与解密处理的编译工具

    image

    image

    实现代码:

    <h2>
    encodeURIComponent函数编码
    </h2>
    <hr/>
    <textarea rows="10" cols="20" placeholder="请输入你要加密的信息!" id="importCon">
    </textarea>
    <button onclick="encode()">编码</button>
    <textarea rows="10" cols="20" placeholder="请输入你要解密的信息" id="exportCon">
    </textarea>
    <button onclick="decode()">解码</button>
    <script>
    //理解value、innerText
    function encode() {
    document.getElementById("exportCon").innerText = encodeURIComponent(document.getElementById("importCon").value);
    }
    function decode() {
    document.getElementById("importCon").innerText = decodeURIComponent(document.getElementById("exportCon").value);
    }
    </script>
  • 相关阅读:
    bzoj 1856 组合
    bzoj 2809 左偏树平衡树启发式合并
    【HMOI】小C的填数游戏 DP+线段树维护
    【HNOI】 小A的树 tree-dp
    bzoj 1483 链表启发式合并
    bzoj 2733 平衡树启发式合并
    bzoj 2669 状压DP
    bzoj 2165 DP
    【HNOI】 lct tree-dp
    远程debug配置
  • 原文地址:https://www.cnblogs.com/Jener/p/6110897.html
Copyright © 2011-2022 走看看