Skip to content

Instantly share code, notes, and snippets.

@fabiopaiva
Created August 31, 2015 13:03
Show Gist options
  • Save fabiopaiva/107413b31bb300f5c89b to your computer and use it in GitHub Desktop.
Save fabiopaiva/107413b31bb300f5c89b to your computer and use it in GitHub Desktop.
Crypt/Decrypt a string or text usign zend/crypt
{
"require": {
"zendframework/zend-crypt":"dev-master"
}
}
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Zend\Crypt\BlockCipher;
$privateKey = 'a1w2b4c6v7b6fcdbatyr';
$blockCipher = BlockCipher::factory('mcrypt', array('algo' => 'aes'));
$blockCipher->setKey($privateKey);
$result = $blockCipher->encrypt('Tutorial crypt/decrypt');
echo 'Crypt: ' . $result . PHP_EOL;
$encrypted = '0c3d3e539040028b7fadf55c084694cdd7536d208eb19d1d26281183efc1d2f7RMyNXF1Q9d9qHppFm6TrD8eQNAcVLBWFAgyGSu5sJqWXtj/KijzzH5RhUvye6sd+';
$result = $blockCipher->decrypt($encrypted);
echo 'Decrypt: ' . $result . PHP_EOL;
$ php index.php
Crypt: c3c48afc00550bddeb058f8f6794a49aaae02eed9688dd05d69f705b400b43e3wCQNJSEQaTwWBExxJBkZdXkfofhvyCot7USEKRphp9ajjm6Hkk9qvKUOdeqB6QIo
Decrypt: Tutorial crypt/decrypt

For each call, the output hash is different

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment