我在研究Internet上的AES加密時發現了以下代碼。在此代碼中,我發現key
和iv
使用hash function
生成,並使用sha256
。我想知道這種方法是否對使用PHP加密文本是安全的。用於AES加密的生成密鑰的安全
,我在網上找到了下面給出的代碼,
<?php
function encrypt_decrypt($action, $string) {
$output = false;
$encrypt_method = "AES-256-CBC";
$secret_key = 'This is my secret key';
$secret_iv = 'This is my secret iv';
// hash
$key = hash('sha256', $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);
if ($action == 'encrypt') {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
} else if($action == 'decrypt') {
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
return $output;
}
$plain_txt = "This is my plain text";
echo "Plain Text =" .$plain_txt. "\n";
$encrypted_txt = encrypt_decrypt('encrypt', $plain_txt);
echo "Encrypted Text = " .$encrypted_txt. "\n";
$decrypted_txt = encrypt_decrypt('decrypt', $encrypted_txt);
echo "Decrypted Text =" .$decrypted_txt. "\n";
if ($plain_txt === $decrypted_txt) echo "SUCCESS";
else echo "FAILED";
echo "\n";
?>
我對代碼做了一些更改。你能看看代碼並告訴我這是否會有所作爲?這裏是代碼的鏈接https://codeshare.io/5wydZB –
第1號。使用CPRNG,而不是使用'array_rand'的自制實現,它聲明:「它使用僞隨機數生成器**不適用於加密目的。「 2.從文檔'crypt'不會迭代,並且需要足夠的迭代來需要100ms。這個IV似乎沒有通過解密。 4.海事組織你最好的選擇是使用[RNCryptor](https://github.com/RNCryptor/RNCryptor-php),它使事情正確,增加認證,版本控制並且經過審查。 – zaph