Php 无法使加密类工作
我无法让加密类工作(它位于classes文件夹中的一个单独文件中)。该类的代码为:Php 无法使加密类工作,php,security,encryption,Php,Security,Encryption,我无法让加密类工作(它位于classes文件夹中的一个单独文件中)。该类的代码为: class SymmetricCrypt { // Encryption/decryption key. private static $msSecretKey = "Hello"; // The initialisation vector. private static $msHexaIv = "c7098adc8d6128b5d4b4f7b2fe7f7f05"; /
class SymmetricCrypt
{
// Encryption/decryption key.
private static $msSecretKey = "Hello";
// The initialisation vector.
private static $msHexaIv = "c7098adc8d6128b5d4b4f7b2fe7f7f05";
// Use the Rijndael Algorithm.
private static $msCipherAlgorithm = MCRYPT_RIJNDAEL_128;
public static function Encrypt($plainString)
{
$binary_iv = pack("H*", SymmetricCrypt::$msHexaIv);
// Encrypt source.
$binary_encrypted_string = mcrypt_encrypt(SymmetricCrypt::$msCipherAlgorithm, SymmetricCrypt::$msSecretKey, $plainString, MCRYPT_MODE_CBC, $binary_iv);
// Convert $binary_encrypted_string to hexadeciaml format.
$hexa_encrypted_string = bin2hex($binary_encrypted_string);
return $hexa_encrypted_string;
}
public static function Decrypt($encryptedString)
{
$binary_iv = pack("H*", SymmetricCrypt::$msHexaIv);
// Convert string in hexadecimal to byte array.
$binary_encrypted_string = pack("H*", $encryptedString);
// Decrypt $binary_encrypted_string,
$decrypted_string = mcrypt_decrypt(SymmetricCrypt::$msCipherAlgorithm, SymmetricCrypt::$msSecretKey, $binary_encrypted_string, MCRYPT_MODE_CBC, $binary_iv);
return $decrypted_string;
}
}
我是这样称呼这个班的:
require_once 'classes/symmetric_crypt.php';
$sc = new SymmetricCrypt();
$password = "password";
$ec_password = $sc->Encrypt($password);
... insert into database.
如果我回显$password的内容,那么它将显示“password”。如果我回显$ec_密码,它将不返回任何内容
我以前在不同服务器上的不同项目上使用过它。可能与服务器有关吗?还有其他想法吗
谢谢
阿德里安在工作
注二:
- 不应重复使用初始化向量。否则,就更容易找到加密密钥(请参见WEP)
- 像premiso saids一样,您不应该将密码存储为可解密字符串。使用带有强散列函数(不是MD5!)的盐散列