在PHP中,对称加密通常使用AES(Advanced Encryption Standard)算法。为了确保对称加密的可靠性,可以采取以下措施:
openssl_random_pseudo_bytes()
函数生成一个安全的随机密钥。$key = openssl_random_pseudo_bytes(32); // 256-bit key
$cipher = "AES-256-CBC";
$ivLength = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivLength);
$padding = 16; // PKCS7 padding
$plaintext = "Your plaintext data";
$paddedPlaintext = pkcs7_pad($plaintext, $padding);
// Encryption
$ciphertext = openssl_encrypt($paddedPlaintext, $cipher, $key, 0, $iv);
// Decryption
$unpaddedPlaintext = openssl_decrypt($ciphertext, $cipher, $key, 0, $iv);
$plaintext = pkcs7_unpad($unpaddedPlaintext, $padding);
通过遵循这些建议,可以确保PHP对称加密的可靠性。