本文整理汇总了PHP中openssl_encrypt函数的典型用法代码示例。如果您正苦于以下问题:PHP openssl_encrypt函数的具体用法?PHP openssl_encrypt怎么用?PHP openssl_encrypt使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了openssl_encrypt函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: encrypt_decrypt
function encrypt_decrypt($action, $string)
{
/* =================================================
* ENCRYPTION-DECRYPTION
* =================================================
* ENCRYPTION: encrypt_decrypt('encrypt', $string);
* DECRYPTION: encrypt_decrypt('decrypt', $string) ;
*/
$output = false;
$encrypt_method = "AES-256-CBC";
$secret_key = 'WS-SERVICE-KEY';
$secret_iv = 'WS-SERVICE-VALUE';
// hash
$key = hash('sha256', $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);
if ($action == 'encrypt') {
$output = base64_encode(openssl_encrypt($string, $encrypt_method, $key, 0, $iv));
} else {
if ($action == 'decrypt') {
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
}
return $output;
}
示例2: encrypt
/**
*
* @param string $data
* @param string $iv
* @param string $key
* @return string|false
*/
public function encrypt($data, $iv, $key)
{
// Max. 2^32 blocks with a same key (not realistic in a web application).
$cipherText = openssl_encrypt($data, 'AES-128-CBC', $key, true, $iv);
unset($data, $iv, $key);
return $cipherText;
}
示例3: encrypt
/**
* @param $data
* @param $passphrase
* @param null $salt ONLY FOR TESTING
* @return string encrypted data in base64 OpenSSL format
*/
public static function encrypt($data, $passphrase, $salt = null)
{
$salt = $salt ?: openssl_random_pseudo_bytes(8);
list($key, $iv) = self::evpkdf($passphrase, $salt);
$ct = openssl_encrypt($data, 'aes-256-cbc', $key, true, $iv);
return self::encode($ct, $salt);
}
示例4: encrypt
/**
* Gera um novo arquivo criptografado
* @param string $from Arquivo original
* @param string $to Novo arquivo
* @return Array Retorna a classe \SplFileInfo com as informações do novo arquivo
*/
public function encrypt($from, $to)
{
$iv = $this->createIv();
$data = openssl_encrypt(file_get_contents($from), $this->cipher_method, $this->password, OPENSSL_RAW_DATA, $iv);
file_put_contents($to, $iv . $this->password . $data);
return new \SplFileInfo($to);
}
示例5: encrypt
public static function encrypt($data, $password, $IV, $AAD)
{
if (self::useOpenSSL()) {
$method = self::getMethod($password);
$encrypt = openssl_encrypt($data, $method, $password, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $IV, $tag, $AAD);
} else {
if (self::useSO()) {
try {
$cipher = \Crypto\Cipher::aes(\Crypto\Cipher::MODE_GCM, self::bitLen($password));
$cipher->setAAD($AAD);
$encrypt = $cipher->encrypt($data, $password, $IV);
$tag = $cipher->getTag();
} catch (\Exception $e) {
//echo $e->getMessage();
return false;
}
} else {
try {
list($encrypt, $tag) = AESGCM::encrypt($password, $IV, $data, $AAD);
} catch (\Exception $e) {
//echo $e->getMessage();
return false;
}
}
}
return $encrypt . $tag;
}
示例6: actionIndex
public function actionIndex()
{
$user = \Yii::$app->user->identity;
$parent = null;
$child = null;
if (!\Yii::$app->user->can('admin')) {
$user->parent_id ? $parent = \common\models\User::findOne(['id' => $user->parent_id]) : '';
$child = new \yii\data\ActiveDataProvider(['query' => \common\models\User::find()->where(['parent_id' => $user->id])]);
} else {
$userList = \common\models\User::find()->where(['parent_id' => null])->orderBy('id')->all();
$tree = [];
foreach ($userList as $key => $item) {
$tree[] = $item;
$branch = $this->makeTree($item->id, 0, array());
$tree = array_merge($tree, $branch);
}
$userList = $tree;
// print_r('<pre>');
// print_r($userList);
// print_r('</pre>');
// die();
}
$crypt = openssl_encrypt($user->email, 'aes-128-ecb', '304c6528f659c77866a510d9c1d6ae5e', false);
return $this->render('index', ['parent' => $parent, 'child' => $child, 'crypt' => $crypt, 'userList' => $userList]);
}
示例7: encrypt
public function encrypt($input, $times = 1)
{
for ($i = 0; $i < $times; $i++) {
$input = openssl_encrypt($input, $this->method, $this->key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $this->iv);
}
return $input;
}
示例8: run
/**
* Run
*/
public function run()
{
// Try to get username
$username = $this->prompt('Username', false);
// No username, abort
if (strlen($username) === 0) {
$this->abort('No username provided');
}
// Try to get password
$password = $this->prompt('Password', true);
// No password, abort
if (strlen($password) === 0) {
$this->abort('No password provided');
}
// Encode username
$encoded_username = openssl_encrypt($username, $this->app->getConfig('INI_SALT_METHOD'), $this->app->getConfig('INI_USERNAME_SALT'), 0, $this->app->getConfig('INI_USERNAME_IV'));
// Encode password
$encoded_password = openssl_encrypt($password, $this->app->getConfig('INI_SALT_METHOD'), $this->app->getConfig('INI_PASSWORD_SALT'), 0, $this->app->getConfig('INI_PASSWORD_IV'));
// Create file contents
$content = implode("\n", array('username = "' . $encoded_username . '"', 'password = "' . $encoded_password . '"', ''));
// Target filename
$file = sprintf('%s/%s', $this->app->getConfig('ROOT_PATH'), $this->app->getConfig('INI_FILENAME'));
// Failed to write a file
if (!@file_put_contents($file, $content)) {
$this->abort('Failed to create credentials file %s', $file);
}
// Success
$this->line('Credentials file %s was created for future logins', $file);
}
示例9: encrypt_decrypt
function encrypt_decrypt($action, $string, $key)
{
$output = false;
global $encryption_method;
// Pull the hashing method that will be used
// Hash the password
$key = hash('sha256', $key);
if ($action == 'encrypt') {
// Generate a random string, hash it and get the first 16 character of the hashed string which will be ised as the IV
$str = "qwertyuiopasdfghjklzxcvbnm,./;'\\[]-=`!@#\$%^&*()_+{}|\":?><0123456789QWERTYUIOPASDFGHJKLZXCVBNM";
$shuffled = str_shuffle($str);
$iv = substr(hash('sha256', $shuffled), 0, 16);
$output = openssl_encrypt($string, $encryption_method, $key, 0, $iv);
$output = base64_encode($output);
// Tidy up the string so that it survives the transport 100%
$ivoutput = $iv . $output;
// Concat the IV with the encrypted message
return $ivoutput;
} else {
if ($action == 'decrypt') {
$iv = substr($string, 0, 16);
// Extract the IV from the encrypted string
$string = substr($string, 16);
// The rest of the encrypted string is the message
$output = openssl_decrypt(base64_decode($string), $encryption_method, $key, 0, $iv);
return $output;
}
}
}
示例10: encrypt
private function encrypt($plaintext)
{
// Use a random IV
$iv = openssl_random_pseudo_bytes(16);
// Use IV as first block of ciphertext
return $iv . openssl_encrypt($plaintext, "AES-128-CBC", $this->encryption_key, OPENSSL_RAW_DATA, $iv);
}
示例11: encrypt_decrypt
function encrypt_decrypt($action, $string)
{
if (!function_exists("openssl_encrypt")) {
die("openssl function openssl_encrypt does not exist");
}
if (!function_exists("hash")) {
die("function hash does not exist");
}
global $encryption_key;
$output = false;
$encrypt_method = "AES-256-CBC";
//echo "$encryption_key\n";
$secret_iv = 'RgX54.Ju7h';
// hash
$key = hash('sha256', $encryption_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);
if ($action == 'encrypt') {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
} else {
if ($action == 'decrypt') {
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
}
return $output;
}
示例12: encrypt
/**
* Encrypt with AES-256-CTR + HMAC-SHA-512
*
* @param string $plaintext Your message
* @param string $encryptionKey Key for encryption
* @param string $macKey Key for calculating the MAC
* @return string
*/
public static function encrypt($plaintext, $encryptionKey, $macKey)
{
$nonce = openssl_random_pseudo_bytes(16);
$ciphertext = openssl_encrypt($plaintext, 'aes-256-ctr', $encryptionKey, OPENSSL_RAW_DATA, $nonce);
$mac = hash_hmac('sha512', $nonce . $ciphertext, $macKey, true);
return base64_encode($mac . $nonce . $ciphertext);
}
示例13: encrypt
/**
* Performs text encryption with openssl_encrypt and returns it as a string.<br />
* If openssl_encrypt is not available encrypts with mcrypt, if mcrypt is not available encrypts with xor
*
* @param string $text The text to encode
* @param string $key [optionnal] The key to use. Default is the application key
* @return string The encrypted string
*/
public static function encrypt($text, $key = null)
{
// Get the application key if no key is given
if ($key === null) {
$key = self::_getKey();
}
// To avoid same encoded string for the same string
$text = self::hash($text) . '~~~' . $text;
// If zlib is active we compress the value to crypt
if (function_exists('gzdeflate')) {
$text = gzdeflate($text, 9);
}
// Use openssl_encrypt with PHP >= 5.3.0
if (Config::get('general.crypt_method', 'openssl') === 'openssl' && function_exists('openssl_encrypt') && in_array('BF-ECB', openssl_get_cipher_methods())) {
$method = 'BF-ECB';
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($method));
return strtr(openssl_encrypt($text, $method, $key), '+/', '-_');
} else {
if (function_exists('mcrypt_encrypt')) {
$size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($size, MCRYPT_RAND);
$crypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
return rtrim(strtr(base64_encode($crypt), '+/', '-_'), '=');
}
}
// ... else encrypt with xor technique
$n = mb_strlen($text, '8bit');
$m = mb_strlen($key, '8bit');
if ($n !== $m) {
$key = mb_substr(str_repeat($key, ceil($n / $m)), 0, $n, '8bit');
}
return base64_encode($text ^ $key);
}
示例14: persist
/**
* @inheritdoc
*/
public function persist(\Bitpay\KeyInterface $key)
{
$path = $key->getId();
$data = serialize($key);
$encoded = bin2hex(openssl_encrypt($data, self::METHOD, $this->password, 1, self::IV));
file_put_contents($path, $encoded);
}
示例15: encrypt
/**
* Encrypt a value using AES-256.
*
* *Caveat* You cannot properly encrypt/decrypt data with trailing null bytes.
* Any trailing null bytes will be removed on decryption due to how PHP pads messages
* with nulls prior to encryption.
*
* @param string $plain The value to encrypt.
* @param string $key The 256 bit/32 byte key to use as a cipher key.
* @param string|null $hmacSalt The salt to use for the HMAC process. Leave null to use Security.salt.
* @return string Encrypted data.
* @throws \InvalidArgumentException On invalid data or key.
*/
public static function encrypt($plain, $key, $hmacSalt = null)
{
$method = 'AES-256-CBC';
$ivSize = openssl_cipher_iv_length($method);
$iv = openssl_random_pseudo_bytes($ivSize);
return $iv . openssl_encrypt($plain, $method, $key, OPENSSL_RAW_DATA, $iv);
}