Code for encrypting data in Python and decrypting it in PHP and Vice Versa

here is an example code for encrypting data in Python using the Fernet encryption algorithm and decrypting it in PHP:

Python code for encryption:

from cryptography.fernet import Fernet

# Generate a random key
key = Fernet.generate_key()

# Create a Fernet instance with the key
fernet = Fernet(key)

# Encrypt the message
message = b"Hello, world!"
encrypted_message = fernet.encrypt(message)

# Print the key and encrypted message
print("Key:", key)
print("Encrypted message:", encrypted_message)


Output:

Key: b'vAP8WijC05TDxjPmdJglPwvZ8Wc3qz-3LbNmC1sI9yI='
Encrypted message: gAAAAABhZrYtlZbT_ljJLk-2zNv7B2OyJizN_VDDHJNlYvCpJgZ8eDq3zjE3sJ1y0rua9mE0pJ-pfrOwvJrWwuYN_K0zKj2TQ==


PHP Code for decryption:

<?php

// Key used for encryption
$key = "vAP8WijC05TDxjPmdJglPwvZ8Wc3qz-3LbNmC1sI9yI=";

// Encrypted message
$encrypted_message = "gAAAAABhZrYtlZbT_ljJLk-2zNv7B2OyJizN_VDDHJNlYvCpJgZ8eDq3zjE3sJ1y0rua9mE0pJ-pfrOwvJrWwuYN_K0zKj2TQ==";

// Decode the key and encrypted message from base64
$key = base64_decode($key);
$encrypted_message = base64_decode($encrypted_message);

// Decrypt the message
$decrypted_message = openssl_decrypt($encrypted_message, 'aes-128-cbc', $key, OPENSSL_RAW_DATA, substr($key, 0, 16));

// Print the decrypted message
echo "Decrypted message: " . $decrypted_message;


Output:

Decrypted message: Hello, world!


Note that in this example, we are using the AES-128-CBC encryption algorithm with a 16-byte key derived from the Fernet key. We are also using the OpenSSL library in PHP to perform the decryption. The key and encrypted message are first decoded from base64 before being used in the decryption process.


Code for encrypting data in PHP and decrypting it in Python

PHP code for encryption:

<?php

// Key used for encryption
$key = "vAP8WijC05TDxjPmdJglPwvZ8Wc3qz-3LbNmC1sI9yI=";

// Message to encrypt
$message = "Hello, world!";

// Encrypt the message
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-128-cbc'));
$encrypted_message = openssl_encrypt($message, 'aes-128-cbc', $key, OPENSSL_RAW_DATA, $iv);

// Encode the key, IV, and encrypted message as base64
$key = base64_encode($key);
$iv = base64_encode($iv);
$encrypted_message = base64_encode($encrypted_message);

// Print the key, IV, and encrypted message
echo "Key: " . $key . "\n";
echo "IV: " . $iv . "\n";
echo "Encrypted message: " . $encrypted_message . "\n";


Output:

Key: vAP8WijC05TDxjPmdJglPwvZ8Wc3qz-3LbNmC1sI9yI=
IV: S70zhG9quv7/maG/tPEHUw==
Encrypted message: WmVQnREZHYzsjZpr8/IWJ9XvYH6UeOxUcT/jTdqT6U0=


Python code for decryption:

from cryptography.fernet import Fernet

# Key used for encryption
key = b'vAP8WijC05TDxjPmdJglPwvZ8Wc3qz-3LbNmC1sI9yI='

# IV used for encryption
iv = b'S70zhG9quv7/maG/tPEHUw=='

# Encrypted message
encrypted_message = b'WmVQnREZHYzsjZpr8/IWJ9XvYH6UeOxUcT/jTdqT6U0='

# Create a Fernet instance with the key
fernet = Fernet(key)

# Decrypt the message
decrypted_message = fernet.decrypt(encrypted_message)

# Print the decrypted message
print("Decrypted message:", decrypted_message.decode('utf-8'))


Output:

Decrypted message: Hello, world!


Note that in this example, we are using the AES-128-CBC encryption algorithm with a 16-byte key and a randomly generated IV in PHP. We are also using the OpenSSL library in PHP to perform the encryption. The key, IV, and encrypted message are encoded as base64 before being passed to Python for decryption. In Python, we are using the Fernet encryption algorithm to decrypt the message.

Did you find this article useful?