如何导出IV和密钥到crypto.createCipheriv进行解密?

Mic*_*ael 4 encryption cryptography aes pbkdf2 node.js

我已经看到了有关创建用于加密的初始化向量(IV)的其他问题,并且似乎使用随机值是一个选项.但是,我需要生成用于解密的IV,因此我必须使用与基于某些盐加密数据相同的IV.

node.js加密函数createDecipher说:

crypto.createDecipher()的实现使用OpenSSL函数EVP_BytesToKey导出密钥,摘要算法设置为MD5,一次迭代,无盐.

为了向后兼容其他软件加密的资产,我需要不同的迭代次数和我指定的salt.

继续阅读文档,它进一步说:

在与OpenSSL的推荐使用PBKDF2,而不是EVP_BytesToKey线,建议开发者得到自己使用crypto.pbkdf2()键和IV和使用crypto.createDecipheriv()创建欲海对象.

好的,听起来不错.我需要解密的数据使用EVP_BytesToKey进行加密以获取密钥和IV,因此我需要与之兼容.

无论如何,crypto.pbkdf2函数似乎采用了我需要的所有参数,但问题是,它似乎没有创建初始化向量.

执行解密的相应C代码需要兼容,如下所示:

// parameters to function:
// unsigned char *decrypt_salt
// int nrounds
// unsigned char *decrypt_key_data  <- the password
//  int decrypt_key_data_len <- password length

// the following is not initialized before the call to EVP_BytesToKey
unsigned char decrypt_key[32], decrypt_iv[32];

EVP_BytesToKey(EVP_aes_256_cbc(), EVP_md5(), decrypt_salt, decrypt_key_data,
                   decrypt_key_data_len, nrounds, decrypt_key, decrypt_iv);
Run Code Online (Sandbox Code Playgroud)

我试图用来crypto.pbkdf2复制这种行为:

crypto.pbkdf2(password, salt, nrounds, 32, "md5", (err, derivedKey) => {
    if (err) throw err
    console.log(derivedKey.toString("hex"))
})
Run Code Online (Sandbox Code Playgroud)

derivedKey也与上面C代码生成的密钥不匹配.我不确定这是否是预期的!我也尝试过48和64的密钥长度,但这些密钥长度和预期的密钥和IV都没有.

给定正确的密码,盐和散列轮次,如何生成相同的密钥和IV来解密?

Luk*_*ark 9

首先,你没有得到你想要的结果是因为你使用的C代码确实使用EVP_BytesToKey,而你的NodeJS代码使用PBKDF2.我想你可能误解了OpenSSL的建议.他们推荐PBKDF2,不是产生相同结果的更好方法,而是解决问题的更好方法.PBKDF2只是一个更好的密钥派生函数,但它不会产生相同的结果EVP_BytesToKey.

此外,您首先处理IV代的方式非常差.使用KDF生成您的密钥非常好,做得很好.坦率地说,使用KDF生成IV是一个相当糟糕的想法.您的初始读数,您发现随机生成IV是一个好主意,是正确的. 所有IV/nonce应该随机生成.总是. 这里要记住的重要一点是IV不是秘密.你可以公开传递它.

大多数实现将随机生成IV,然后将其作为密文的前缀.然后,当涉及到解密时,您可以简单地删除前128位(AES)的字节并将其用作IV.这涵盖了您的所有基础,这意味着您不必从与关键材料(令人讨厌的)相同的地方获取您的IV.

有关详细信息,请参阅此GitHub存储库中的示例.我在下面包含了NodeJS,这是NodeJS中最佳实践现代加密的一个例子:

const crypto = require("crypto");

const ALGORITHM_NAME = "aes-128-gcm";
const ALGORITHM_NONCE_SIZE = 12;
const ALGORITHM_TAG_SIZE = 16;
const ALGORITHM_KEY_SIZE = 16;
const PBKDF2_NAME = "sha256";
const PBKDF2_SALT_SIZE = 16;
const PBKDF2_ITERATIONS = 32767;

function encryptString(plaintext, password) {
    // Generate a 128-bit salt using a CSPRNG.
    let salt = crypto.randomBytes(PBKDF2_SALT_SIZE);

    // Derive a key using PBKDF2.
    let key = crypto.pbkdf2Sync(new Buffer(password, "utf8"), salt, PBKDF2_ITERATIONS, ALGORITHM_KEY_SIZE, PBKDF2_NAME);

    // Encrypt and prepend salt.
    let ciphertextAndNonceAndSalt = Buffer.concat([ salt, encrypt(new Buffer(plaintext, "utf8"), key) ]);

    // Return as base64 string.
    return ciphertextAndNonceAndSalt.toString("base64");
}

function decryptString(base64CiphertextAndNonceAndSalt, password) {
    // Decode the base64.
    let ciphertextAndNonceAndSalt = new Buffer(base64CiphertextAndNonceAndSalt, "base64");

    // Create buffers of salt and ciphertextAndNonce.
    let salt = ciphertextAndNonceAndSalt.slice(0, PBKDF2_SALT_SIZE);
    let ciphertextAndNonce = ciphertextAndNonceAndSalt.slice(PBKDF2_SALT_SIZE);

    // Derive the key using PBKDF2.
    let key = crypto.pbkdf2Sync(new Buffer(password, "utf8"), salt, PBKDF2_ITERATIONS, ALGORITHM_KEY_SIZE, PBKDF2_NAME);

    // Decrypt and return result.
    return decrypt(ciphertextAndNonce, key).toString("utf8");
}

function encrypt(plaintext, key) {
    // Generate a 96-bit nonce using a CSPRNG.
    let nonce = crypto.randomBytes(ALGORITHM_NONCE_SIZE);

    // Create the cipher instance.
    let cipher = crypto.createCipheriv(ALGORITHM_NAME, key, nonce);

    // Encrypt and prepend nonce.
    let ciphertext = Buffer.concat([ cipher.update(plaintext), cipher.final() ]);

    return Buffer.concat([ nonce, ciphertext, cipher.getAuthTag() ]);
}

function decrypt(ciphertextAndNonce, key) {
    // Create buffers of nonce, ciphertext and tag.
    let nonce = ciphertextAndNonce.slice(0, ALGORITHM_NONCE_SIZE);
    let ciphertext = ciphertextAndNonce.slice(ALGORITHM_NONCE_SIZE, ciphertextAndNonce.length - ALGORITHM_TAG_SIZE);
    let tag = ciphertextAndNonce.slice(ciphertext.length + ALGORITHM_NONCE_SIZE);

    // Create the cipher instance.
    let cipher = crypto.createDecipheriv(ALGORITHM_NAME, key, nonce);

    // Decrypt and return result.
    cipher.setAuthTag(tag);
    return Buffer.concat([ cipher.update(ciphertext), cipher.final() ]);
}
Run Code Online (Sandbox Code Playgroud)

  • 嗨@Artiom,是的,这是正确的。用于*加密*的 IV/nonce 应始终随机生成。正如您正确指出的那样,*解密* 期间使用的 IV/nonce 应该与加密期间使用的相同。 (2认同)