/** * ApiClient constructor * @param String $api_username Your API Username * @param String $api_password Your API Password * @param String $api_key Your API Key * @param Integer $version The API version * @param Integer $timeout Set how long to wait for the API to respond * @return null */ public function __construct($api_username, $api_password, $api_key, $version = 1, $timeout = 10) { // Set API Credentials $this->api_username = $api_username; $this->api_password = $api_password; $this->api_key = $api_key; $this->public_key = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC8NVUZUtr2IHiFoY8s/qFGmZOIewAvg' . 'S4FMXWZ81Qc8lkAlZr9e171xn4PgKr+S7YsfCt+1XKyo5XmrJyaNUe/aRptB93NFn6RoFzExgfpkooxcHpWcP' . 'y+Hb5e0rwPDBA6zfyrYRj8uK/1HleFEr4v8u/HbnJmiFoNJ2hfZXn6Qw== phpseclib-generated-key'; // Set API Version $this->version = 1; if (is_numeric($version) && $version > 0 && $version < 2) { $this->version = $version; } // Set API Timeout $this->timeout = 10; if (is_numeric($version)) { $this->timeout = $timeout; } // Define a code alphabet for generating strings $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; // Create Triple DES Key $triple_des_size = mcrypt_get_key_size('tripledes', 'cbc'); for ($i = 0; $i < $triple_des_size; $i++) { $this->triple_des_key .= $codeAlphabet[$this->cryptoRandSecure(0, strlen($codeAlphabet))]; } }
/** * Loads encryption configuration and validates the data. * * @param array|string custom configuration or config group name * @throws Kohana_Exception */ public function __construct($config = FALSE) { if (!defined('MCRYPT_ENCRYPT')) { throw new Kohana_Exception('encrypt.requires_mcrypt'); } if (is_string($config)) { $name = $config; // Test the config group name if (($config = Kohana::config('encryption.' . $config)) === NULL) { throw new Kohana_Exception('encrypt.undefined_group', $name); } } if (is_array($config)) { // Append the default configuration options $config += Kohana::config('encryption.default'); } else { // Load the default group $config = Kohana::config('encryption.default'); } if (empty($config['key'])) { throw new Kohana_Exception('encrypt.no_encryption_key'); } // Find the max length of the key, based on cipher and mode $size = mcrypt_get_key_size($config['cipher'], $config['mode']); if (strlen($config['key']) > $size) { // Shorten the key to the maximum size $config['key'] = substr($config['key'], 0, $size); } // Find the initialization vector size $config['iv_size'] = mcrypt_get_iv_size($config['cipher'], $config['mode']); // Cache the config in the object $this->config = $config; Kohana::log('debug', 'Encrypt Library initialized'); }
/** * @param array|string $options * @throws \InvalidArgumentException * * new Cryptor('key') * new Cryptor(array('key' => 'test', 'cipher' => MCRYPT_RIJNDAEL_192)) */ public function __construct($options = array()) { if ($options) { if (is_array($options)) { $this->options = $options + $this->options; } else { $this->options['key'] = (string) $options; } } if (!$this->options['key']) { throw new \InvalidArgumentException('Cryptor need key param'); } // Find the max length of the key, based on cipher and mode $size = mcrypt_get_key_size($this->options['cipher'], $this->options['mode']); if (isset($this->options['key'][$size])) { // Shorten the key to the maximum size $this->options['key'] = substr($this->options['key'], 0, $size); } // Store the IV size $this->_iv_size = mcrypt_get_iv_size($this->options['cipher'], $this->options['mode']); // Set the rand type if it has not already been set if ($this->options['rand'] === null) { if (defined('MCRYPT_DEV_URANDOM')) { // Use /dev/urandom $this->options['rand'] = MCRYPT_DEV_URANDOM; } elseif (defined('MCRYPT_DEV_RANDOM')) { // Use /dev/random $this->options['rand'] = MCRYPT_DEV_RANDOM; } else { // Use the system random number generator $this->options['rand'] = MCRYPT_RAND; } } }
/** * Loads encryption configuration and validates the data. * * @param array|string custom configuration or config group name * @throws Kohana_Exception */ public function __construct($config = FALSE) { if (!defined('MCRYPT_ENCRYPT')) { throw new Kohana_Exception('To use the Encrypt library, mcrypt must be enabled in your PHP installation'); } if (is_string($config)) { $name = $config; // Test the config group name if (($config = Kohana::config('encryption.' . $config)) === NULL) { throw new Kohana_Exception('The :name: group is not defined in your configuration.', array(':name:' => $name)); } } if (is_array($config)) { // Append the default configuration options $config += Kohana::config('encryption.default'); } else { // Load the default group $config = Kohana::config('encryption.default'); } if (empty($config['key'])) { throw new Kohana_Exception('To use the Encrypt library, you must set an encryption key in your config file'); } // Find the max length of the key, based on cipher and mode $size = mcrypt_get_key_size($config['cipher'], $config['mode']); if (strlen($config['key']) > $size) { // Shorten the key to the maximum size $config['key'] = substr($config['key'], 0, $size); } // Find the initialization vector size $config['iv_size'] = mcrypt_get_iv_size($config['cipher'], $config['mode']); // Cache the config in the object $this->config = $config; Kohana_Log::add('debug', 'Encrypt Library initialized'); }
/** * Generate settings file */ public function generateSettingsFile($database_server, $database_login, $database_password, $database_name, $database_prefix, $database_engine) { // Check permissions for settings file if (file_exists(_PS_ROOT_DIR_ . '/' . self::SETTINGS_FILE) && !is_writable(_PS_ROOT_DIR_ . '/' . self::SETTINGS_FILE)) { $this->setError($this->language->l('%s file is not writable (check permissions)', self::SETTINGS_FILE)); return false; } elseif (!file_exists(_PS_ROOT_DIR_ . '/' . self::SETTINGS_FILE) && !is_writable(_PS_ROOT_DIR_ . '/' . dirname(self::SETTINGS_FILE))) { $this->setError($this->language->l('%s folder is not writable (check permissions)', dirname(self::SETTINGS_FILE))); return false; } // Generate settings content and write file $settings_constants = array('_DB_SERVER_' => $database_server, '_DB_NAME_' => $database_name, '_DB_USER_' => $database_login, '_DB_PASSWD_' => $database_password, '_DB_PREFIX_' => $database_prefix, '_MYSQL_ENGINE_' => $database_engine, '_PS_CACHING_SYSTEM_' => 'CacheMemcache', '_PS_CACHE_ENABLED_' => '0', '_MEDIA_SERVER_1_' => '', '_MEDIA_SERVER_2_' => '', '_MEDIA_SERVER_3_' => '', '_COOKIE_KEY_' => Tools::passwdGen(56), '_COOKIE_IV_' => Tools::passwdGen(8), '_PS_CREATION_DATE_' => date('Y-m-d'), '_PS_VERSION_' => _PS_INSTALL_VERSION_); // If mcrypt is activated, add Rijndael 128 configuration if (function_exists('mcrypt_encrypt')) { $settings_constants['_RIJNDAEL_KEY_'] = Tools::passwdGen(mcrypt_get_key_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB)); $settings_constants['_RIJNDAEL_IV_'] = base64_encode(mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND)); } $settings_content = "<?php\n"; foreach ($settings_constants as $constant => $value) { $settings_content .= "define('{$constant}', '" . str_replace('\'', '\\\'', $value) . "');\n"; } if (!file_put_contents(_PS_ROOT_DIR_ . '/' . self::SETTINGS_FILE, $settings_content)) { $this->setError($this->language->l('Cannot write settings file')); return false; } return true; }
/** * UpdateCrypto * * {@inheritdoc} */ public function __construct(SplFileInfo $fileInfo, ArrayObject $collection) { parent::__construct($fileInfo, $collection); $this->source = \Scalr::getContainer()->crypto(MCRYPT_TRIPLEDES, MCRYPT_MODE_CFB, null, 24, 8); $this->globals = \Scalr::getContainer()->crypto(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB, null, mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB), mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB)); $this->target = \Scalr::getContainer()->crypto; }
/** * Returns the correct size key for the algorithm * @return string */ private function _getKey() { $key_size = mcrypt_get_key_size($this->_algorithm, MCRYPT_MODE_ECB); $key = str_pad($this->_key, $key_size, '0'); $key = substr($key, 0, $key_size); return $key; }
public function __construct($envId, $scope = Scalr_Scripting_GlobalVariables::SCOPE_ENVIRONMENT) { $this->crypto = new Scalr_Util_CryptoTool(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB, @mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB), @mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB)); $this->cryptoKey = @file_get_contents(APPPATH . "/etc/.cryptokey"); $this->envId = $envId; $this->scope = $scope; $this->db = \Scalr::getDb(); }
/** * @param $key a per-site secret string which is used as the base encryption key. * @param $salt a per-session random string which is used as a salt to generate a per-session key * * The base encryption key needs to stay secret. If an attacker ever gets it, they can read their session, * and even modify & re-sign it. * * The salt is a random per-session string that is used with the base encryption key to create a per-session key. * This (amongst other things) makes sure an attacker can't use a known-plaintext attack to guess the key. * * Normally we could create a salt on encryption, send it to the client as part of the session (it doesn't * need to remain secret), then use the returned salt to decrypt. But we already have the Session ID which makes * a great salt, so no need to generate & handle another one. */ public function __construct($key, $salt) { $this->ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); $this->keySize = mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); $this->key = $key; $this->salt = $salt; $this->saltedkey = function_exists('hash_pbkdf2') ? hash_pbkdf2('sha256', $this->key, $this->salt, 1000, $this->keySize, true) : $this->hash_pbkdf2('sha256', $this->key, $this->salt, 100, $this->keySize); }
public function __construct($key, $algo = 0) { if (!isset(self::$algos[$algo])) { throw new Exception('AESCrypter::__construct: algorithem ID not available!', 404); } $this->algo =& self::$algos[$algo]; $this->key = substr($key, 0, mcrypt_get_key_size($this->algo, MCRYPT_MODE_ECB)); }
public static function decrypt($data) { $keyHash = md5(self::$key); $key = substr($keyHash, 0, mcrypt_get_key_size(self::$cipher, self::$mode)); $iv = substr($keyHash, 0, mcrypt_get_block_size(self::$cipher, self::$mode)); $data = base64_decode($data); $data = mcrypt_decrypt(self::$cipher, $key, $data, self::$mode, $iv); return rtrim($data); }
/** * Constructor. * * @access public * @param string $key Encryption key * @param \mako\security\crypto\padders\PadderInterface $padder Padder instance * @param int $cipher Cipher * @param int $mode Mode */ public function __construct($key, PadderInterface $padder, $cipher = null, $mode = null) { $this->key = $key; $this->padder = $padder; $this->cipher = $cipher ?: MCRYPT_RIJNDAEL_256; $this->mode = $mode ?: MCRYPT_MODE_CBC; $this->keySize = mcrypt_get_key_size($this->cipher, $this->mode); $this->ivSize = mcrypt_get_iv_size($this->cipher, $this->mode); }
/** * Takes un-encrypted text and encrypts it. * @param string $text * @return string $text */ public static function encrypt($text) { if (function_exists('mcrypt_encrypt')) { $iv_size = mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $len = mcrypt_get_key_size(MCRYPT_XTEA, MCRYPT_MODE_ECB); $text = base64_encode(mcrypt_encrypt(MCRYPT_XTEA, substr(Config::get('concrete.security.token.encryption'), 0, $len), $text, MCRYPT_MODE_ECB, $iv)); } return $text; }
/** * @param int $accountId * @param int $envId * @param string $scope */ public function __construct($accountId = 0, $envId = 0, $scope = Scalr_Scripting_GlobalVariables::SCOPE_SCALR) { $this->crypto = new Scalr_Util_CryptoTool(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB, @mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB), @mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB)); $this->cryptoKey = @file_get_contents(APPPATH . "/etc/.cryptokey"); $this->accountId = $accountId; $this->envId = $envId; $this->scope = $scope; $this->listScopes = [self::SCOPE_SCALR, self::SCOPE_ACCOUNT, self::SCOPE_ENVIRONMENT, self::SCOPE_ROLE, self::SCOPE_FARM, self::SCOPE_FARMROLE, self::SCOPE_SERVER]; $this->db = \Scalr::getDb(); }
public static function decrypt($string) { // Unpackage the encoded, encrypted string. list($encoded_string, $encoded_iv) = explode('|', $string); $encrypted_string = base64_decode($encoded_string); $iv = base64_decode($encoded_iv); // Trim encryption key to size supported by this cipher. $key = substr(DF_ENCRYPTION_KEY, 0, mcrypt_get_key_size(DF_ENCRYPTION_CIPHER, MCRYPT_MODE_ECB)); return trim(mcrypt_decrypt(DF_ENCRYPTION_CIPHER, $key, $encrypted_string, MCRYPT_MODE_ECB, $iv)); }
public static function decode($string) { $key = Config::item("application", "key"); $key = mb_substr($key, 0, mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)); list($string, $ivCode) = explode("|", $string); $string = static::from64($string); $ivCode = static::from64($ivCode); $string = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $string, MCRYPT_MODE_CBC, $ivCode); return rtrim($string, ""); }
/** * Takes un-encrypted text and encrypts it. * @param string $text * @return string $text */ public static function encrypt($text) { if (function_exists('mcrypt_encrypt')) { $iv_size = mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $len = mcrypt_get_key_size(MCRYPT_XTEA, MCRYPT_MODE_ECB); $text = base64_encode(mcrypt_encrypt(MCRYPT_XTEA, substr(Config::get('SECURITY_TOKEN_ENCRYPTION'), 0, $len), $text, MCRYPT_MODE_ECB, $iv)); } return $text; }
/** * Returns the maximum key size that can be used with a particular * cipher type. Any key size equal to or less than the returned * value are legal key sizes. Depending on if the local mycrypt * extension is linked against 2.2 or 2.3/2.4 the block mode could * be required, hence the if/else statement. * * @param string $cipher_type * @return int */ public function getKeySize($cipher_type = MCRYPT_TRIPLEDES) { $block_mode = 'cbc'; $max_key_size = mcrypt_get_key_size($cipher_type); if ($max_key_size !== false) { return $max_key_size; } else { return mcrypt_get_key_size($cipher_type, $block_mode); } }
function random_aes_key($ksize) { $chars = range(chr(0), chr(255)); if ($ksize > mcrypt_get_key_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB)) { echo 'Warning: ', __FUNCTION__, '() is intended for AES-128 key generation; key is too long.', PHP_EOL; } for ($key = '', $size = count($chars) - 1; strlen($key) < $ksize; $key .= $chars[rand(0, $size)]) { } return $key; }
/** * Takes un-encrypted text and encrypts it. * @param string $text * @return string $text */ public static function encrypt($text) { if (function_exists('mcrypt_encrypt')) { $iv_size = mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $len = mcrypt_get_key_size(MCRYPT_XTEA, MCRYPT_MODE_ECB); $text = base64_encode(mcrypt_encrypt(MCRYPT_XTEA, substr(PASSWORD_SALT, 0, $len), $text, MCRYPT_MODE_ECB, $iv)); } return $text; }
/** * Generates a key of given length with ascii characters from 33 to 126. * @param int $length the length of the key that is being generated. default: 32 * @access public static * @return string the generated ascii key */ public static function generateKey() { $length = mcrypt_get_key_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC); $index = 0; $key = ''; while ($index != $length) { $key .= chr(rand(33, 126)); $index++; } return $key; }
function _encrypt($s, $key) { $maxkey = mcrypt_get_key_size(MCRYPT_Blowfish, MCRYPT_MODE_ECB); if (strlen($key) > $maxkey) { $key = substr($key, $maxkey); } $block = mcrypt_get_block_size(MCRYPT_Blowfish, MCRYPT_MODE_ECB); $pad = $block - strlen($s) % $block; $s .= str_repeat(chr($pad), $pad); return mcrypt_encrypt(MCRYPT_Blowfish, $key, $s, MCRYPT_MODE_ECB); }
/** * @param string $secret * @param string $cipher * @param string $mode * * @throws \RuntimeException */ public function __construct($secret, $cipher = MCRYPT_RIJNDAEL_128, $mode = MCRYPT_MODE_CBC) { if (!extension_loaded('mcrypt')) { throw new \RuntimeException('Mcrypt is not available.'); } $this->cipher = $cipher; $this->mode = $mode; $this->ivSize = mcrypt_get_iv_size($this->cipher, $this->mode); $maxSize = mcrypt_get_key_size($this->cipher, $this->mode); $this->secret = strlen($secret) > $maxSize ? substr($secret, 0, $maxSize) : $secret; }
/** * Décode le mot de passe avec la clé $key * @param type $password le mot de passe à décoder * @param type $key la clé de décodage * @return type la chaine décodée */ static function decode($password, $key) { $keyHash = \md5($key); $algorithm = \MCRYPT_RIJNDAEL_128; $mode = 'cbc'; $key = \substr($keyHash, 0, \mcrypt_get_key_size($algorithm, $mode)); $iv = \substr($keyHash, 0, \mcrypt_get_block_size($algorithm, $mode)); $data = \base64_decode($password); $data = \mcrypt_decrypt($algorithm, $key, $data, $mode, $iv); return \rtrim($data); }
/** * Generates a set of keys given a random salt and a master key * * @param string $salt A random string to change the keys each encryption * @param string $key The supplied key to encrypt with * * @returns array An array of keys (a cipher key, a mac key, and a IV) */ protected function getKeys($salt, $key) { $ivSize = mcrypt_get_iv_size($this->cipher, $this->mode); $keySize = mcrypt_get_key_size($this->cipher, $this->mode); $length = 2 * $keySize + $ivSize; $key = $this->pbkdf2('sha512', $key, $salt, $this->rounds, $length); $cipherKey = substr($key, 0, $keySize); $macKey = substr($key, $keySize, $keySize); $iv = substr($key, 2 * $keySize); return array($cipherKey, $macKey, $iv); }
/** * Decrypt the given session data * * @param mixed $data Data to decrypt * @return $data Decrypted data */ private static function decrypt($data) { $data = base64_decode($data, true); $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); $keySize = mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); $key = substr(sha1(self::$key), 0, $keySize); $iv = substr($data, 0, $ivSize); $data = substr($data, $ivSize); $data = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CBC, $iv); return $data; }
private function setKey($key) { if (!is_null($key)) { $key_size = mcrypt_get_key_size($this->algo, $this->mode); $this->key = hash("whirlpool", $key, true); $this->key = substr($this->key, 0, $key_size); } if (is_null($this->key)) { trigger_error("You must specify a key at least once in either Cipher::encrpyt() or Cipher::decrypt().", E_USER_ERROR); } }
function xdecrypt($ciphertext, $passphrase) { if (function_exists('mcrypt_encrypt')) { $key_len = mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); $iv_len = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); $salt = substr($ciphertext, 8, 8); list($key, $iv) = salted_key_and_iv($key_len, $iv_len, $passphrase, $salt); return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, substr($ciphertext, 16), MCRYPT_MODE_CBC, $iv)); } else { return openssldo('decrypt', $ciphertext, $passphrase); } }
/** * Takes un-encrypted text and encrypts it. * @param string $text * @return string $text */ public static function encrypt($text) { if (function_exists('mcrypt_encrypt')) { $iv_size = mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $len = mcrypt_get_key_size(MCRYPT_XTEA, MCRYPT_MODE_ECB); /** @var \Concrete\Core\Config\Repository\Repository $config */ $config = \Core::make('config/database'); $text = base64_encode(mcrypt_encrypt(MCRYPT_XTEA, substr($config->get('concrete.security.token.encryption'), 0, $len), $text, MCRYPT_MODE_ECB, $iv)); } return $text; }
function decrypt($encoded, $algo = MCRYPT_RIJNDAEL_256, $mode = MCRYPT_MODE_CBC) { if (($secret = config('cookies.secret')) == null) { error(500, '[cookies.secret] is not set'); } $secret = mb_substr($secret, 0, mcrypt_get_key_size($algo, $mode)); list($enc_str, $iv_code) = explode('|', $encoded); $enc_str = from_b64($enc_str); $iv_code = from_b64($iv_code); $enc_str = mcrypt_decrypt($algo, $secret, $enc_str, $mode, $iv_code); return rtrim($enc_str, ""); }