Home  >  Article  >  Web Front-end  >  Simple how to use CryptoJS

Simple how to use CryptoJS

一个新手
一个新手Original
2017-09-25 10:21:157512browse

CryptoJS (crypto.js) provides a variety of encryption algorithms for JavaScript. Currently supported algorithms include: MD5SHA-1SHA-256AESRabbitMARC4HMACHMAC-MD5HMAC-SHA

Specific download address: Click to download

There are mainly two folders, components and rollups

The first one is the component and the second one is the summary.

The files in the summary folder are compressed after the assembly of one or more folders.

This allows aggregation of separate folders into your project to incorporate project files without having to worry about its dependencies.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CryptoJS</title>
    <script src="js/CryptoJS%20v3.1.2/components/core.js"></script>
    <script src="js/CryptoJS%20v3.1.2/components/md5.js"></script>
    <script src="js/CryptoJS%20v3.1.2/components/evpkdf.js"></script>
    <script src="js/CryptoJS%20v3.1.2/components/enc-base64.js"></script>
    <script src="js/CryptoJS%20v3.1.2/components/cipher-core.js"></script>
    <script src="js/CryptoJS%20v3.1.2/components/aes.js"></script>
    <script src="js/CryptoJS%20v3.1.2/components/hmac.js"></script>
    <script src="js/CryptoJS%20v3.1.2/components/sha1.js"></script>
    <script src="js/CryptoJS%20v3.1.2/components/sha256.js"></script>
</head>
<body>
<p id="content"></p>
    <script>
        var md5 = CryptoJS.MD5("Message").toString(CryptoJS.enc.Hex);
        console.log("md5 = %s", md5);

        var sHA1 = CryptoJS.SHA1("Message").toString(CryptoJS.enc.Hex);
        console.log("sHA1 = %s", sHA1);

        var sHA256 = CryptoJS.SHA256("Message").toString(CryptoJS.enc.Hex);
        console.log("sHA256 = %s", sHA256);

        var hmacMD5 = CryptoJS.HmacMD5("Message", "Secret Passphrase").toString(CryptoJS.enc.Hex);
        console.log("hmacMD5 = %s", hmacMD5);

        var hmacSHA1 = CryptoJS.HmacSHA1("Message", "Secret Passphrase").toString(CryptoJS.enc.Hex);
        console.log("hmacSHA1 = %s", hmacSHA1);

        var aesEncrypt = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
        console.log("aesEncrypt = %s", aesEncrypt.iv.toString(CryptoJS.enc.Hex));

        var aesDecrypt = CryptoJS.AES.decrypt(aesEncrypt, "Secret Passphrase");
        console.log("aesDecrypt = %s", aesDecrypt.toString(CryptoJS.enc.Utf8));

        // base64 encrypt
        var rawStr = "hello world!";
        var wordArray = CryptoJS.enc.Utf8.parse(rawStr);
        var base64 = CryptoJS.enc.Base64.stringify(wordArray);
        console.log(&#39;base64Encrypt = &#39;, base64);

        // base64 decrypt
        var parsedWordArray = CryptoJS.enc.Base64.parse(base64);
        var parsedStr = parsedWordArray.toString(CryptoJS.enc.Utf8);
        console.log(&#39;base64Decrypt = &#39;,parsedStr);
    </script>
</body>
</html>

The front-end uses the CryptoJS class library to perform AES encryption and submit it to the backend. After that, the backend decryption fails and an error is reported.Given final block not properly paddedAfter some time After searching, I finally have a solution!

Since the background java uses AES encryption and uses AES/EBC/PKCS5Padding for initialization,

1、首先var key = CryptoJS.enc.Utf8.parse(key);  
2、由于后端用的PKCS5Padding,而CryptoJS类库类库中没有,但有PKCS7Padding,跟PKCS5Padding是一样的,所以加密时加上红色部分的参数即可:CryptoJS.AES.encrypt(text, key, {  
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
});

The above is the detailed content of Simple how to use CryptoJS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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