aes encrypt in php

To AES encrypt in PHP, you can use the openssl_encrypt() function. Here is an example code block for AES-256 encryption:

main.php
// Set the encryption key and IV
$key = 'mysecretkey12345';
$iv = '1234567890123456';

// Set the plaintext message to be encrypted
$message = 'This is a secret message.';

// Encrypt the message using AES-256-CBC
$encrypted_message = openssl_encrypt($message, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);

// Convert the encrypted message to base64 encoding
$base64_encrypted_message = base64_encode($encrypted_message);

// Output the encrypted message
echo 'Encrypted message: ' . $base64_encrypted_message;
511 chars
16 lines

In the code above, we set the encryption key and initialization vector (IV) to be used with AES-256-CBC encryption. We then set the plaintext message to be encrypted and use the openssl_encrypt() function to generate the encrypted message. Finally, we convert the encrypted message to base64 encoding and output it.

Note that you should always use a strong and secure encryption key, as well as a random and unpredictable IV, to ensure the security of your encrypted messages.

gistlibby LogSnag