Home  >  Article  >  Backend Development  >  What are the methods for php encryption and decryption?

What are the methods for php encryption and decryption?

WBOY
WBOYOriginal
2024-02-23 15:09:04861browse

What are the methods for php encryption and decryption?

What are the methods of PHP encryption and decryption? Specific code examples are required

With the continuous development of network technology, data security has attracted more and more attention. In the web development process, handling and protecting users' sensitive information is an essential part.

PHP, as a scripting language widely used in web development, provides a variety of methods in data encryption and decryption. Several commonly used encryption and decryption methods will be introduced below and corresponding code examples will be provided.

1. Symmetric encryption
The symmetric encryption algorithm uses the same key for encryption and decryption. Common symmetric encryption algorithms include DES, 3DES, AES, etc.

  1. DES encryption and decryption

function des_encrypt($data, $key) {

$data = openssl_encrypt($data, 'DES-ECB', $key, OPENSSL_RAW_DATA);
return base64_encode($data);

}

function des_decrypt($data, $key) {

$data = base64_decode($data);
return openssl_decrypt($data, 'DES-ECB', $key, OPENSSL_RAW_DATA);

}

$key = 'secret_key';
$data = 'Hello, world!';
$ encrypted_data = des_encrypt($data, $key);
$decrypted_data = des_decrypt($encrypted_data, $key);
echo $encrypted_data . "
";
echo $decrypted_data . "
";
?>

  1. AES encryption and decryption

function aes_encrypt($data, $key) {

$data = openssl_encrypt($data, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, '1234567890123456');
return base64_encode($data);

}

function aes_decrypt($data, $key) {

$data = base64_decode($data);
return openssl_decrypt($data, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, '1234567890123456');

}

$key = 'secret_key';
$data = 'Hello, world!';
$encrypted_data = aes_encrypt($data, $key);
$decrypted_data = aes_decrypt($encrypted_data, $key);
echo $encrypted_data . "
";
echo $decrypted_data . "
";
?>

2. Asymmetric encryption
The asymmetric encryption algorithm uses public key encryption and private key decryption. Common asymmetric encryption algorithms include RSA, DSA, etc.

  1. RSA encryption and decryption

function rsa_encrypt($data, $public_key) {

openssl_public_encrypt($data, $encrypted, $public_key);
return base64_encode($encrypted);

}

function rsa_decrypt($data, $private_key) {

openssl_private_decrypt(base64_decode($data), $decrypted, $private_key);
return $decrypted;

}

$public_key = file_get_contents('public.key');
$private_key = file_get_contents('private.key ');
$data = 'Hello, world!';
$encrypted_data = rsa_encrypt($data, $public_key);
$decrypted_data = rsa_decrypt($encrypted_data, $private_key);
echo $encrypted_data . "
";
echo $decrypted_data . "
";
?>

3. Hash algorithm
The hash algorithm is a one-way Encryption algorithm, which cannot be decrypted, is often used for password storage and data integrity verification. Common hashing algorithms include MD5, SHA1, SHA256, etc.

  1. MD5 Hash

$data = 'Hello, world!';
$hashed_data = md5($data);
echo $hashed_data . "
";
?>

  1. SHA256 Hash

$data = 'Hello, world!';
$hashed_data = hash('sha256', $data);
echo $hashed_data . "
";
?>

The above is Commonly used encryption and decryption methods in PHP and corresponding code examples. In practical applications, appropriate encryption algorithms and methods need to be selected according to specific circumstances to ensure data security and reliability. At the same time, attention should also be paid to the safe storage and management of keys to prevent the leakage of keys from causing data to be cracked.

The above is the detailed content of What are the methods for php encryption and decryption?. 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