note 108838 deleted from book.openssl by danbrown
| From: | danbrown@php.net | Date: | Tue, 29 May 2012 18:27:48 +0000 |
| Subject: | note 108838 deleted from book.openssl by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-188280@lists.php.net to get a copy of this message | ||
Note Submitter: michelerosica at gmail dot com
----
/*-----------------------------------------------------------*
* AES: openSSL (PHP) implementation *
*-----------------------------------------------------------*/
# init
$keySizeInBits = 128;
$keySize = ($keySizeInBits / 8);
# get method
$method = getMethod($keySize, "ofb");
# get the cipher key
$salt = openssl_random_pseudo_bytes(8);
$key = pbkdf2("Secret Passphrase", $salt, 1000, $keySize);
/*-----------------------------------------------------------*
* ENCRYPT: AES 128 bit, OFB *
*-----------------------------------------------------------*/
# get iv
$iv = openssl_random_pseudo_bytes(16);
$iv64 = base64_encode($iv);
# do encryption
$raw = false; // true returns raw bytes, false returns base64
$ciphertext = openssl_encrypt("plaintext", $method, $key, $raw, $iv);
/*-----------------------------------------------------------*
* DECRYPT: AES 128 bit, OFB *
*-----------------------------------------------------------*/
# get the IV
$iv = base64_decode($iv64);
# do decryption
$plain = openssl_decrypt($ciphertext, $method, $key, $raw, $iv);
http://cryptojs.altervista.org/secretkey/doc/doc_aes_openssl-php.html