首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么在node.js中decipherIv.final()在解密使用PHP加密的值时会失败?

为什么在node.js中decipherIv.final()在解密使用PHP加密的值时会失败?
EN

Stack Overflow用户
提问于 2019-04-17 08:16:36
回答 1查看 251关注 0票数 2

PHP版本: 5.6.39

Node.js版本: 10.9.0

目标:使用PHP加密,使用Node.js解密

PHP:我目前假设到OpenSSL的PHP Node.js绑定都使用PKCS7填充。和Node.js是否使用到OpenSSL的不兼容绑定?

PHP加密/解密代码示例:

代码语言:javascript
复制
class SymmetricEncryption {

   public static function simpleEncrypt($key, $plaintext) {
      $iv = openssl_random_pseudo_bytes(16);
      $ciphertext = openssl_encrypt($plaintext, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv)

      return base64_encode($iv) . "." . base64_encode($ciphertext);
   }

   public static function simpleDecrypt($key, $token) {
       list($iv, $ciphertext) = explode(".", $token);

        return openssl_decrypt(
           base64_decode($ciphertext),
           "aes-256-cbc",
           $key,
           OPENSSL_RAW_DATA,
           base64_decode($iv)
        );
   }
}

Node.js加密/解密代码示例:

代码语言:javascript
复制
class SymmetricEncryption {

    static simpleEncrypt(key: Buffer, plaintext: string): string {
        const iv = randomBytes(16)
        const cipher = createCipheriv('aes-256-cbc', key, iv)
        const encrypted = cipher.update(plaintext)
        const token = Buffer.concat([encrypted, cipher.final()])

        return iv.toString('base64') + "." + token.toString('base64')
    }

    static simpleDecrypt(key: Buffer, token: string): string {
        const [iv, ciphertext] = token.split(".").map(piece => Buffer.from(piece, 'base64'))
        const decipher = createDecipheriv('aes-256-cbc', key, iv)
        const output = decipher.update(ciphertext)
        return Buffer.concat([output, decipher.final()]).toString('utf8')
    }
}

我已经成功地独立测试了每个实现,但是在PHP中加密和在node.js中解密时,我得到了以下错误:

代码语言:javascript
复制
Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt

堆栈跟踪将我指向有问题的行,它是simpleDecrypt方法中的decipher.final()

我正在使用以下(失败的)单元测试来验证我的实现

代码语言:javascript
复制
it('should be able to decrypt values from php', () => {
    const testAesKey = Buffer.from('9E9CEB8356ED0212C37B4D8CEA7C04B6239175420203AF7A345527AF9ADB0EB8', 'hex')
    const phpToken = 'oMhL/oIPAGQdMvphMyWdJw==.bELyRSIwy+nQGIyLj+aN8A=='
    const decrypted = SymmetricEncryption.simpleDecrypt(testAesKey, phpToken)
    expect(decrypted).toBe('hello world')
})

我在这里使用的phpToken变量是使用以下代码创建的:

代码语言:javascript
复制
$testAesKey = "9E9CEB8356ED0212C37B4D8CEA7C04B6239175420203AF7A345527AF9ADB0EB8";

echo SymmetricEncryption::simpleEncrypt($testAesKey, "hello world");
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-17 08:46:44

我怀疑你的问题是由你在PHP中传递密钥的方式引起的。openssl_encrypt的文档没有指定任何地方将键解释为十六进制。但是,它确实会截断过长的键。

这里可能发生的情况是,PHP将64个字符的十六进制字符串截断为32个字节,并使用这些字符作为键。在使用你的PHP key之前尝试使用hex2bin -这应该可以解决你的问题!

代码语言:javascript
复制
$testAesKey = hex2bin("9E9CEB8356ED0212C37B4D8CEA7C04B6239175420203AF7A345527AF9ADB0EB8");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55718212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档