2017-02-21 51 views
-1

我想創建一個函數,可以加密一個字符串。我有下面的代碼,但我得到一個錯誤。mcrypt_get_block_size():模塊初始化失敗 - 錯誤功能

$key = "testkey"; // This is the function that does the encryption. Treat it as a black box. Do not edit! function encrypt($str, $key){ $block = mcrypt_get_block_size('ISO-8859-1', 'ecb'); $pad = $block - (strlen($str) % $block); $str .= str_repeat(chr($pad), $pad); return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_ECB)); } // call the encrypt function and send it the key and the data to encrypt. Store the returned data in the $dataopt variable. $dataopt = encrypt($rawstring, $key);

的錯誤是 「mcrypt_get_block_size():模塊初始化失敗上行線路41」,它是$塊= mcrypt_get_block_size( 'ISO-8859-1', 'ECB');

任何想法?

回答

0

您正在將'ISO-8859-1'作爲第一個參數傳遞給mcrypt_get_block_size

您可能打算通過MCRYPT_RIJNDAEL_128作爲第一個參數。

請注意,mcrypt已被棄用,因此您應該查看其他解決方案。看看this question的一些替代品。

+0

嗨,奧茲。感謝您的回覆。我已經用MCRYPT_RIJNDAEL_128取代了ISO-8859-1,但是我得到了錯誤「警告:mcrypt_encrypt():這個算法不支持大小爲7的鍵。只有在第44行支持的大小爲16,24或32的鍵涉及返回base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$ key,$ str,MCRYPT_MODE_ECB));感謝您的幫助 – deanhodges

+0

@deanhodges如果答案有幫助,請接受它。正如我所提到的,我強烈建議你根本不使用mcrypt。你的錯誤是你需要一個二進制密鑰,並且你正在傳遞一個字符串(參見http://php.net/manual/en/function.mcrypt-encrypt.php)。 mcrypt是非常低的水平,它很容易讓事情在概念上錯誤**即使一切看起來都很好**。我不知道你爲什麼要專門加密字符串,但有一個更好的解決方案,比幾乎任何需要的密碼。 –

相關問題