openssl_encrypt & AES256

*爱你&永不变心* 提交于 2019-12-23 04:42:04

问题


maybe someone know how do this on PHP

echo "TEST" | openssl enc -e -aes-256-cbc -k 123456 -a -md md5

i will try like

$iv = openssl_random_pseudo_bytes(16);
$data = base64_encode(openssl_encrypt('TEST', 'aes-256-cbc', $password, OPENSSL_RAW_DATA, $iv));

but not success..


回答1:


Interestingly, like the version at https://linux.die.net/man/1/enc , the man page for my local openssl installation lists '-md' (without an argument) as an option for openssl enc - but doesn't seem to explain what its for. The openssl wiki says its used for key derivation.

A bit more digging and I found this on the SO cryptography site.

But before attempting to write an implementation, since openssl's implementation is somewhat esoteric, it rather begs the question of whether there is a need to specifically support this in your implementation? If you merely need a method of encrypting/decrypting data outside your website, you could just write a command line PHP script.

A quick read through the linked document, suggests that....

  $data = base64_encode(openssl_encrypt('TEST', 'aes-256-cbc', md5($password, true), OPENSSL_RAW_DATA, $iv));

But I expect that openssl is doing something else to the key in the derivation function to pad the 16 bytes out to the required 128 bits (why are you forcing a 128 bit key for a 256 bit algorithm?)



来源:https://stackoverflow.com/questions/49225904/openssl-encrypt-aes256

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