Backend Development 4 min read

Encrypting and Decrypting Form Data with PHP

This article explains how to securely encrypt and decrypt user‑submitted form data in PHP using the AES‑256‑CBC algorithm, providing step‑by‑step code examples for creating encryption/decryption functions, applying them to form inputs, and emphasizing key safety and HTTPS.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Encrypting and Decrypting Form Data with PHP

In web development, handling user‑submitted data securely often requires encrypting sensitive information before storage or transmission.

1. Data Encryption

Import Encryption Library

PHP provides built‑in libraries such as OpenSSL; this example uses the AES‑256‑CBC algorithm.

<?php
// 引入加密函数库
require_once('encryption_functions.php');
// 设定加密算法和加密密钥
$encryptionAlgorithm = 'AES-256-CBC';
$encryptionKey = 's1mp13k3y';
?>

Create Encryption Function

The encryptData function generates an IV, encrypts the data with OpenSSL, and returns the base64‑encoded ciphertext concatenated with the IV.

<?php
function encryptData($data, $encryptionKey, $encryptionAlgorithm) {
    // 生成加密初始向量
    $encryptionIV = openssl_random_pseudo_bytes(openssl_cipher_iv_length($encryptionAlgorithm));
    // 对数据进行加密
    $encryptedData = openssl_encrypt($data, $encryptionAlgorithm, $encryptionKey, 0, $encryptionIV);
    // 拼接加密数据和初始向量
    $encryptedDataWithIV = base64_encode($encryptedData . '::' . $encryptionIV);
    return $encryptedDataWithIV;
}
?>

Use Encryption Function

Before processing the form, retrieve the posted values and encrypt the password.

<?php
// 获取表单提交的数据
$username = $_POST['username'];
$password = $_POST['password'];
// 对密码进行加密
$encryptedPassword = encryptData($password, $encryptionKey, $encryptionAlgorithm);
?>

2. Data Decryption

Create Decryption Function

The decryptData function splits the stored string, extracts the IV, and decrypts the ciphertext using the same algorithm and key.

<?php
function decryptData($encryptedData, $encryptionKey, $encryptionAlgorithm) {
    // 拆分加密数据和初始向量
    $encryptedDataWithIV = base64_decode($encryptedData);
    list($encryptedData, $encryptionIV) = explode('::', $encryptedDataWithIV, 2);
    // 对数据进行解密
    $decryptedData = openssl_decrypt($encryptedData, $encryptionAlgorithm, $encryptionKey, 0, $encryptionIV);
    return $decryptedData;
}
?>

Use Decryption Function

When the encrypted data is needed, call the decryption function to retrieve the original value.

<?php
// 解密密码
$decryptedPassword = decryptData($encryptedPassword, $encryptionKey, $encryptionAlgorithm);
?>

3. Conclusion

By following these steps, developers can easily encrypt and decrypt form data in PHP, ensuring that sensitive information such as passwords is protected; it is also important to safeguard the encryption key and use additional measures like HTTPS.

phpencryptiondecryptionForm Security
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.