About the difference between front-end AES encryption and background encryption.

the background is php. Use openssl to encrypt it with aes . The encryption result is consistent with the result of http://tool.chacuo.net/cryptaes.

The problem with

now is that the front end uses CryptoJS no matter what. Either the data encrypted in the background is inconsistent, or the CryptoJS encryption cannot be decrypted with CryptoJS .

what is the problem? if you have a better plan, please write it down as well. Too distressing.

php Code:

//
var aeskey = CryptoJS.enc.Utf8.parse("ABC123");
//
var aesiv = CryptoJS.enc.Utf8.parse(1111111111111111);

// 
function encrypt(data) {
    var srcs = CryptoJS.enc.Utf8.parse(data);
    var encrypted = CryptoJS.AES.encrypt(srcs, aeskey, {
        iv: aesiv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    //base64
    return encrypted.toString();
}

//
function decrypt(data) {
    // database64
    var decrypt = CryptoJS.AES.decrypt(data, aeskey, {
        iv: aesiv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    var decryptedStr = decrypt.toString();
    return decryptedStr;
}

// :IkNtZY1m41sEkgIqsuZBoQ==
// 
console.log(encrypt("1000001"));

both front and back end codes are posted


I read the CryptoJS document:
CryptoJS supports AES-128, AES-192, and AES-256. It will pick the variant by the size of the key you pass in. If you use a passphrase, then it will generate a 256-bit key.
means that his encryption algorithm is automatically selected according to the length of your key, so you should first fix the length of key:

/**
 *  `\x00` 
 * @param {string} key     
 * @param {Number} keySize , : 128, 256
 */
function fillKey(key, keySize) {
    keySize = keySize || 128;
    var filledKey = Buffer.alloc(keySize / 8);
    var keys = Buffer.from(key);
    if (keys.length < filledKey.length) {
        for (var i = 0; i < filledKey.length; iPP) {
            filledKey[i] = keys[i];
        }
    }

    return filledKey;
}

then reset your key:

//
var aeskey = CryptoJS.lib.WordArray.create(fillKey("ABC123", 128));

I tested it. The encryption is now correct, but the decryption is not quite right. Look at the source code and find that it should be the processing problem of toString:

//
function decrypt(data) {
    // database64
    var decrypt = CryptoJS.AES.decrypt(data, aeskey, {
        iv: aesiv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    
    return decrypt.toString(CryptoJS.enc.Utf8);
}

well, there is no problem now
PS: it is recommended to remove var srcs = CryptoJS.enc.Utf8.parse (data) from the encryption method.
although there is no problem, I think the encrypt method requires string, while parse gets WordArray

.

length problem. Backend encryption should be 16-bit by default. If the encryption string is intercepted by 16-bit encryption, if it is too long, the encryption result will not match the backend


I have this problem now. Has the landlord solved it?

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bf5d93-31fc7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bf5d93-31fc7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?