Home  >  Article  >  Backend Development  >  How to do symmetric and asymmetric encryption in PHP?

How to do symmetric and asymmetric encryption in PHP?

WBOY
WBOYOriginal
2023-05-21 15:10:402422browse

In the field of network security, encryption technology is a very important technical means, which can encrypt and decrypt data to ensure data security. As a popular server-side programming language, PHP also provides support for symmetric and asymmetric encryption to meet the needs of different application scenarios.

  1. Symmetric encryption

Symmetric encryption refers to an encryption method that uses the same key for encryption and decryption. There are many symmetric encryption algorithms, such as DES, 3DES, AES, etc. In PHP, symmetric encryption can be implemented using the functions provided by the mcrypt extension library.

The sample code is as follows:

$key = '123456789'; // 密钥
$data = 'Hello World'; // 待加密字符串
$iv = mcrypt_create_iv(mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC), MCRYPT_RAND);
$cipher = mcrypt_encrypt(MCRYPT_DES, $key, $data, MCRYPT_MODE_CBC, $iv);
echo base64_encode($cipher); // 输出加密后的字符串

In the above code, DES encryption is performed through the mcrypt_encrypt function, and the MCRYPT_MODE_CBC mode is used for filling. $key is the key for symmetric encryption, $data is the plaintext string to be encrypted, and $iv is a random initialization vector.

The decryption sample code is as follows:

$cipher = base64_decode('JnFXVC9e+rzR8oUgM67Q0w=='); // 加密后的字符串
$key = '123456789'; // 密钥
$iv = mcrypt_create_iv(mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC), MCRYPT_RAND);
$plain = mcrypt_decrypt(MCRYPT_DES, $key, $cipher, MCRYPT_MODE_CBC, $iv);
echo $plain; // 输出解密后的字符串
  1. Asymmetric encryption

The asymmetric encryption algorithm requires a pair of keys for encryption and decryption, where the public key The key can be made public, but the private key must be kept secret. Commonly used asymmetric encryption algorithms include RSA, DSA, etc. In PHP, asymmetric encryption can be implemented using the functions provided by the openssl extension library.

The sample code is as follows:

$data = 'Hello World'; // 待加密字符串
$res = openssl_pkey_new(); // 生成密钥对
openssl_pkey_export($res, $privateKey); // 获取私钥
$publicKey = openssl_pkey_get_details($res)["key"]; // 获取公钥
openssl_public_encrypt($data, $cipher, $publicKey); // 加密数据
echo base64_encode($cipher); // 输出加密后的字符串

In the above code, first use the openssl_pkey_new function to generate a pair of public and private keys, and then use the openssl_public_encrypt function to encrypt the data. The $publicKey obtained at this time can be made public, but the $privateKey must be kept secret.

The decryption sample code is as follows:

$cipher = base64_decode('RvnOeNe+p8LtlpuUcq0wqFoX2yUaGXvY6FHlWpT0NQwzVQwVcDSnkw4C6YJMvnsMH4N5JDP18RI8x7CFqUCdJg=='); // 加密后的字符串
$privateKey = '-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----'; // 私钥
openssl_private_decrypt($cipher, $plain, $privateKey); // 解密数据
echo $plain; // 输出解密后的字符串

In the above code, first use the openssl_pkey_export function to obtain the string form of the private key, and then use the openssl_private_decrypt function to decrypt the encrypted data and obtain the original plaintext .

Summary:

Through the above introduction, we learned that in PHP, you can use the mcrypt and openssl extension libraries to implement symmetric and asymmetric encryption. For different encryption requirements in application scenarios, we can flexibly choose to use different encryption algorithms and encryption methods. Of course, during the actual encryption process, we also need to pay attention to the protection and management of keys to ensure data security.

The above is the detailed content of How to do symmetric and asymmetric encryption in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn