10个PHP加密解密函数,保护你的数据安全
PHP加密解密函数是保护敏感数据的关键。在大多数PHP应用程序中,我们存储和传输数据,如登录凭据,银行账户,个人身份证号码等等。如果这些数据没有被加密,那么这些数据就很容易被人窃取和滥用。
在本文中,我将介绍10个PHP加密解密函数,以保护你的数据安全。
1. md5()
md5()是一种单向加密算法,它将数据转换成32个字符的哈希值。虽然不是最安全的加密算法,但它很容易实现。一般来说,我们将MD5用于口令的加密与比较,例如网站登录,和一些简单的信息校验等。
$password = 'mypassword'; $encrypted_password = md5($password);
2. sha1()
sha1()是另一种单向加密算法,它将数据转换成40个字符的哈希值,比MD5更加安全。
$password = 'mypassword'; $encrypted_password = sha1($password);
3. base64_encode()和base64_decode()
base64_encode() 将数据编码成Base64 字符串,适用于将二进制数据转换成可打印字符的场景,例如通过邮件或者HTTP协议发送二进制数据。
$data = 'mydata'; $encoded_data = base64_encode($data);
base64_decode() 将Base64字符串解码回二进制数据。
$encoded_data = 'bXlkYXRh'; $data = base64_decode($encoded_data);
4. openssl_encrypt()和openssl_decrypt()
openssl_encrypt() 和 openssl_decrypt() 是基于RSA加密算法的加密解密函数,它们支持不同的加密算法和密钥长度。
$data = 'mydata';
$encryption_key = 'mykey';
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
$encrypted_data = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv);
这里的参数依次是:
1. 待加密的数据
2. 加密算法
3. 密钥
4. 加密选项
5. 初始化向量
$encrypted_data = 'aes-256-cbc-encrypted-data';
$decryption_key = 'mykey';
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
$decrypted_data = openssl_decrypt($encrypted_data, 'aes-256-cbc', $decryption_key, 0, $iv);
解密函数的参数和加密函数一样。
5. sodium_crypto_secretbox()和sodium_crypto_secretbox_open()
sodium_crypto_secretbox() 和 sodium_crypto_secretbox_open() 是基于Curve25519 XSalsa20 Poly1305的加密解密函数,它们提供了快速、安全、易于使用的加密解密功能。需要我们为其生成一个随机密钥 $key。
$data = 'mydata'; $key = sodium_crypto_secretbox_keygen(); $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $encrypted_data = sodium_crypto_secretbox($data, $nonce, $key);
这里的参数依次是:
1. 待加密的数据
2. 一个12字节的随机数
3. 一个密钥
$encrypted_data = 'sodium-crypto-secretbox-encrypted-data'; $key = sodium_crypto_secretbox_keygen(); $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $decrypted_data = sodium_crypto_secretbox_open($encrypted_data, $nonce, $key);
解密函数的参数和加密函数一样。
6. sodium_crypto_box()和sodium_crypto_box_open()
sodium_crypto_box()和sodium_crypto_box_open() 是使用Curve25519的基于公钥密码学的加密解密函数。它们需要使用公钥和私钥来加密和解密数据。需要我们为其生成一对公钥私钥,并将公钥传递给接收方。
$data = 'mydata'; $sender_keypair = sodium_crypto_box_keypair(); $receiver_public_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $nonce = random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES); $encrypted_data = sodium_crypto_box($data, $nonce, $receiver_public_key, $sender_keypair);
这里的参数依次是:
1. 待加密的数据
2. 一个24字节的随机数
3. 接收方的公钥
4. 发送方的密钥对
$encrypted_data = 'sodium-crypto-box-encrypted-data'; $receiver_keypair = sodium_crypto_box_keypair(); $sender_public_key = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'; $nonce = random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES); $decrypted_data = sodium_crypto_box_open($encrypted_data, $nonce, $sender_public_key, $receiver_keypair);
解密函数的参数和加密函数一样。
7. bcrypt()
bcrypt() 是一种密码哈希算法,它基于Blowfish算法,哈希强度比较高,而且比较慢。一般来说,我们使用它来哈希密码,而不是普通的数据。
$password = 'mypassword'; $hashed_password = password_hash($password, PASSWORD_BCRYPT);
比较密码和哈希密码的函数:
$password = 'mypassword';
$hashed_password = '$2y$10$ciMgQS5Jte/8fyz2HSDmquV9wkpcFmVzXY5fpVyaCEVj2h.BXp2Ri';
if (password_verify($password, $hashed_password)) {
echo 'Password is correct
';
} else {
echo 'Password is incorrect
';
}
8. sodium_crypto_pwhash()和sodium_crypto_pwhash_str_verify()
sodium_crypto_pwhash() 和 sodium_crypto_pwhash_str_verify() 是用于哈希密码的基于Argon2i的函数。我们需要一个盐字符串来哈希密码,这个盐字符串可以随机生成。
$password = 'mypassword'; $salt = random_bytes(SODIUM_CRYPTO_PWHASH_SALTBYTES); $hashed_password = sodium_crypto_pwhash_str($password, SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE, SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE);
这里的参数依次是:
1. 待哈希的密码
2. 迭代次数
3. 内存限制
比较密码和哈希密码的函数:
$password = 'mypassword';
$hashed_password = '$argon2i$v=19$m=1048576,t=2,p=1$F72cy2qFSf0bAzZl8kf+ug$UTCaYr4mFckD4JfQljLno9MB1y3EEK4DiToDRTUeW3s';
if (sodium_crypto_pwhash_str_verify($hashed_password, $password)) {
echo 'Password is correct
';
} else {
echo 'Password is incorrect
';
}
注意,Argon2i 是一种非常耗费资源的算法,因此我们需要适当增加CPU和内存限制,以使性能达到 。
9. sodium_crypto_secretstream_xchacha20poly1305_push()和sodium_crypto_secretstream_xchacha20poly1305_pull()
sodium_crypto_secretstream_xchacha20poly1305_push() 和 sodium_crypto_secretstream_xchacha20poly1305_pull() 是基于ID ChaCha20和Poly1305的加密解密函数,它们支持流式加密解密,这意味着我们可以加密和解密数据块。
`php
$data = 'mydata';
$key = sodium_crypto_secretstream_xchacha20poly1305_keygen();
$nonce = random_bytes(SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_NONCEBYTES);
$stream = sodium_crypto_secretstream_xchacha20poly1305_init_push($key);
$encrypted_data = sodium
