CryptoJS 不能只使用 AES 而不会出现错误

Get*_*awn 6 javascript

我有一个 gulpfile,它连接我的依赖项以创建一个文件。

最终的输出文件中包含 CryptoJS。如果我使用整个库:
node_modules/crypto-js/crypto-js.js

我能够加密数据:

async function save(data) {
    let saveData = CryptoJS.AES.encrypt(data, 'My Secret').toString();
}
Run Code Online (Sandbox Code Playgroud)

如果我只使用我需要的库的一部分AES,我会收到以下错误:

未捕获(承诺中)类型错误:无法读取未定义的属性“创建”

我要连接的文件按以下顺序排列:

let fileList = [
  'node_modules/crypto-js/core.js',
  'node_modules/crypto-js/cipher-core.js',
  'node_modules/crypto-js/enc-utf8.js',
  'node_modules/crypto-js/aes.js',
]
Run Code Online (Sandbox Code Playgroud)

我缺少文件吗?

编辑

错误堆栈:

Uncaught (in promise) TypeError: Cannot read property 'create' of undefined
    at Object.execute (gamesmart.js:2067)
    at Object.encrypt (gamesmart.js:2114)
    at Object.encrypt (gamesmart.js:1493)
    at Object.<anonymous> (gamesmart.js:42)
    at Generator.next (<anonymous>)
    at gamesmart.js:7
    at new Promise (<anonymous>)
    at __awaiter (gamesmart.js:3)
    at Object.save (gamesmart.js:41)
    at HTMLDocument.document.addEventListener (ajax.js:3)
execute @   gamesmart.js:2067
encrypt @   gamesmart.js:2114
encrypt @   gamesmart.js:1493
(anonymous) @   gamesmart.js:42
(anonymous) @   gamesmart.js:7
__awaiter   @   gamesmart.js:3
save    @   gamesmart.js:41
document.addEventListener   @   ajax.js:3
async function (async)      
document.addEventListener
Run Code Online (Sandbox Code Playgroud)

它在这个函数中(来自核心而不是我的)(var key = EvpKDF.create({ keySize: keySize + ivSize }).compute(password, salt)):

execute: function (password, keySize, ivSize, salt) {
            // Generate random salt
            if (!salt) {
                salt = WordArray.random(64/8);
            }

            // Derive key and IV
            var key = EvpKDF.create({ keySize: keySize + ivSize }).compute(password, salt);

            // Separate key and IV
            var iv = WordArray.create(key.words.slice(keySize), ivSize * 4);
            key.sigBytes = keySize * 4;

            // Return params
            return CipherParams.create({ key: key, iv: iv, salt: salt });
        }
Run Code Online (Sandbox Code Playgroud)

Fab*_*oli 0

您在浏览器中使用它吗?节点还是哪里?

我认为与每个文件中的代码包装有关:

;(function (root, factory, undef) {
    if (typeof exports === "object") {
        // CommonJS
        // No good for simple concatenation
    }
    else if (typeof define === "function" && define.amd) {
        // AMD
        // No good for simple concatenation
    }
    else {
        // Global (browser)
        // Should work
    }
}(this, function (CryptoJS) {  }));
Run Code Online (Sandbox Code Playgroud)

我认为如果你使用类似于rollup o 的东西来合并文件会更好。

或者您必须确保“导出”和“定义”没有被定义或定义为与对象和函数不同的东西。