LibMcrypt是在2007年的更多信息被遺棄https://wiki.php.net/rfc/mcrypt-viking-funeral
你必須使用的OpenSSL加密http://php.net/manual/en/function.openssl-encrypt.php
PHP
<?php
$textToEncrypt = "Secret Text to Encrypt";
$encryptionMethod = 'aes-256-cbc';
$secretHash = "315a5504d921f8327f73a356d2bbcbf1"; // <---- you have to use some persistent key.
$iv_size = openssl_cipher_iv_length($encryptionMethod);
$iv = openssl_random_pseudo_bytes($iv_size);
//To encrypt
$encryptedMessage = openssl_encrypt($textToEncrypt, $encryptionMethod, $secretHash, 0, $iv);
//Concatenate iv with data
$encryptedMessageWithIv = bin2hex($iv) . $encryptedMessage;
//To Decrypt
$iv_size = openssl_cipher_iv_length($encryptionMethod);
$iv = hex2bin(substr($encryptedMessageWithIv, 0, $iv_size * 2));
$decryptedMessage = openssl_decrypt(substr($encryptedMessageWithIv, $iv_size * 2), $encryptionMethod, $secretHash, 0, $iv);
echo "Encrypted: $encryptedMessageWithIv <br>Decrypted: $decryptedMessage";
嘗試在這裏https://3v4l.org/r9pYv
周的Node.js(我真的不使Node.js的程序員,可以有更有效的方法)
var data = "ad699a2537ec2a7f699acbf97ca0080eh3z5EgvnTAvlc76YeR6HdWPmkDDt+pHiG//qo7xnqyQ=";
var key = "315a5504d921f8327f73a356d2bbcbf1";
var iv = new Buffer(data.substring(0,32), 'hex');
var dec = crypto.createDecipheriv('aes-256-cbc',key,iv);
var decrypted = Buffer.concat([dec.update(new Buffer(data.substring(32),'base64')), dec.final()]);
console.log('DECRYPTED TEXT: '+decrypted.toString());
試一下:https://repl.it/FQyo/2
這就解釋了爲什麼其棄用,並就如何改用建議http://php.net/manual/en/migration71.deprecated.php – RiggsFolly
嘗試使用'openssl_encrypt' http://php.net/manual/en/function.openssl-encrypt.php –
建議使用openssl,而不是隻是因爲LibMcrypt已被棄用,而且因爲NodeJS Crypto模塊在openssl上中繼,因此您可以期望算法具有良好的兼容性。 –