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

时间:2018-02-28 02:45:55

标签: node.js encryption cryptography aes pbkdf2

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

node.js加密函数createDecipher说:

  

crypto.createDecipher()的实现使用。来派生密钥   OpenSSL函数EVP_BytesToKey,摘要算法设置为MD5,   一次迭代,没有盐。

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

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

  

符合OpenSSL建议使用PBKDF2代替   EVP_BytesToKey建议开发人员派生密钥和IV   使用crypto.pbkdf2()并使用crypto.createDecipheriv()   创建Decipher对象。

好的,听起来不错。我需要解密的数据是使用EVP_BytesToKey加密来获取密钥和IV的,所以我需要与之兼容。

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

执行解密的相应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);

我尝试使用crypto.pbkdf2复制此行为:

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

derivedKey也与上面C代码生成的密钥不匹配。我不确定这是否是预期的!我还尝试了48和64的密钥长度,但这些密钥长度和预期的密钥和IV都没有产生任何类似的东西。

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

1 个答案:

答案 0 :(得分:7)

首先,您未获得所需结果的原因是因为的C代码使用EVP_BytesToKey,而您的NodeJS代码使用PBKDF2。我想你可能误解了OpenSSL的建议。他们推荐PBKDF2,不是产生相同结果的更好方法,而是解决问题的更好方法。 PBKDF2只是一个更好的密钥派生函数,但它不会产生与EVP_BytesToKey相同的结果。

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

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

有关详细信息,请参阅this GitHub repository中的示例。我在下面包含了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() ]);
}
相关问题