Advertisement
Guest User

OpenSSL Encryption/Decryption

a guest
Sep 21st, 2015
1,182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.78 KB | None | 0 0
  1. <?php
  2. $data = "hello! this is a test!";
  3. $method = 'aes-256-cbc';
  4. $key = '59b6ab46d379b89d794c87b74a511fbd59b6ab46d379b89d794c87b74a511fbd';
  5. $iv = '0aaff094b6dc29742cc98a4bac8bc8f9';
  6.  
  7. $e = openssl_encrypt( $data, $method, hex2bin( $key ), 0, hex2bin( $iv ));
  8.  
  9. echo 'Ciphertext: [', bin2hex( base64_decode( $e )), "]\n";
  10. echo 'Key:        [', $key, "]\n";
  11. echo 'Cleartext:  [', openssl_decrypt( $e, $method, hex2bin( $key ), 0, hex2bin( $iv )), "]\n";
  12.  
  13. // Test with openssl on the command line as well, just to be sure!
  14. file_put_contents( 'clear.txt', $data );
  15.  
  16. $exec = "openssl enc -$method -e -in clear.txt -out encrypted.txt -base64 -nosalt -K $key -iv $iv";
  17. exec ($exec);
  18. $out = file_get_contents( 'encrypted.txt' );
  19. echo 'Ciphertext: [', bin2hex( base64_decode(trim($out))), "]\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement