How to correctly encrypt data with proper authentication using AES-256-CBC in php?

点点圈 提交于 2019-12-04 19:10:58

Simple solution, use RNCryptor which is available for php and many other languages. See this ReadMe for implementation details.

Even if you don't use RNCryptor the methods are correct and secure.

Some details from the site:

  • AES-256 encryption
  • CBC mode
  • Password stretching with PBKDF2
  • Password salting
  • Random IV
  • Encrypt-then-hash HMAC
  • Versioning

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?

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

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.)

Don't use RNCryptor.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!