本文目录导读:

在PHP中,字符串加密算法选择取决于你的具体需求,以下是主要场景和推荐算法:
密码哈希(单向加密)
用于存储用户密码,不可逆。
// 推荐:password_hash() (使用bcrypt算法)
$hash = password_hash('mypassword', PASSWORD_DEFAULT);
// 验证
if (password_verify('mypassword', $hash)) {
echo '密码正确';
}
// 或使用Argon2id(更安全)
$hash = password_hash('mypassword', PASSWORD_ARGON2ID);
数据加密(可逆加密)
用于加密敏感数据,需要解密使用。
AES-256-GCM(推荐)
function encryptData($data, $key) {
$iv = openssl_random_pseudo_bytes(12); // GCM模式推荐12字节IV
$tag = '';
$ciphertext = openssl_encrypt(
$data, 'aes-256-gcm', $key,
OPENSSL_RAW_DATA, $iv, $tag
);
return base64_encode($iv . $tag . $ciphertext);
}
function decryptData($encrypted, $key) {
$decoded = base64_decode($encrypted);
$iv = substr($decoded, 0, 12);
$tag = substr($decoded, 12, 16);
$ciphertext = substr($decoded, 28);
return openssl_decrypt(
$ciphertext, 'aes-256-gcm', $key,
OPENSSL_RAW_DATA, $iv, $tag
);
}
$key = 'your-32-byte-secret-key'; // 必须是256位(32字节)
$encrypted = encryptData('敏感数据', $key);
$decrypted = decryptData($encrypted, $key);
AES-256-CBC(传统方式)
function aesEncrypt($data, $key) {
$iv = openssl_random_pseudo_bytes(16); // CBC需要16字节IV
$encrypted = openssl_encrypt(
$data, 'aes-256-cbc', $key,
OPENSSL_RAW_DATA, $iv
);
return base64_encode($iv . $encrypted);
}
function aesDecrypt($encrypted, $key) {
$decoded = base64_decode($encrypted);
$iv = substr($decoded, 0, 16);
$ciphertext = substr($decoded, 16);
return openssl_decrypt(
$ciphertext, 'aes-256-cbc', $key,
OPENSSL_RAW_DATA, $iv
);
}
简单混淆(非安全用途)
用于URL参数、短ID等,不需要真正安全:
// 使用base64(不推荐用于安全场景)
$encoded = base64_encode('hello world');
$decoded = base64_decode($encoded);
// 使用自定义混淆
function simpleEncode($data) {
return urlencode(base64_encode($data));
}
function simpleDecode($data) {
return base64_decode(urldecode($data));
}
完整安全方案示例
结合以上功能的安全类:
class SecureEncryption {
private $key;
private $cipher = 'aes-256-gcm';
public function __construct($key) {
$this->key = hash('sha256', $key, true); // 确保密钥长度为32字节
}
public function encrypt($plaintext) {
$iv = openssl_random_pseudo_bytes(12);
$tag = '';
$ciphertext = openssl_encrypt(
$plaintext, $this->cipher, $this->key,
OPENSSL_RAW_DATA, $iv, $tag
);
if ($ciphertext === false) {
throw new Exception('加密失败');
}
return base64_encode($iv . $tag . $ciphertext);
}
public function decrypt($ciphertext) {
$decoded = base64_decode($ciphertext);
if (strlen($decoded) < 28) {
throw new Exception('数据格式错误');
}
$iv = substr($decoded, 0, 12);
$tag = substr($decoded, 12, 16);
$data = substr($decoded, 28);
$plaintext = openssl_decrypt(
$data, $this->cipher, $this->key,
OPENSSL_RAW_DATA, $iv, $tag
);
if ($plaintext === false) {
throw new Exception('解密失败或数据被篡改');
}
return $plaintext;
}
}
// 使用示例
$secure = new SecureEncryption('你的密钥');
$encrypted = $secure->encrypt('Hello World!');
$decrypted = $secure->decrypt($encrypted);
安全建议
- 密码存储:永远使用
password_hash(),不要自定义 - 数据加密:优先选择 AES-256-GCM(带认证)
- 密钥管理:存储在环境变量或专用密钥管理系统中
- 传输层:使用HTTPS而不是在应用层加密
- 哈希算法:SHA-256用于完整性校验,MD5不要再使用
不推荐使用的算法
- ❌
md5():已不安全 - ❌
sha1():已被破解 - ❌
crypt():过时且不安全 - ❌ 自定义加密算法:总是有问题
选择算法时,请根据你的具体使用场景和安全需求来决定,对于敏感数据,推荐使用AES-256-GCM;对于密码,务必使用password_hash()。