Home > Web Front-end > Front-end Q&A > What are the JavaScript encryption methods?

What are the JavaScript encryption methods?

青灯夜游
Release: 2023-01-05 16:11:16
Original
7471 people have browsed it

Encryption methods include: 1. Use the "window.btoa(value)" statement to encrypt; 2. Use hex_md5(), b64_md5(), str_md5() and other functions to encrypt; 3. Use sha1() function to encrypt; 4. Use the escape() function to encrypt; 5. Use AES to encrypt.

What are the JavaScript encryption methods?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

1. Base64 encryption

Example

<html>
    <head>
        <title>前端的base64使用方法</title>
    </head>
    <body>
    </body>
<script>
var str = "hello";
var str64 = window.btoa("hello");
console.log("字符串是:"+str);
console.log("经base64编码后:"+str64);
console.log("base64解码后:"+window.atob(str64));
</script>
</html>
Copy after login

2. MD5 encryption (irreversible)

About MD5:
MD5.js is a tool that encrypts user information, passwords and other private information through front-end js encryption. It can also be called a plug-in.

MD5 has 6 encryption methods:
1, hex_md5(value)
2, b64_md5(value)
3, str_md5(value)
4 , hex_hmac_md5(key, data)
5, b64_hmac_md5(key, data)
6, str_hmac_md5(key, data)

md5 code download address - click here to download

After downloading, use the script tag to import and use

<script src="md5/md5.js"></script>"></script>
    <script>
        var code = "123456";
        var username = "123456";
        var password = "123456";
        var str1 = hex_md5("123456");
        var str2 = b64_md5("123456");
        var str3 = str_md5("123456");
        var str4 = hex_hmac_md5(code,code);
        var str5 = b64_hmac_md5(username,username);
        var str6 = str_hmac_md5(password,password);
        console.log(str1);            // e10adc3949ba59abbe56e057f20f883e
        console.log(str2);            // 4QrcOUm6Wau+VuBX8g+IPg
        console.log(str3);            // áÜ9IºY«¾VàWò��>
        console.log(str4);            // 30ce71a73bdd908c3955a90e8f7429ef
        console.log(str5);            // MM5xpzvdkIw5VakOj3Qp7w
        console.log(str6);            // 0Îq§;Ý��9U©��t)ï
</script>
Copy after login

3. sha1 encryption (irreversible)

The sha1.js download address is not found here, so find the online file in bootcdn and import it. , you can download it from bootcdn if you need it

<script src="https://cdn.bootcss.com/js-sha1/0.6.0/sha1.js"></script>
<script type="text/javascript">
        var sha1_1 = sha1("mosquito~");
        console.log(sha1_1);
        var sha1_2 = sha1("admin:1001");
        console.log(sha1_2);
</script>
Copy after login

4. Encoding and decoding strings

Use the JS function escape() and unescape() to encode and decode strings respectively

<script type="text/javascript">
      var escape1 =escape("我的名字是:mosquito~");//编码
      console.log(escape1);
      var unescape1 = unescape(escape1); //解码
      console.log(unescape1);
</script>
Copy after login

5. AES/DES encryption and decryption

Download crypto-js.js The introduction URL is click to download

var aseKey = "12345678"     //秘钥必须为:8/16/32位
var message = "80018000142";
//加密
var encrypt = CryptoJS.AES.encrypt(message, CryptoJS.enc.Utf8.parse(aseKey), {
  mode: CryptoJS.mode.ECB,
  padding: CryptoJS.pad.Pkcs7
}).toString();
console.log(encrypt);    //VKrZlqykem73x8/T2oCfCQ==

//解密
var decrypt = CryptoJS.AES.decrypt(encrypt, CryptoJS.enc.Utf8.parse(aseKey), {
  mode: CryptoJS.mode.ECB,
  padding: CryptoJS.pad.Pkcs7
}).toString(CryptoJS.enc.Utf8);
console.log(decrypt);    //80018000142
Copy after login

[Note]: It must be converted into a string when encrypted. toString. When decrypting, the format of utf8 must be used

Others

RSA encryption

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What are the JavaScript encryption methods?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template