Essays on md5 encryption and aes encryption in CryptoJS

Various encryptions have been used in recent projects, including aes encryption that has never been touched, so from various online searches, an official statement:

Advanced Encryption Standard (English: Advanced Encryption Standard, abbreviation: AES), also known as Rijndael encryption in cryptography, is a block encryption standard adopted by the United States federal government . This standard is used to replace the original DES , which has been analyzed by many parties and is widely used all over the world. After a five-year selection process, the Advanced Encryption Standard was published by the National Institute of Standards and Technology (NIST) in FIPS PUB 197 on November 26, 2001, and became a valid standard on May 26, 2002. In 2006, the Advanced Encryption Standard had become one of the most popular algorithms in symmetric key encryption.

Obviously, it's useless. Finally found a CryptoJS package, download: https://code.google.com/archive/p/crypto-js/downloads , introduction:   https://code.google.com/archive/p/crypto-js/ , including all kinds of encryption, let's talk about aes here.

To be honest, I have stepped on a lot of pits, because aes has several modes and complementing methods, so I have referred to a lot of materials, and one of them is very well written, which introduces each step in detail, https://zhidao. baidu.com/question/1819427615658816228.html , if you want to know more, you can take a look. We are using CBC mode, AES-128bit, Pkcs7's complement method (the background may be PKCS5Padding, which is the same), and the background setting is the same at the beginning. The key is 10 bits, they can be encrypted and decrypted, but I can't use it, I think it must be I didn't handle it well, but in the end there is no way for everyone to change it to 16 bits, that's right, you can match the background . Paste code:

 

//aes加密
function encrypt(word) {
    var key = CryptoJS.enc.Utf8.parse("1234567890000000"); //16位
    var iv = CryptoJS.enc.Utf8.parse("1234567890000000");
    var encrypted = '';
    if (typeof(word) == 'string') {
        var srcs = CryptoJS.enc.Utf8.parse(word);
        encrypted = CryptoJS.AES.encrypt(srcs, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
    } else  if ( typeof (word) == 'object') { // Convert object format to json string 
        data = JSON.stringify(word);
         var srcs = CryptoJS.enc.Utf8.parse(data);
        encrypted = CryptoJS.AES.encrypt(srcs, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        })
    }
    return encrypted.ciphertext.toString();
}
// aes解密
function decrypt(word) {
    var key = CryptoJS.enc.Utf8.parse("1234567890000000"); 
    var iv = CryptoJS.enc.Utf8.parse("1234567890000000");
    var encryptedHexStr = CryptoJS.enc.Hex.parse(word);
    var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    var decrypt = CryptoJS.AES.decrypt(srcs, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();
}

 

加密解密的key和iv必须是一致的,前台后台的加密方式也必须是一致的,不然肯定解不对,说实话我折腾了好久,终于好了。因为CryptoJS默认就是CBC模式和Pkcs补码,所以我只用aes.js就可以,如果大家用的是别的模式和补码方式,还要引用相应的js。

 

 

mode开头的是模式,pad开头的是补码方式。

CryptoJs还有md5加密我们也用到了,这个比较简单,先引用md5.js。

// md5
function md5encode(word) {
    return CryptoJS.MD5(word).toString();
}

好了,写完了,主要是记录一下,写的挺不详细的,大家凑合看吧。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326039433&siteId=291194637