阅读量:0
- 问题:如何生成随机的密钥和IV? 解决方法:可以使用openssl_random_pseudo_bytes()函数生成随机的密钥和IV。示例代码如下:
$key = openssl_random_pseudo_bytes(32); $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
- 问题:如何使用PHP进行AES加密和解密? 解决方法:可以使用openssl_encrypt()和openssl_decrypt()函数进行AES加密和解密。示例代码如下:
$data = 'Hello, World!'; $cipher = 'aes-256-cbc'; $key = 'your_secret_key'; $iv = 'your_iv'; $encrypted = openssl_encrypt($data, $cipher, $key, 0, $iv); $decrypted = openssl_decrypt($encrypted, $cipher, $key, 0, $iv); echo $encrypted . "\n"; echo $decrypted . "\n";
- 问题:如何使用公钥和私钥进行RSA加密和解密? 解决方法:可以使用openssl_public_encrypt()和openssl_private_decrypt()函数进行RSA加密和解密。示例代码如下:
$data = 'Hello, World!'; $publicKey = file_get_contents('public.pem'); $privateKey = file_get_contents('private.pem'); openssl_public_encrypt($data, $encrypted, $publicKey); openssl_private_decrypt($encrypted, $decrypted, $privateKey); echo $encrypted . "\n"; echo $decrypted . "\n";
- 问题:如何使用PHP进行数字签名和验证? 解决方法:可以使用openssl_sign()和openssl_verify()函数进行数字签名和验证。示例代码如下:
$data = 'Hello, World!'; $privateKey = file_get_contents('private.pem'); $publicKey = file_get_contents('public.pem'); openssl_sign($data, $signature, $privateKey, OPENSSL_ALGO_SHA256); $verified = openssl_verify($data, $signature, $publicKey, OPENSSL_ALGO_SHA256); echo $signature . "\n"; echo $verified . "\n";