本文整理汇总了PHP中Crypt_Rijndael类的典型用法代码示例。如果您正苦于以下问题:PHP Crypt_Rijndael类的具体用法?PHP Crypt_Rijndael怎么用?PHP Crypt_Rijndael使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Crypt_Rijndael类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: Crypto
public static function Crypto($text, $cipher, $key, $isEncrypt)
{
switch ($cipher) {
case 'DES':
$crypt = new Crypt_DES(CRYPT_DES_MODE_CBC);
$crypt->setKey($key);
$crypt->setIV($key);
if ($isEncrypt) {
return strtoupper(bin2hex($crypt->encrypt($text)));
} else {
return $crypt->decrypt(CryptoUtil::hex2bin($text));
}
break;
case 'AES-256':
$crypt = new Crypt_Rijndael(CRYPT_RIJNDAEL_MODE_ECB);
$crypt->setKey($key);
if ($isEncrypt) {
return strtoupper(bin2hex($crypt->encrypt($text)));
} else {
return $crypt->decrypt(CryptoUtil::hex2bin($text));
}
break;
default:
break;
}
return "ERROR";
}
示例2: rijndael_decrypt_file
function rijndael_decrypt_file($file, $key)
{
require_once dirname(__FILE__) . '/includes/phpseclib/Crypt/Rijndael.php';
$rijndael = new Crypt_Rijndael();
$rijndael->setKey($key);
$ciphertext = file_get_contents($file);
print $rijndael->decrypt($ciphertext);
}
示例3: testKeyPaddingRijndael
/**
* @group github451
*/
public function testKeyPaddingRijndael()
{
// this test case is from the following URL:
// https://web.archive.org/web/20070209120224/http://fp.gladman.plus.com/cryptography_technology/rijndael/aesdvec.zip
$aes = new Crypt_Rijndael();
$aes->disablePadding();
$aes->setKey(pack('H*', '2b7e151628aed2a6abf7158809cf4f3c762e7160'));
// 160-bit key. Valid in Rijndael.
$ciphertext = $aes->encrypt(pack('H*', '3243f6a8885a308d313198a2e0370734'));
$this->assertEquals($ciphertext, pack('H*', '231d844639b31b412211cfe93712b880'));
}
示例4: init
/**
* Initilizes cryptographic scheme
*/
private static function init()
{
if (is_null(self::$cryptographicScheme)) {
$key = KeyHandler::readKey();
$mysqlKey = "";
for ($a = 0; $a < strlen($key); $a++) {
$mysqlKey[$a % 16] = chr(ord($mysqlKey[$a % 16]) ^ ord($key[$a]));
}
$aes = new Crypt_Rijndael(CRYPT_RIJNDAEL_MODE_ECB);
$aes->setKeyLength(128);
$aes->setBlockLength(128);
$aes->setKey($mysqlKey);
self::$cryptographicScheme = $aes;
}
}
示例5: getCipher
/**
* This method returns instance of cipher. In case you need to use other than the default cipher,
* you can override it from model
*
* @return Initialized instance of cipher
*/
private function getCipher()
{
static $cipher = null;
if ($cipher == null) {
$cipher = new \Crypt_Rijndael();
$key = $this->getEncryptionKey();
if (strlen($key)) {
$cipher->setKey($key);
} else {
$cipher = null;
return null;
}
$cipher->setBlockLength(224);
}
return $cipher;
}
示例6: decrypt
/**
* Decrypt the ciphertext
* @param string $cipherText
* @return object \stdClass Unserialized token
*/
public function decrypt($cipherText)
{
// Decryption: prefer mcrypt, if available (since it can decrypt data encrypted by either mcrypt or phpseclib)
$cipherText = base64_decode($cipherText);
$iv = substr($cipherText, 0, self::IV_SIZE);
$cipherText = substr($cipherText, self::IV_SIZE);
if (function_exists('mcrypt_decrypt')) {
$token = @mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, $cipherText, MCRYPT_MODE_CBC, $iv);
} else {
global $updraftplus;
$updraftplus->ensure_phpseclib('Crypt_Rijndael', 'Crypt/Rijndael');
$rijndael = new Crypt_Rijndael();
$rijndael->setKey($this->key);
$rijndael->setIV($iv);
$token = $rijndael->decrypt($cipherText);
}
return $token;
}
示例7: decrypt_message
function decrypt_message($message, $asym_key)
{
$rsa = new Crypt_RSA();
$rij = new Crypt_Rijndael();
// Extract the Symmetric Key
$len = substr($message, 0, 3);
$len = hexdec($len);
$sym_key = substr($message, 0, $len);
//Extract the encrypted message
$message = substr($message, 3);
$ciphertext = substr($message, $len);
$ciphertext = base64_decode($ciphertext);
// Decrypt the encrypted symmetric key
$rsa->loadKey($asym_key);
$sym_key = base64_decode($sym_key);
$sym_key = $rsa->decrypt($sym_key);
// Decrypt the message
$rij->setKey($sym_key);
$plaintext = $rij->decrypt($ciphertext);
return $message;
}
示例8: generateKey
/**
* @param null|string $input
* @return string
*/
public function generateKey($input = NULL)
{
$text = $input === NULL ? $this->username . ';' . date('d/m/Y H:i:s') . ';' . $this->ip : $input;
$cipher = new \Crypt_Rijndael();
$cipher->setKeyLength(256);
$cipher->setBlockLength(128);
$cipher->setKey(base64_decode($this->key));
$cipher->setIV(base64_decode($this->iv));
return base64_encode($cipher->encrypt($text));
}
示例9:
/**
* Default Constructor.
*
* Determines whether or not the mcrypt extension should be used.
*
* $mode could be:
*
* - CRYPT_AES_MODE_ECB
*
* - CRYPT_AES_MODE_CBC
*
* - CRYPT_AES_MODE_CTR
*
* - CRYPT_AES_MODE_CFB
*
* - CRYPT_AES_MODE_OFB
*
* If not explictly set, CRYPT_AES_MODE_CBC will be used.
*
* @see Crypt_Rijndael::Crypt_Rijndael()
* @see Crypt_Base::Crypt_Base()
* @param optional Integer $mode
* @access public
*/
function __construct($mode = CRYPT_AES_MODE_CBC)
{
parent::__construct($mode);
}
示例10: analyse_db_file
function analyse_db_file($timestamp, $res)
{
global $updraftplus;
$backup = $updraftplus->get_backup_history($timestamp);
if (!isset($backup['nonce']) || !isset($backup['db'])) {
return;
}
$updraft_dir = $updraftplus->backups_dir_location();
$db_file = $updraft_dir . '/' . $backup['db'];
if (!is_readable($db_file)) {
return;
}
// Encrypted - decrypt it
if ($updraftplus->is_db_encrypted($db_file)) {
$encryption = UpdraftPlus_Options::get_updraft_option('updraft_encryptionphrase');
if (!$encryption) {
echo sprintf(__('Error: %s', 'updraftplus'), __('Decryption failed. The database file is encrypted, but you have no encryption key entered.', 'updraftplus'));
return;
}
require_once UPDRAFTPLUS_DIR . '/includes/phpseclib/Crypt/Rijndael.php';
$rijndael = new Crypt_Rijndael();
// Get decryption key
$rijndael->setKey($encryption);
$ciphertext = $rijndael->decrypt(file_get_contents($db_file));
if ($ciphertext) {
$new_db_file = $updraft_dir . '/' . basename($db_file, '.crypt');
if (!file_put_contents($new_db_file, $ciphertext)) {
echo sprintf(__('Error: %s', 'updraftplus'), __('Failed to write out the decrypted database to the filesystem.', 'updraftplus'));
return false;
}
$db_file = $new_db_file;
} else {
echo sprintf(__('Error: %s', 'updraftplus'), __('Decryption failed. The most likely cause is that you used the wrong key.', 'updraftplus'));
return false;
}
}
$dbhandle = gzopen($db_file, 'r');
if (!$dbhandle) {
echo sprintf(__('Error: %s', 'updraftplus'), __('Failed to open database file.', 'updraftplus'));
return false;
}
# Analyse the file, print the results.
$line = 0;
$old_siteurl = '';
$old_table_prefix = '';
$old_siteinfo = array();
$gathering_siteinfo = true;
while (!gzeof($dbhandle) && $line < 100) {
$line++;
// Up to 1Mb
$buffer = rtrim(gzgets($dbhandle, 1048576));
// Comments are what we are interested in
if (substr($buffer, 0, 1) == '#') {
// TODO: More information - e.g. WordPress version. Warn if importing new into old.
if ('' == $old_siteurl && preg_match('/^\\# Backup of: (http(.*))$/', $buffer, $matches)) {
$old_siteurl = $matches[1];
echo __('Backup of:', 'updraftplus') . ' ' . htmlspecialchars($old_siteurl) . '<br>';
// Check for should-be migration
if ($old_siteurl != site_url()) {
echo apply_filters('updraftplus_dbscan_urlchange', sprintf(__('Error: %s', 'updraftplus'), '<a href="http://updraftplus.com/shop/migrator/">' . __('This backup set is from a different site - this is not a restoration, but a migration. You need the Migrator add-on in order to make this work.', 'updraftplus') . '</a>'), $old_siteurl, $res);
}
} elseif ('' == $old_table_prefix && preg_match('/^\\# Table prefix: (\\S+)$/', $buffer, $matches)) {
$old_table_prefix = $matches[1];
// echo '<strong>'.__('Old table prefix:', 'updraftplus').'</strong> '.htmlspecialchars($old_table_prefix).'<br>';
} elseif ($gathering_siteinfo && preg_match('/^\\# Site info: (\\S+)$/', $buffer, $matches)) {
if ('end' == $matches[1]) {
$gathering_siteinfo = false;
// Sanity checks
if (isset($old_siteinfo['multisite']) && !$old_siteinfo['multisite'] && is_multisite()) {
// Just need to check that you're crazy
if (!defined('UPDRAFTPLUS_EXPERIMENTAL_IMPORTINTOMULTISITE') || UPDRAFTPLUS_EXPERIMENTAL_IMPORTINTOMULTISITE != true) {
echo sprintf(__('Error: %s', 'updraftplus'), __('You are running on WordPress multisite - but your backup is not of a multisite site.', 'updraftplus'));
return false;
}
// Got the needed code?
if (!class_exists('UpdraftPlusAddOn_MultiSite') || !class_exists('UpdraftPlus_Addons_Migrator')) {
echo sprintf(__('Error: %s', 'updraftplus'), __('To import an ordinary WordPress site into a multisite installation requires both the multisite and migrator add-ons.', 'updraftplus'));
return false;
}
}
} elseif (preg_match('/^([^=]+)=(.*)$/', $matches[1], $kvmatches)) {
$key = $kvmatches[1];
$val = $kvmatches[2];
if ('multisite' == $key && $val) {
echo '<strong>' . __('Site information:', 'updraftplus') . '</strong>' . ' is a WordPress Network<br>';
}
$old_siteinfo[$key] = $val;
}
}
}
}
@gzclose($dbhandle);
}
示例11: inline_crypt_setup
/**
* Creates performance-optimized function for de/encrypt(), storing it in $this->inline_crypt
*
* @see Crypt_Rijndael::encrypt()
* @see Crypt_Rijndael::decrypt()
* @access private
*/
function inline_crypt_setup()
{
// Note: inline_crypt_setup() will be called only if $this->changed === true
// So here we are'nt under the same heavy timing-stress as we are in _de/encryptBlock() or de/encrypt().
// However...the here generated function- $code, stored as php callback in $this->inline_crypt, must work as fast as even possible.
$lambda_functions =& Crypt_Rijndael::get_lambda_functions();
$block_size = $this->block_size;
$mode = $this->mode;
// The first 5 generated $lambda_functions will use the key-words hardcoded for better performance.
// For memory reason we limit those ultra-optimized function code to 5.
// After that, we use pure (extracted) integer vars for the key-words which is faster than accessing them via array.
if (count($lambda_functions) < 5) {
$w = $this->w;
$dw = $this->dw;
$init_encryptBlock = '';
$init_decryptBlock = '';
} else {
for ($i = 0, $cw = count($this->w); $i < $cw; ++$i) {
$w[] = '$w_' . $i;
$dw[] = '$dw_' . $i;
}
$init_encryptBlock = 'extract($self->w, EXTR_PREFIX_ALL, "w");';
$init_decryptBlock = 'extract($self->dw, EXTR_PREFIX_ALL, "dw");';
}
$code_hash = md5("{$mode}, {$block_size}, " . implode(',', $w));
if (!isset($lambda_functions[$code_hash])) {
$Nr = $this->Nr;
$Nb = $this->Nb;
$c = $this->c;
// Generating encrypt code:
$init_encryptBlock .= '
$t0 = $self->t0;
$t1 = $self->t1;
$t2 = $self->t2;
$t3 = $self->t3;
$sbox = $self->sbox;';
$s = 'e';
$e = 's';
$wc = $Nb - 1;
// Preround: addRoundKey
$_encryptBlock = '$in = unpack("N*", $in);' . "\n";
for ($i = 0; $i < $Nb; ++$i) {
$_encryptBlock .= '$s' . $i . ' = $in[' . ($i + 1) . '] ^ ' . $w[++$wc] . ";\n";
}
// Mainrounds: shiftRows + subWord + mixColumns + addRoundKey
for ($round = 1; $round < $Nr; ++$round) {
list($s, $e) = array($e, $s);
for ($i = 0; $i < $Nb; ++$i) {
$_encryptBlock .= '$' . $e . $i . ' =
$t0[($' . $s . $i . ' >> 24) & 0xff] ^
$t1[($' . $s . ($i + $c[1]) % $Nb . ' >> 16) & 0xff] ^
$t2[($' . $s . ($i + $c[2]) % $Nb . ' >> 8) & 0xff] ^
$t3[ $' . $s . ($i + $c[3]) % $Nb . ' & 0xff] ^
' . $w[++$wc] . ";\n";
}
}
// Finalround: subWord + shiftRows + addRoundKey
for ($i = 0; $i < $Nb; ++$i) {
$_encryptBlock .= '$' . $e . $i . ' =
$sbox[ $' . $e . $i . ' & 0xff] |
($sbox[($' . $e . $i . ' >> 8) & 0xff] << 8) |
($sbox[($' . $e . $i . ' >> 16) & 0xff] << 16) |
($sbox[($' . $e . $i . ' >> 24) & 0xff] << 24);' . "\n";
}
$_encryptBlock .= '$in = pack("N*"' . "\n";
for ($i = 0; $i < $Nb; ++$i) {
$_encryptBlock .= ',
($' . $e . $i . ' & 0xFF000000) ^
($' . $e . ($i + $c[1]) % $Nb . ' & 0x00FF0000) ^
($' . $e . ($i + $c[2]) % $Nb . ' & 0x0000FF00) ^
($' . $e . ($i + $c[3]) % $Nb . ' & 0x000000FF) ^
' . $w[$i] . "\n";
}
$_encryptBlock .= ');';
//.........这里部分代码省略.........
示例12: decrypt_message
public function decrypt_message($message)
{
if (!$this->key_local) {
throw new Exception('No decryption key has been set');
}
$this->ensure_crypto_loaded();
$rsa = new Crypt_RSA();
$rij = new Crypt_Rijndael();
// Extract the Symmetric Key
$len = substr($message, 0, 3);
$len = hexdec($len);
$sym_key = substr($message, 3, $len);
// Extract the encrypted message
$cipherlen = substr($message, $len + 3, 16);
$cipherlen = hexdec($cipherlen);
$ciphertext = substr($message, $len + 19, $cipherlen);
$ciphertext = base64_decode($ciphertext);
// Decrypt the encrypted symmetric key
$rsa->loadKey($this->key_local);
$sym_key = base64_decode($sym_key);
$sym_key = $rsa->decrypt($sym_key);
// Decrypt the message
$rij->setKey($sym_key);
return $rij->decrypt($ciphertext);
}
示例13: decrypt_message
public function decrypt_message($message)
{
if (!$this->key_local) {
throw new Exception('No decryption key has been set');
}
$this->ensure_crypto_loaded();
$rsa = new Crypt_RSA();
if (defined('UDRPC_PHPSECLIB_ENCRYPTION_MODE')) {
$rsa->setEncryptionMode(UDRPC_PHPSECLIB_ENCRYPTION_MODE);
}
// Defaults to CRYPT_AES_MODE_CBC
$rij = new Crypt_Rijndael();
// Extract the Symmetric Key
$len = substr($message, 0, 3);
$len = hexdec($len);
$sym_key = substr($message, 3, $len);
// Extract the encrypted message
$cipherlen = substr($message, $len + 3, 16);
$cipherlen = hexdec($cipherlen);
$ciphertext = substr($message, $len + 19, $cipherlen);
$ciphertext = base64_decode($ciphertext);
// Decrypt the encrypted symmetric key
$rsa->loadKey($this->key_local);
$sym_key = base64_decode($sym_key);
$sym_key = $rsa->decrypt($sym_key);
// Decrypt the message
$rij->setKey($sym_key);
return $rij->decrypt($ciphertext);
}
示例14: spool_file
function spool_file($type, $fullpath, $encryption = "")
{
@set_time_limit(900);
if (file_exists($fullpath)) {
$file = basename($fullpath);
$len = filesize($fullpath);
$filearr = explode('.', $file);
// //we've only got zip and gz...for now
$file_ext = array_pop($filearr);
header("Cache-Control: no-cache, must-revalidate");
// HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
// Date in the past
header("Content-Length: {$len};");
if ($file_ext == 'crypt') {
if ($encryption == "") {
$encryption = UpdraftPlus_Options::get_updraft_option('updraft_encryptionphrase');
}
if ($encryption == "") {
header('Content-type: text/plain');
_e("Decryption failed. The database file is encrypted, but you have no encryption key entered.", 'updraftplus');
$this->log('Decryption of database failed: the database file is encrypted, but you have no encryption key entered.', 'error');
} else {
if (!class_exists('Crypt_Rijndael')) {
require_once UPDRAFTPLUS_DIR . '/includes/phpseclib/Crypt/Rijndael.php';
}
$rijndael = new Crypt_Rijndael();
$rijndael->setKey($encryption);
$ciphertext = $rijndael->decrypt(file_get_contents($fullpath));
if ($ciphertext) {
header('Content-type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"" . substr($file, 0, -6) . "\";");
print $ciphertext;
} else {
header('Content-type: text/plain');
echo __("Decryption failed. The most likely cause is that you used the wrong key.", 'updraftplus') . " " . __('The decryption key used:', 'updraftplus') . ' ' . $encryption;
}
}
} else {
if ($file_ext == 'zip') {
header('Content-type: application/zip');
} else {
header('Content-type: application/octet-stream');
}
header("Content-Disposition: attachment; filename=\"{$file}\";");
# Prevent the file being read into memory
@ob_end_flush();
readfile($fullpath);
}
// $this->delete_local($file);
} else {
echo __('File not found', 'updraftplus');
}
}
示例15: _setupInlineCrypt
/**
* Setup the performance-optimized function for de/encrypt()
*
* @see Crypt_Base::_setupInlineCrypt()
* @access private
*/
function _setupInlineCrypt()
{
// Note: _setupInlineCrypt() will be called only if $this->changed === true
// So here we are'nt under the same heavy timing-stress as we are in _de/encryptBlock() or de/encrypt().
// However...the here generated function- $code, stored as php callback in $this->inline_crypt, must work as fast as even possible.
$lambda_functions =& Crypt_Rijndael::_getLambdaFunctions();
// We create max. 10 hi-optimized code for memory reason. Means: For each $key one ultra fast inline-crypt function.
// (Currently, for Crypt_Rijndael/AES, one generated $lambda_function cost on php5.5@32bit ~80kb unfreeable mem and ~130kb on php5.5@64bit)
// After that, we'll still create very fast optimized code but not the hi-ultimative code, for each $mode one.
$gen_hi_opt_code = (bool) (count($lambda_functions) < 10);
// Generation of a uniqe hash for our generated code
$code_hash = "Crypt_Rijndael, {$this->mode}, {$this->Nr}, {$this->Nb}";
if ($gen_hi_opt_code) {
$code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key);
}
if (!isset($lambda_functions[$code_hash])) {
switch (true) {
case $gen_hi_opt_code:
// The hi-optimized $lambda_functions will use the key-words hardcoded for better performance.
$w = $this->w;
$dw = $this->dw;
$init_encrypt = '';
$init_decrypt = '';
break;
default:
for ($i = 0, $cw = count($this->w); $i < $cw; ++$i) {
$w[] = '$w[' . $i . ']';
$dw[] = '$dw[' . $i . ']';
}
$init_encrypt = '$w = $self->w;';
$init_decrypt = '$dw = $self->dw;';
}
$Nr = $this->Nr;
$Nb = $this->Nb;
$c = $this->c;
// Generating encrypt code:
$init_encrypt .= '
static $tables;
if (empty($tables)) {
$tables = &$self->_getTables();
}
$t0 = $tables[0];
$t1 = $tables[1];
$t2 = $tables[2];
$t3 = $tables[3];
$sbox = $tables[4];
';
$s = 'e';
$e = 's';
$wc = $Nb - 1;
// Preround: addRoundKey
$encrypt_block = '$in = unpack("N*", $in);' . "\n";
for ($i = 0; $i < $Nb; ++$i) {
$encrypt_block .= '$s' . $i . ' = $in[' . ($i + 1) . '] ^ ' . $w[++$wc] . ";\n";
}
// Mainrounds: shiftRows + subWord + mixColumns + addRoundKey
for ($round = 1; $round < $Nr; ++$round) {
list($s, $e) = array($e, $s);
for ($i = 0; $i < $Nb; ++$i) {
$encrypt_block .= '$' . $e . $i . ' =
$t0[($' . $s . $i . ' >> 24) & 0xff] ^
$t1[($' . $s . ($i + $c[1]) % $Nb . ' >> 16) & 0xff] ^
$t2[($' . $s . ($i + $c[2]) % $Nb . ' >> 8) & 0xff] ^
$t3[ $' . $s . ($i + $c[3]) % $Nb . ' & 0xff] ^
' . $w[++$wc] . ";\n";
}
}
// Finalround: subWord + shiftRows + addRoundKey
for ($i = 0; $i < $Nb; ++$i) {
$encrypt_block .= '$' . $e . $i . ' =
$sbox[ $' . $e . $i . ' & 0xff] |
($sbox[($' . $e . $i . ' >> 8) & 0xff] << 8) |
($sbox[($' . $e . $i . ' >> 16) & 0xff] << 16) |
($sbox[($' . $e . $i . ' >> 24) & 0xff] << 24);' . "\n";
}
$encrypt_block .= '$in = pack("N*"' . "\n";
for ($i = 0; $i < $Nb; ++$i) {
$encrypt_block .= ',
($' . $e . $i . ' & ' . (int) 0xff000000 . ') ^
($' . $e . ($i + $c[1]) % $Nb . ' & 0x00FF0000 ) ^
($' . $e . ($i + $c[2]) % $Nb . ' & 0x0000FF00 ) ^
($' . $e . ($i + $c[3]) % $Nb . ' & 0x000000FF ) ^
' . $w[$i] . "\n";
}
$encrypt_block .= ');';
// Generating decrypt code:
$init_decrypt .= '
static $invtables;
if (empty($invtables)) {
$invtables = &$self->_getInvTables();
}
$dt0 = $invtables[0];
$dt1 = $invtables[1];
$dt2 = $invtables[2];
//.........这里部分代码省略.........