Home  >  Article  >  Backend Development  >  How to implement information encryption and decryption in PHP

How to implement information encryption and decryption in PHP

WBOY
WBOYOriginal
2023-06-18 10:10:392805browse

With the rapid development of information technology, information encryption and decryption have gradually become one of the important means of protecting digital information today. As a programming language widely used in the field of Web development, PHP's method of encrypting and decrypting information has also become very important.

In this article, I will introduce several methods on how to implement information encryption and decryption in PHP.

1. Use PHP’s own encryption functions

In PHP, we can use a series of built-in encryption functions to encrypt and decrypt data. These functions include md5(), sha1(), base64_encode(), base64_decode(), etc.

First, we select the base64_encode() and base64_decode() functions for demonstration. They can convert data into a set of strings consisting of A-Z, a-z, 0-9, , / characters and restore the original data after decryption.

The specific implementation code is as follows:

//加密函数
function encrypt($data){
    $data = base64_encode($data);
    return $data;
}

//解密函数
function decrypt($data){
    $data = base64_decode($data);
    return $data;
}

Through these two functions, we can achieve simple encryption and decryption of data. However, it should be noted that this method is not very secure as it can be easily brute-forced.

2. Use symmetric encryption algorithm

Symmetric encryption algorithm is an algorithm that uses the same key to encrypt and decrypt data. Common symmetric encryption algorithms include DES, AES, etc.

In PHP, we can use the mcrypt library or openssl extension to implement the symmetric encryption algorithm.

We take the AES algorithm as an example to show the implementation method of the symmetric encryption algorithm. The specific implementation code is as follows:

$key = '1234567890123456'; //密钥
$plaintext = 'Hello, world!'; //原始明文数据

//加密
$ciphertext = openssl_encrypt($plaintext, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, '1234567890123456');
$ciphertext = base64_encode($ciphertext);

//解密
$ciphertext = base64_decode($ciphertext);
$plaintext = openssl_decrypt($ciphertext, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, '1234567890123456');

Among them, $key is the key and $plaintext is the original plaintext data. Encryption uses the openssl_encrypt() function, and decryption uses the openssl_decrypt() function.

It should be noted that the choice of encryption algorithm should be based on the actual situation. If you consider a more secure encryption method, you can choose the AES-256 algorithm, but the encryption speed will be relatively slower.

3. Use asymmetric encryption algorithm

The asymmetric encryption algorithm is an algorithm that uses a pair of keys (public key and private key) to encrypt and decrypt data. Common non- The symmetric encryption algorithm is RSA.

In PHP, we can use openssl extension to implement asymmetric encryption algorithm.

We take the RSA algorithm as an example to show the implementation method of the asymmetric encryption algorithm. The specific implementation code is as follows:

$private_key = <<<EOD
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQC7C1DJoA4vf6Z+/gwTt3eOjQqi7odXbVf/eKeTX2YVi2wS+Z02
Jbcm8+pkuGZDNxXgO89qr13ytGWUhcSy2t/hRMmLjAdEIiCU8a9VcdDsk1Bgz4Ir
qKLj84XdT3pc5N9yMSsEItvqn31ro7/5nDdzRJAsWH6xd220EaJLZL7/hwIDAQAB
AoGBAIJc3cZhytX5jni/jdDa7+keAwDTbLsPAx1pyJvZbIZ26v8f8cxVgCD1yJ0+
Fm6fi/BTcCfi5UOD6ClIdYxQ9ZsHkhDkU+z8r114kguNziHkrm2kR/ijXmYdNDaU
Y9jNtRjvMHOQWfj+ym9p2jKJd/wYG1TodY5AVMJeSLvjo/7RAkEA/6je8XnhVADo
xMGs/eJ80AYEVo11+6PjxVVqivJ0O0l8DNd+4ih+U2ohjvQEjBpfcUGWx+cpxhR8
iE5TgrPXswJBANg/xhNhMNqL4DsXdc0y37n68QAVR/+SNYkipUZqHSIqe6LH4db/
H09kWeZJQ4ArtiIwgl5Q5dgngnAubdVreRUCQDXN287lEns+hIn7GUYWCO5V4fND
WbM8K02UaO+qutglLrbzIlLa6+IjYb+7p8adR/K66QiLJe6UdMze3/Nlzi0CQQCV
Xa5m6m1Q7lfALG53u0dvpVx3JmIEPZvfYh8onMyODFfF4PPFNZwgIjlbSRVTFxJ1
hW2PWF1EBbJ2vEI7tWnBAkEAt0LnrmfkApmwhPiEKzjvezUnTeqWcZaImp7uZ8V7
av2Q43WTXNQeKAzI4ItZHvEKtN0Xrnvah7NiW7K7ZQA25w==
-----END RSA PRIVATE KEY-----
EOD;

$public_key = <<<EOD
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC7C1DJoA4vf6Z+/gwTt3eOjQqi
7odXbVf/eKeTX2YVi2wS+Z02Jbcm8+pkuGZDNxXgO89qr13ytGWUhcSy2t/hRMmL
jAdEIiCU8a9VcdDsk1Bgz4IrqKLj84XdT3pc5N9yMSsEItvqn31ro7/5nDdzRJAs
WH6xd220EaJLZL7/hwIDAQAB
-----END PUBLIC KEY-----
EOD;

$plaintext = 'Hello, world!';

//私钥加密
openssl_private_encrypt($plaintext, $ciphertext, $private_key);
$ciphertext = base64_encode($ciphertext);

//公钥解密
$ciphertext = base64_decode($ciphertext);
openssl_public_decrypt($ciphertext, $plaintext, $public_key);

Among them, $private_key is the private key, $public_key is the public key, and $plaintext is the original plaintext data. Encryption uses the openssl_private_encrypt() function, and decryption uses the openssl_public_decrypt() function.

It should be noted that the encryption speed of the RSA algorithm is slow, so it is not suitable for encrypting large amounts of data. In practical applications, we can use a combination of symmetric encryption algorithms and asymmetric encryption algorithms. First use the symmetric encryption algorithm to encrypt the data, and then use the asymmetric encryption algorithm to encrypt the key used by the symmetric encryption algorithm.

To sum up, the above are several methods for PHP to implement information encryption and decryption. In practical applications, we should consider which encryption method to choose based on actual needs and security.

The above is the detailed content of How to implement information encryption and decryption 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