本文整理汇总了PHP中mcrypt_generic函数的典型用法代码示例。如果您正苦于以下问题:PHP mcrypt_generic函数的具体用法?PHP mcrypt_generic怎么用?PHP mcrypt_generic使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了mcrypt_generic函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _encryptRijndael256
/**
* Encrypt and return a string using the Rijndael 256 cypher
* @param string $str String to encrypt
* @param string $key Key to encrypt the string
* @param string $iv Initialisation vector
* @return string
*/
public static function _encryptRijndael256($str, $key, $iv = FALSE)
{
// If the string is empty
if (empty($str)) {
return '';
}
// Open the cypher
$cypher = mcrypt_module_open('rijndael-256', '', 'ofb', '');
// Create the IV if there is none
if ($iv === FALSE) {
$iv = self::_genRijndael256IV($cypher);
}
// Set Key
$key = substr($key, 0, mcrypt_enc_get_key_size($cypher));
// Initialise encryption
mcrypt_generic_init($cypher, $key, $iv);
// Encrypt String
$encrypted = mcrypt_generic($cypher, $strString);
// Terminate encryption hander
mcrypt_generic_deinit($cypher);
// Close Module
mcrypt_module_close($cypher);
// Return encrypted string
return $encrypted;
}
示例2: encrypt
/**
*/
public function encrypt($text)
{
mcrypt_generic_init($this->_mcrypt, $this->key, empty($this->iv) ? str_repeat('0', Horde_Crypt_Blowfish::IV_LENGTH) : $this->iv);
$out = mcrypt_generic($this->_mcrypt, $this->_pad($text));
mcrypt_generic_deinit($this->_mcrypt);
return $out;
}
示例3: tripleDESEncrypt1
function tripleDESEncrypt1($src, $key)
{
if ($src == NULL) {
return NULL;
}
$iv = $this->my_mcrypt_create_iv();
/* Open module, and create IV */
$td = mcrypt_module_open(MCRYPT_3DES, '', 'cbc', '');
if ($key == NULL) {
$key = "";
}
$key = substr($key, 0, mcrypt_enc_get_key_size($td));
/* Initialize encryption handle */
if (mcrypt_generic_init($td, $key, $iv) != -1) {
/* Encrypt data */
$encryptedData = mcrypt_generic($td, $src);
$iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CBC);
$encryptedData = chr($iv_size) . $iv . $encryptedData;
mcrypt_generic_deinit($td);
//echo "src is:" . $src. "<br>\n";
//echo "iv is :" .bin2hex($iv)."<br>\n";
//echo "key is :" .bin2hex($key)."<br>\n";
} else {
$encryptedData = null;
}
mcrypt_module_close($td);
return $encryptedData;
}
示例4: encrypt
public static function encrypt($keyString, $value)
{
$origKey = $keyString;
if (strlen($keyString) > 32) {
$keyString = substr($keyString, 0, 32);
}
if (strlen($keyString) < 32) {
$keyString = str_pad($keyString, 32, 'X');
}
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
$iv = TPSecurityUtils::genRandomString(16);
if (mcrypt_generic_init($cipher, $keyString, $iv) != -1) {
$blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$padding = $blockSize - strlen($value) % $blockSize;
$value .= str_repeat(chr($padding), $padding);
// PHP pads with NULL bytes if $value is not a multiple of the block size..
$cipherText = mcrypt_generic($cipher, $value);
mcrypt_generic_deinit($cipher);
mcrypt_module_close($cipher);
$safe = TPSecurityUtils::urlensafe($cipherText);
return $safe . TPSecurityUtils::DELIM . TPSecurityUtils::hashHmacSha256($origKey, $safe);
}
$safe = TPSecurityUtils::urlensafe($value);
return $safe . TPSecurityUtils::DELIM . TPSecurityUtils::hashHmacSha256($origKey, $safe);
}
示例5: encrypt
/**
* Encrypt
*
* @return mixed string|null
* @param object $value
*/
public function encrypt($value)
{
if (empty($value)) {
return $value;
}
return base64_encode(mcrypt_generic($this->_handler, (string) $value));
}
示例6: encrypt
/**
* Ritorna la string codificata.
* Se il costruttore ha fallito nell'inizializzazione dell'algoritmo di cifratura, la stringa viene
* restituita in chiaro; altrimenti la codifica consiste nella cifratura con l'algoritmo scelto.
*
* @param string $str Se non viene passata nessuna stringa ritorna una stringa vuota.
* @return string
*/
public function encrypt($str)
{
try {
$str = filter_var($str, FILTER_SANITIZE_STRING);
if (empty($str)) {
throw new InvalidArgumentException('Empty or not valid string as agrument', E_USER_NOTICE);
}
if (!isset($this->_cipher)) {
throw new UnexpectedValueException('Cypher error', E_USER_WARNING);
}
if (false === ($iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->_cipher), MCRYPT_RAND))) {
throw new UnexpectedValueException('Cannot initialize vector IV', E_USER_WARNING);
}
$s = mcrypt_generic_init($this->_cipher, $this->_key, $iv);
if (0 > $s || false === $s) {
throw new exceptions('Encrypt failure', E_USER_WARNING);
}
$enc = mcrypt_generic($this->_cipher, $str);
mcrypt_generic_deinit($this->_cipher);
return $enc;
} catch (Exception $e) {
debug::exception($e);
return $str;
}
}
示例7: enkripsi
public function enkripsi($algoritma, $mode, $secretkey, $fileplain)
{
/* Membuka Modul untuk memilih Algoritma & Mode Operasi */
$td = mcrypt_module_open($algoritma, '', $mode, '');
/* Inisialisasi IV dan Menentukan panjang kunci yang digunakan */
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$ks = mcrypt_enc_get_key_size($td);
/* Menghasilkan Kunci */
$key = $secretkey;
//echo "kuncinya : ". $key. "<br>";
/* Inisialisasi */
mcrypt_generic_init($td, $key, $iv);
/* Enkripsi Data, dimana hasil enkripsi harus di encode dengan base64.\
Hal ini dikarenakan web browser tidak dapat membaca karakter-karakter\
ASCII dalam bentuk simbol-simbol */
$buffer = $fileplain;
$encrypted = mcrypt_generic($td, $buffer);
$encrypted1 = base64_encode($iv) . ";" . base64_encode($encrypted);
$encrypted2 = base64_encode($encrypted1);
$filecipher = $encrypted2;
/* Menghentikan proses enkripsi dan menutup modul */
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $filecipher;
}
示例8: aes128_encode
function aes128_encode($data, $mode)
{
switch ($mode) {
case "ECB":
case "CBC":
if ($mode === "ECB") {
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
} else {
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
}
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($cipher), MCRYPT_RAND);
$key = substr('naveen', 0, mcrypt_enc_get_key_size($cipher));
if (mcrypt_generic_init($cipher, $key, $iv) != 1) {
$cipherData = mcrypt_generic($cipher, $data);
mcrypt_generic_deinit($cipher);
mcrypt_module_close($cipher);
if ($mode === "ECB") {
$sanitizedCipherData = trim(base64_encode($cipherData));
} else {
$sanitizedCipherData = trim(base64_encode($iv) . "_" . base64_encode($cipherData));
}
return $sanitizedCipherData;
} else {
return false;
}
break;
default:
return false;
break;
}
}
示例9: encrypt
static function encrypt($input, $base64 = true)
{
if (!$input || !strlen($input) > 0) {
return null;
}
if (!($td = mcrypt_module_open('rijndael-256', '', 'ctr', ''))) {
return null;
}
if (!($iv = mcrypt_create_iv(32, MCRYPT_RAND))) {
return null;
}
$content = serialize($input);
if (mcrypt_generic_init($td, MSettings::$c_key, $iv) !== 0) {
return null;
}
$content = mcrypt_generic($td, $content);
$content = $iv . $content;
$mac = self::pbkdf2($content, MSettings::$c_key, 1000, 32);
$content .= $mac;
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
if ($base64) {
$content = base64_encode($content);
}
return $content;
}
示例10: encrypt
/**
* Encrypt data
*
* This method will encrypt data using a given key, vector, and cipher.
* By default, this will encrypt data using the RIJNDAEL/AES 256 bit cipher. You
* may override the default cipher and cipher mode by passing your own desired
* cipher and cipher mode as the final key-value array argument.
*
* @param string $data The unencrypted data
* @param string $key The encryption key
* @param string $iv The encryption initialization vector
* @param array $settings Optional key-value array with custom algorithm and mode
* @return string
*/
public static function encrypt($data, $key, $iv, $settings = array())
{
if ($data === '' || !extension_loaded('mcrypt')) {
return $data;
}
//Merge settings with defaults
$defaults = array('algorithm' => MCRYPT_RIJNDAEL_256, 'mode' => MCRYPT_MODE_CBC);
$settings = array_merge($defaults, $settings);
//Get module
$module = mcrypt_module_open($settings['algorithm'], '', $settings['mode'], '');
//Validate IV
$ivSize = mcrypt_enc_get_iv_size($module);
if (strlen($iv) > $ivSize) {
$iv = substr($iv, 0, $ivSize);
}
//Validate key
$keySize = mcrypt_enc_get_key_size($module);
if (strlen($key) > $keySize) {
$key = substr($key, 0, $keySize);
}
//Encrypt value
mcrypt_generic_init($module, $key, $iv);
$res = @mcrypt_generic($module, $data);
mcrypt_generic_deinit($module);
return $res;
}
示例11: set
/**
* Store an encrypted cookie
*
* @param string $cookieName
* @param mixed $cookieValue
* @param int $expiry default stores just for the browser session
*/
public static function set($cookieName, $cookieValue, $expiry = 0)
{
if (isset($_COOKIE['synsec'])) {
$synsec = $_COOKIE['synsec'];
} else {
$synsec = Tools::randomString('12');
}
if ((!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') && (!isset($_SERVER['HTTP_X_FORWARDED_PROTO']) || $_SERVER['HTTP_X_FORWARDED_PROTO'] != 'https')) {
$ssl = false;
} else {
$ssl = true;
}
setcookie('synsec', $synsec, time() + 60 * 60 * 24 * 30, '/', $_SERVER['HTTP_HOST'], $ssl, true);
$synsec .= 'synErgy' . self::$token;
/* Open the cipher */
$td = mcrypt_module_open('rijndael-256', '', 'ofb', '');
/* Create the IV and determine the keysize length, use MCRYPT_RAND
* on Windows instead */
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
$ks = mcrypt_enc_get_key_size($td);
/* Create key */
$key = substr(md5($synsec), 0, $ks);
/* Intialize encryption */
mcrypt_generic_init($td, $key, $iv);
/* Encrypt data */
$encrypted = mcrypt_generic($td, serialize($cookieValue));
# Store our secure cookie
setcookie($cookieName, trim(base64_encode($iv . '|' . $encrypted)), $expiry, '/', $_SERVER['HTTP_HOST'], $ssl, true);
/* Terminate encryption handler */
mcrypt_generic_deinit($td);
}
示例12: encrypt
private function encrypt($key, $data, $cipher = MCRYPT_3DES, $hash = 'sha256')
{
$td = mcrypt_module_open($cipher, '', MCRYPT_MODE_ECB, '');
if ($td === false) {
return false;
}
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_URANDOM);
if ($iv === false) {
return false;
}
$ks = mcrypt_enc_get_key_size($td);
switch ($hash) {
case 'md5':
$key = substr($this->md5($key), 0, $ks);
case 'sha256':
$key = substr($this->sha256($key), 0, $ks);
case 'sha512':
$key = substr($this->sha512($key), 0, $ks);
case 'none':
default:
$key = substr($key, 0, $ks);
}
mcrypt_generic_init($td, $key, $iv);
$ret = mcrypt_generic($td, $data);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $iv . $ret;
}
示例13: encrypt
public function encrypt($string)
{
mcrypt_generic_init($this->cipher, $this->key, $this->iv);
$cipherText = mcrypt_generic($this->cipher, $string);
mcrypt_generic_deinit($this->cipher);
return $cipherText;
}
示例14: mcrypt
/**
* @param string $string
* @return string
*/
protected function mcrypt($string)
{
$this->encryptInit();
$encrypted = mcrypt_generic($this->getEncryptionDescriptor(), $string);
$this->encryptDeinit();
return $encrypted;
}
示例15: encrypt
/**
* 对明文进行加密
* @param string $text 需要加密的明文
* @return string 加密后的密文
*/
public function encrypt($text, $appid)
{
try {
//获得16位随机字符串,填充到明文之前
$random = $this->getRandomStr();
$text = $random . pack("N", strlen($text)) . $text . $appid;
// 网络字节序
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$iv = substr($this->key, 0, 16);
//使用自定义的填充方式对明文进行补位填充
$pkc_encoder = new Pkcs7Encoder();
$text = $pkc_encoder->encode($text);
mcrypt_generic_init($module, $this->key, $iv);
//加密
$encrypted = mcrypt_generic($module, $text);
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
//使用BASE64对加密后的字符串进行编码
return base64_encode($encrypted);
} catch (Exception $e) {
@error_log('Encrypt AES Error: ' . $e->getMessage(), 0);
return FALSE;
}
}