本文整理汇总了PHP中openssl_decrypt函数的典型用法代码示例。如果您正苦于以下问题:PHP openssl_decrypt函数的具体用法?PHP openssl_decrypt怎么用?PHP openssl_decrypt使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了openssl_decrypt函数的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: verify
public function verify($password, $hash)
{
$key = hash(self::HASH_PRIMITIVE, $password, true);
$hash = base64_decode($hash);
$header = substr($hash, 0, self::HEADER_SIZE);
$iv = substr($hash, self::HEADER_SIZE, self::IV_LENGTH);
$ciphertext = substr($hash, self::HEADER_SIZE + self::IV_LENGTH);
$decrypted = openssl_decrypt($ciphertext, self::CIPHER_PRIMITIVE, $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
list(, $version, $rounds, $pointerSize, $dataSize) = unpack('C*', $header);
$iterationCount = pow(2, $rounds);
$dataSizeDecoded = pow(2, $dataSize);
if ($version !== 1) {
throw new \RuntimeException("Unknown version encountered");
}
if (strlen($decrypted) !== self::HASH_LENGTH + $iterationCount * $pointerSize) {
throw new \RuntimeException("Invalid data payload, was it truncated?");
}
$h = hash_init(self::HASH_PRIMITIVE);
for ($i = 0; $i < $iterationCount; $i++) {
$pointer = substr($decrypted, $i * $pointerSize, $pointerSize);
hash_update($h, $this->read($pointer, $dataSizeDecoded));
}
$test = hash_final($h, true);
return hash_equals($test, substr($decrypted, $iterationCount * $pointerSize));
}
示例3: crack_keystore
/**
* crack_keystore
*
* Makes a bruteforce to find the final hash contained in the KeyStore
* Returns the plaintext password used to encrypt de disk of the virtual machine
*/
function crack_keystore($keystore, $wordlist)
{
// Open wordlist file
$fp = fopen($wordlist, 'r');
// Continue if it is a valid resource
if (is_resource($fp)) {
// Get hash and method from keystore
$hash = get_hash_algorithm($keystore);
$method = get_openssl_method($keystore);
while (!feof($fp)) {
// Read each line of the file, it is the user password
$user_password = trim(fgets($fp));
// First call to PBKDF2
$EVP_password = hash_pbkdf2($hash, $user_password, $keystore['pbkdf2_1_salt'], $keystore['pbkdf2_1_iterations'], $keystore['generic_key_length'], true);
// Here, the password used for the second call to PBKDF2 is decrypted
$decrypted_password = openssl_decrypt(substr($keystore['pbkdf2_2_encrypted_password'], 0, $keystore['evp_decrypt_input_length']), $method, $EVP_password, OPENSSL_RAW_DATA, '');
if ($decrypted_password === false) {
continue;
}
// Final hash is computed
$final_hash = hash_pbkdf2($hash, $decrypted_password, $keystore['pbkdf2_2_salt'], $keystore['pbkdf2_2_iterations'], $keystore['pbkdf2_2_key_length'], true);
// If the computed hash is equal to the stored hash, then we have got the right user password
if ($final_hash === $keystore['final_hash']) {
return $user_password;
}
}
return false;
} else {
return false;
}
}
示例4: decrypt
/**
* Decrypt a string.
*
* @param string $value Encrypted string
*
* @throws Exception
*
* @return string
*/
public function decrypt($value)
{
$decoded = $this->url_decode($value);
$iv = substr($decoded, 0, 16);
$encryptedValue = str_replace($iv, '', $decoded);
return trim(openssl_decrypt($encryptedValue, $this->encoding, $this->password, null, $iv));
}
示例5: decrypt
/**
* Decrypt data from a CryptoJS json encoding string
*
* @param mixed $passphrase
* @param mixed $jsonString
* @return mixed
*/
public static function decrypt($passphrase, $jsonString)
{
$jsondata = json_decode($jsonString, true);
if (!isset($jsondata['s']) || !isset($jsondata['iv']) || !isset($jsondata['ct'])) {
return false;
}
try {
$salt = hex2bin($jsondata['s']);
$iv = hex2bin($jsondata['iv']);
} catch (Exception $e) {
return null;
}
$ct = base64_decode($jsondata['ct']);
$concatedPassphrase = $passphrase . $salt;
$md5 = [];
$md5[0] = md5($concatedPassphrase, true);
$result = $md5[0];
for ($i = 1; $i < 3; $i++) {
$md5[$i] = md5($md5[$i - 1] . $concatedPassphrase, true);
$result .= $md5[$i];
}
$key = substr($result, 0, 32);
$data = openssl_decrypt($ct, 'aes-256-cbc', $key, true, $iv);
return json_decode($data, true);
}
示例6: decrypt
/**
* @param string $base64 encrypted data in base64 OpenSSL format
* @param string $passphrase
* @return string
*/
public static function decrypt($base64, $passphrase)
{
list($ct, $salt) = self::decode($base64);
list($key, $iv) = self::evpkdf($passphrase, $salt);
$data = openssl_decrypt($ct, 'aes-256-cbc', $key, true, $iv);
return $data;
}
示例7: decrypt
/**
* Gera um novo arquivo descriptografado
* @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 decrypt($from, $to)
{
$iv_size = $this->getCipherIvLength();
$data = openssl_decrypt(substr(file_get_contents($from), $iv_size + strlen($this->password)), $this->cipher_method, substr(file_get_contents($from), $iv_size, strlen($this->password)), OPENSSL_RAW_DATA, substr(file_get_contents($from), 0, $iv_size));
file_put_contents($to, $data);
return new \SplFileInfo($to);
}
示例8: 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;
}
}
}
示例9: decrypt
/**
* Decrypt AES (256, 192, 128)
* @param $string base64 encoded cipher
* @param $key string algorithm encryption
* @return dencrypted string
*/
function decrypt($string, $key)
{
// Lengths in bytes:
$key_length = (int) ($this->_nKeySize / 8);
$block_length = 16;
$data = base64_decode($string);
$salt = substr($data, 8, 8);
$encrypted = substr($data, 16);
/**
* From https://github.com/mdp/gibberish-aes
*
* Number of rounds depends on the size of the AES in use
* 3 rounds for 256
* 2 rounds for the key, 1 for the IV
* 2 rounds for 128
* 1 round for the key, 1 round for the IV
* 3 rounds for 192 since it's not evenly divided by 128 bits
*/
$rounds = 3;
if (128 === $this->_nKeySize) {
$rounds = 2;
}
$data00 = $key . $salt;
$md5_hash = array();
$md5_hash[0] = md5($data00, true);
$result = $md5_hash[0];
for ($i = 1; $i < $rounds; $i++) {
$md5_hash[$i] = md5($md5_hash[$i - 1] . $data00, true);
$result .= $md5_hash[$i];
}
$key = substr($result, 0, $key_length);
$iv = substr($result, $key_length, $block_length);
return openssl_decrypt($encrypted, "aes-" . $this->_nKeySize . "-cbc", $key, true, $iv);
}
示例10: 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;
}
示例11: decrypt
/**
* Decrypt a string.
*
* @access public
* @static static method
* @param string $ciphertext
* @return string
* @throws Exception If $ciphertext is empty, or If functions don't exists
*/
public static function decrypt($ciphertext)
{
if (empty($ciphertext)) {
throw new Exception("the string to decrypt can't be empty");
}
if (!function_exists('openssl_cipher_iv_length') || !function_exists('openssl_decrypt')) {
throw new Exception("Encryption function don't exists");
}
// generate key used for authentication using ENCRYPTION_KEY & HMAC_SALT
$key = mb_substr(hash(self::HASH_FUNCTION, Config::get('ENCRYPTION_KEY') . Config::get('HMAC_SALT')), 0, 32, '8bit');
// split cipher into: hmac, cipher & iv
$macSize = 64;
$hmac = mb_substr($ciphertext, 0, $macSize, '8bit');
$iv_cipher = mb_substr($ciphertext, $macSize, null, '8bit');
// generate original hmac & compare it with the one in $ciphertext
$originalHmac = hash_hmac('sha256', $iv_cipher, $key);
if (!function_exists("hash_equals")) {
throw new Exception("Function hash_equals() doesn't exist!");
}
if (!hash_equals($hmac, $originalHmac)) {
return false;
}
// split out the initialization vector and cipher
$iv_size = openssl_cipher_iv_length(self::CIPHER);
$iv = mb_substr($iv_cipher, 0, $iv_size, '8bit');
$cipher = mb_substr($iv_cipher, $iv_size, null, '8bit');
return openssl_decrypt($cipher, self::CIPHER, $key, OPENSSL_RAW_DATA, $iv);
}
示例12: decrypt
/**
* @param string $encrypted
* @return string
*/
public function decrypt($encrypted)
{
$encrypted = base64_decode($encrypted);
$iv = substr($encrypted, 0, $this->ivSize);
$encryptedMessage = substr($encrypted, $this->ivSize);
return openssl_decrypt($encryptedMessage, $this->cipher, $this->key, true, $iv);
}
示例13: decrypt
/**
* Two way encryption: decrypt.
*
* @param string $data String to be encrypted.
* @param string $password Value phrase.
* @param string $type Cipher method name.
*
* @return string
*/
public static function decrypt($data, $password, $type)
{
if ($data) {
return openssl_decrypt($data, $type, $password, 0, Core\Config()->DB['crypt_vector']);
}
return '';
}
示例14: decrypt
/**
* decrypt AES 256
*
* @param string $password
* @param data $edata
*
* @return dencrypted data
*/
public static function decrypt($password, $edata)
{
$data = base64_decode($edata);
$salt = substr($data, 8, 8);
$ct = substr($data, 16);
/**
* From https://github.com/mdp/gibberish-aes
*
* Number of rounds depends on the size of the AES in use
* 3 rounds for 256
* 2 rounds for the key, 1 for the IV
* 2 rounds for 128
* 1 round for the key, 1 round for the IV
* 3 rounds for 192 since it's not evenly divided by 128 bits
*/
$rounds = 3;
$data00 = $password . $salt;
$md5_hash = array();
$md5_hash[0] = md5($data00, true);
$result = $md5_hash[0];
for ($i = 1; $i < $rounds; $i++) {
$md5_hash[$i] = md5($md5_hash[$i - 1] . $data00, true);
$result .= $md5_hash[$i];
}
$key = substr($result, 0, 32);
$iv = substr($result, 32, 16);
return openssl_decrypt($ct, 'aes-256-cbc', $key, true, $iv);
}
示例15: decode
public function decode($data)
{
$ivLength = openssl_cipher_iv_length($this->cipher);
$iv = substr($data, 0, $ivLength);
$data = substr($data, $ivLength);
return openssl_decrypt($data, $this->cipher, $this->password, OPENSSL_RAW_DATA, $iv);
}