如何正确加密数据与正确的认证使用AES-256-CBC在PHP? [英] How to correctly encrypt data with proper authentication using AES-256-CBC in php?

查看:777
本文介绍了如何正确加密数据与正确的认证使用AES-256-CBC在PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用openssl函数在php中用AES-256-CBC加密数据。我已经能够使用唯一的IV(通过生成openssl_random_pseudo_bytes)为每个新的加密加密。

I have been using the openssl function for encrypting data with AES-256-CBC in php. I have been able to encrypt it using an unique IV (by generating with openssl_random_pseudo_bytes)for each new encryption.

但我正在努力与aes cbc的验证加密的想法。当我要解密数据时,我如何基本上验证?

But I am struggling with the idea of authenticated encryption with aes cbc. How do I basically authenticate when I am about to decrypt the data?

我需要使用像PBKDF2,blowfish或hash_hmac()?

Do I need to use something like PBKDF2, blowfish or hash_hmac()?

我需要以某种方式哈希键吗?

Do I need to hash the key somehow?

任何帮助都非常感激。

推荐答案


但我正在努力与aes cbc认证加密的想法。

But I am struggling with the idea of authenticated encryption with aes cbc. How do I basically authenticate when I am about to decrypt the data?

在用随机IV加密数据之后,密文和IV用

After you encrypt the data with a random IV, put both the ciphertext and IV into hash_hmac() with a second key.

如果你问的是因为你需要部署转为生产,请等到 defuse / php-encryption 的版本2发布后使用。 (它是AES-256-CTR而不是AES-256-CBC,但CTR模式具有比CBC模式更少的攻击面;即,如果你击败HMAC,没有填充oracle攻击。)

If you're asking because you need to deploy into production, wait until version 2 of defuse/php-encryption is released and use that instead. (It's AES-256-CTR not AES-256-CBC, but CTR mode has less attack surface than CBC mode; i.e. no padding oracle attacks if you defeat the HMAC.)

RNCryptor未按照加密编码标准,无法在 PHP ,也不在 Python

RNCryptor is/was not written in accordance to cryptography coding standards, neither in PHP, nor in Python.

RNCryptor字面上始终违反密码编码标准的规则1。可能还有其他一些尚未被发现的问题。如果您希望跨语言进行移植,请使用libsodium

RNCryptor literally violates rule 1 of the cryptography coding standards consistently. There may be other issues that have yet been undiscovered. If you want portability across languages, use libsodium.

这篇关于如何正确加密数据与正确的认证使用AES-256-CBC在PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆