Grima  2018-08
Whispering into Alma's ear
encrypt.php
Go to the documentation of this file.
1 <?php
2 $plaintext = "message to be encrypted";
3 $cipher = "aes-128-ocb";
4 $key = random_bytes(32);
5 
6  $ivlen = openssl_cipher_iv_length($cipher);
7 print($ivlen."\n");
8 print(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES."\n");
9 
10  $iv = random_bytes($ivlen);
11  $ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv, $tag);
12 print(strlen($plaintext)."\n");
13 print(strlen($ciphertext)."\n");
14 
15  //store $cipher, $iv, and $tag for decryption later
16 
17  $original_plaintext = openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv, $tag);
18 
19  echo $original_plaintext."\n";
20 
21 $other_plaintext = sodium_crypto_secretbox_open( $ciphertext, $iv, $key);
22 
23 echo $original_plaintext."\n";
$original_plaintext
Definition: encrypt.php:17
$key
Definition: encrypt.php:4
$cipher
Definition: encrypt.php:3
$plaintext
Definition: encrypt.php:2
$iv
Definition: encrypt.php:10
$ivlen
Definition: encrypt.php:6
$other_plaintext
Definition: encrypt.php:21
$ciphertext
Definition: encrypt.php:11