note 123489 added to function.mcrypt-encrypt
| From: | Legendblogs@php.net | Date: | Fri, 04 Jan 2019 05:30:30 +0000 |
| Subject: | note 123489 added to function.mcrypt-encrypt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209988@lists.php.net to get a copy of this message | ||
Mcrypt is a replacement for the popular Unix crypt command. the crypt was a file encryption tool
that used an algorithm very close to the World War II Enigma cipher. Mcrypt provides the same
functionality but uses several modern algorithms such as AES.
class MCrypt {
private $iv = 'abcdef9876541720';
private $key = '1720456789fedcba';
function __construct() {
}
/**
* @param string $str
* @param bool $isBinary whether to encrypt as binary or not. Default is: false
* @return string Encrypted data
*/
function encrypt($str, $isBinary = false) {
$iv = $this->iv;
$str = $isBinary ? $str : utf8_decode($str);
$td = mcrypt_module_open('rijndael-128', ' ', 'cbc', $iv);
mcrypt_generic_init($td, $this->key, $iv);
$encrypted = mcrypt_generic($td, $str);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $isBinary ? $encrypted : bin2hex($encrypted);
}
/**
* @param string $code
* @param bool $isBinary whether to decrypt as binary or not. Default is: false
* @return string Decrypted data
*/
function decrypt($code, $isBinary = false) {
$code = $isBinary ? $code : $this->hex2bin($code);
$iv = $this->iv;
$td = mcrypt_module_open('rijndael-128', ' ', 'cbc', $iv);
mcrypt_generic_init($td, $this->key, $iv);
$decrypted = mdecrypt_generic($td, $code);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $isBinary ? trim($decrypted) : utf8_encode(trim($decrypted));
}
protected function hex2bin($hexdata) {
$bindata = '';
for ($i = 0; $i < strlen($hexdata); $i += 2) {
$bindata .= chr(hexdec(substr($hexdata, $i, 2)));
}
return $bindata;
}
}
function encryptkey($key) {
$mcrypt = new MCrypt();
$encrypted = $mcrypt->encrypt($key);
return $encrypted;
}
function decryptkey($key) {
$mcrypt = new MCrypt();
$decrypted = $mcrypt->decrypt($key);
return $decrypted;
}
$value = "Legend Blogs";
echo $value.'<br>';
echo "Encrypt: ". encryptkey($value).'<br>';
echo "Decrypt: ". decryptkey(encryptkey($value)).'<br>';
Complete example at here<a href="https://www.legendblogs.com/blog/how-do-you-encrypt-and-decrypt-a-php-string/121787">How
do you Encrypt and Decrypt a PHP String</a>
----
Server IP: 208.43.231.9
Probable Submitter: 2a02:cb43:e::301 (proxied: 103.97.184.187)
----
Manual Page -- http://php.net/manual/en/function.mcrypt-encrypt.php
Edit -- https://master.php.net/note/edit/123489
Del: integrated -- https://master.php.net/note/delete/123489/integrated
Del: useless -- https://master.php.net/note/delete/123489/useless
Del: bad code -- https://master.php.net/note/delete/123489/bad+code
Del: spam -- https://master.php.net/note/delete/123489/spam
Del: non-english -- https://master.php.net/note/delete/123489/non-english
Del: in docs -- https://master.php.net/note/delete/123489/in+docs
Del: other reasons-- https://master.php.net/note/delete/123489
Reject -- https://master.php.net/note/reject/123489
Search -- https://master.php.net/manage/user-notes.php