Регистрация Войти
Войти через VK Войти через FB Войти через Google Войти через Яндекс
Войти через VK Войти через FB Войти через Google Войти через Яндекс
Поиск по сайту
Mcrypt
- Введение
- Установка и настройка
- Требования
- Установка
- Настройка во время выполнения
- Типы ресурсов
- Предопределенные константы
- Mcrypt ciphers
- Примеры
- Mcrypt
- mcrypt_cbc — Шифрует/дешифрует даные в режиме CBC
- mcrypt_cfb — Encrypts/decrypts data in CFB mode
- mcrypt_create_iv — Creates an initialization vector (IV) from a random source
- mcrypt_decrypt — Decrypts crypttext with given parameters
- mcrypt_ecb — Deprecated: Encrypts/decrypts data in ECB mode
- mcrypt_enc_get_algorithms_name — Returns the name of the opened algorithm
- mcrypt_enc_get_block_size — Returns the blocksize of the opened algorithm
- mcrypt_enc_get_iv_size — Returns the size of the IV of the opened algorithm
- mcrypt_enc_get_key_size — Returns the maximum supported keysize of the opened mode
- mcrypt_enc_get_modes_name — Returns the name of the opened mode
- mcrypt_enc_get_supported_key_sizes — Returns an array with the supported keysizes of the opened algorithm
- mcrypt_enc_is_block_algorithm_mode — Checks whether the encryption of the opened mode works on blocks
- mcrypt_enc_is_block_algorithm — Checks whether the algorithm of the opened mode is a block algorithm
- mcrypt_enc_is_block_mode — Checks whether the opened mode outputs blocks
- mcrypt_enc_self_test — Runs a self test on the opened module
- mcrypt_encrypt — Encrypts plaintext with given parameters
- mcrypt_generic_deinit — This function deinitializes an encryption module
- mcrypt_generic_end — This function terminates encryption
- mcrypt_generic_init — This function initializes all buffers needed for encryption
- mcrypt_generic — This function encrypts data
- mcrypt_get_block_size — Gets the block size of the specified cipher
- mcrypt_get_cipher_name — Gets the name of the specified cipher
- mcrypt_get_iv_size — Returns the size of the IV belonging to a specific cipher/mode combination
- mcrypt_get_key_size — Gets the key size of the specified cipher
- mcrypt_list_algorithms — Gets an array of all supported ciphers
- mcrypt_list_modes — Gets an array of all supported modes
- mcrypt_module_close — Closes the mcrypt module
- mcrypt_module_get_algo_block_size — Returns the blocksize of the specified algorithm
- mcrypt_module_get_algo_key_size — Returns the maximum supported keysize of the opened mode
- mcrypt_module_get_supported_key_sizes — Returns an array with the supported keysizes of the opened algorithm
- mcrypt_module_is_block_algorithm_mode — Returns if the specified module is a block algorithm or not
- mcrypt_module_is_block_algorithm — This function checks whether the specified algorithm is a block algorithm
- mcrypt_module_is_block_mode — Returns if the specified mode outputs blocks or not
- mcrypt_module_open — Opens the module of the algorithm and the mode to be used
- mcrypt_module_self_test — This function runs a self test on the specified module
- mcrypt_ofb — Encrypts/decrypts data in OFB mode
- mdecrypt_generic — Decrypts data
$modes = mcrypt_list_modes();
$algorithms = mcrypt_list_algorithms();
foreach($algorithms as $cipher)
{
echo "<h1 style=\"border-top:1px solid black;\">".$cipher."</h1>\n";
foreach($modes as $mode)
{
echo "<h3>".$mode."</h3>\n";
@$td = mcrypt_module_open(
$cipher,
'/usr/local/libmcrypt-2.5.8/modules/algorithms/',
$mode,
'/usr/local/libmcrypt-2.5.8/modules/modes/');
@$key_size = mcrypt_enc_get_key_size($td);
@$block_size = mcrypt_get_block_size($cipher,$mode);
@$iv_size = mcrypt_get_iv_size($cipher, $mode);
@mcrypt_module_close($td);
echo "
<pre>
key_size: ". ($key_size?$key_size:'n/a')
." block_size: ". ($block_size?$block_size:'n/a')
." iv_size: ". ($iv_size?$iv_size:'n/a')
." </pre>\n";
$td=NULL;
$key_size=NULL;
$block_size=NULL;
$iv_size=NULL;
}
}
12
Anonymous ¶3 years ago
function encrypt($decrypted, $password, $salt='!kQm*fF3pXe1Kbm%9') {
// Build a 256-bit $key which is a SHA256 hash of $salt and $password.
$key = hash('SHA256', $salt . $password, true);
// Build $iv and $iv_base64. We use a block size of 128 bits (AES compliant) and CBC mode. (Note: ECB mode is inadequate as IV is not used.)
srand(); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22) return false;
// Encrypt $decrypted and an MD5 of $decrypted using $key. MD5 is fine to use here because it's just to verify successful decryption.
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted . md5($decrypted), MCRYPT_MODE_CBC, $iv));
// We're done!
return $iv_base64 . $encrypted;
}
function decrypt($encrypted, $password, $salt='!kQm*fF3pXe1Kbm%9') {
// Build a 256-bit $key which is a SHA256 hash of $salt and $password.
$key = hash('SHA256', $salt . $password, true);
// Retrieve $iv which is the first 22 characters plus ==, base64_decoded.
$iv = base64_decode(substr($encrypted, 0, 22) . '==');
// Remove $iv from $encrypted.
$encrypted = substr($encrypted, 22);
// Decrypt the data. rtrim won't corrupt the data because the last 32 characters are the md5 hash; thus any \0 character has to be padding.
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv), "\0\4");
// Retrieve $hash which is the last 32 characters of $decrypted.
$hash = substr($decrypted, -32);
// Remove the last 32 characters from $decrypted.
$decrypted = substr($decrypted, 0, -32);
// Integrity check. If this fails, either the data is corrupted, or the password/salt was incorrect.
if (md5($decrypted) != $hash) return false;
// Yay!
return $decrypted;
}
3
Maarten Malaise ¶4 years ago
if (isset($_REQUEST['select'])) {
$choice = $_POST['select'];
$mykey = $_POST['mykey'];
$msg = $_POST['plain'];
} else {
die("algorithm not selected");
}
if ($msg == ''){
die("Please enter a text to encrypt! ");
}
if ($mykey == ''){
die("Please enter a key! ");
}
function algorithmdetails($cipher)
{
$chiphername = mcrypt_enc_get_algorithms_name($cipher);
$blocksize = mcrypt_enc_get_block_size($cipher);
$mykeysize = mcrypt_enc_get_supported_key_sizes($cipher);
echo "<p><b>Cipher Name :</b> $chiphername";
echo "<p><b>Block size :</b> $blocksize bytes";
echo "<p><b>Key size :</b> ";
foreach ($mykeysize as $value)
{
echo "$value bytes ";
}unset($value);
}
function encryptnow($thecipher, $thekey, $themsg)
{
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($thecipher), MCRYPT_DEV_RANDOM);
mcrypt_generic_init($thecipher, $thekey, $iv);
$encrypted_text = mcrypt_generic($thecipher, $themsg);
echo "<html><hr size='2' ></html>";
echo "<P><P><b>Plain Text : </b>";
echo($themsg);
echo "<p><b>Cipher Text : </b> ";
echo "$encrypted_text";
mcrypt_generic_deinit($thecipher);
mcrypt_module_close($thecipher);
die();
}
if ($choice == '1'){
$cipher = mcrypt_module_open (MCRYPT_DES, '', 'ecb', '');
algorithmdetails($cipher);
encryptnow($cipher, $mykey, $msg);
}elseif ($choice == '2'){
$cipher = mcrypt_module_open (MCRYPT_3DES, '', 'ecb', '');
algorithmdetails($cipher);
encryptnow($cipher, $mykey, $msg);
}elseif ($choice == '3'){
$cipher = mcrypt_module_open (MCRYPT_RIJNDAEL_128, '', 'ecb', '');
algorithmdetails($cipher);
encryptnow($cipher, $mykey, $msg);
}elseif ($choice == '4'){
$cipher = mcrypt_module_open (MCRYPT_GOST, '', 'ecb', '');
algorithmdetails($cipher);
encryptnow($cipher, $mykey, $msg);
}else {
die("Please choose an algorithm!");
}
Описание класса book, примеры использования класса book.
Смотрите также:
Описание на ru2.php.net
Описание на php.ru