xxxxxxxxxx
<?php
// Store a string into the variable which
// need to be Encrypted
$simple_string = "Welcome to GeeksforGeeks";
// Display the original string
echo "Original String: " . $simple_string . "\n";
// Store cipher method
$ciphering = "BF-CBC";
// Use OpenSSl encryption method
$iv_length = openssl_cipher_iv_length($ciphering);
$options = 0;
// Use random_bytes() function which gives
// randomly 16 digit values
$encryption_iv = random_bytes($iv_length);
// Alternatively, we can use any 16 digit
// characters or numeric for iv
$encryption_key = openssl_digest(php_uname(), 'MD5', TRUE);
// Encryption of string process starts
$encryption = openssl_encrypt($simple_string, $ciphering,
$encryption_key, $options, $encryption_iv);
// Display the encrypted string
echo "Encrypted String: " . $encryption . "\n";
// Decryption of string process starts
// Used random_bytes() which gives randomly
// 16 digit values
$decryption_iv = random_bytes($iv_length);
// Store the decryption key
$decryption_key = openssl_digest(php_uname(), 'MD5', TRUE);
// Descrypt the string
$decryption = openssl_decrypt ($encryption, $ciphering,
$decryption_key, $options, $encryption_iv);
// Display the decrypted string
echo "Decrypted String: " . $decryption;
?>
xxxxxxxxxx
# Simple XOR-based symmetric encryption and decryption
def simple_xor_encrypt_decrypt(input_bytes, key):
encrypted = []
for byte in input_bytes:
encrypted_byte = byte ^ key
encrypted.append(encrypted_byte)
return bytes(encrypted)
if __name__ == '__main__':
message = "This is a secret message."
key = 42 # Replace this with your own secret key (an integer)
# Encrypt the message
encrypted_message = simple_xor_encrypt_decrypt(message.encode(), key)
print("Encrypted message:", encrypted_message)
# Decrypt the message
decrypted_message = simple_xor_encrypt_decrypt(encrypted_message, key)
print("Decrypted message:", decrypted_message.decode())