mcrypt_cbc
(PHP 4, PHP 5)
mcrypt_cbc — Encrypts/decrypts data in CBC mode
Description
string mcrypt_cbc
( int
$cipher
, string $key
, string $data
, int $mode
[, string $iv
] )
string mcrypt_cbc
( string
$cipher
, string $key
, string $data
, int $mode
[, string $iv
] )
The first prototype is when linked against libmcrypt 2.2.x, the
second when linked against libmcrypt 2.4.x or higher. The
mode
should be either
MCRYPT_ENCRYPT
or
MCRYPT_DECRYPT
.
This function should not be used anymore, see mcrypt_generic() and mdecrypt_generic() for replacements.
Warning
This function has been DEPRECATED as of PHP 5.5.0. Relying on this function is highly discouraged.
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Криптографические расширения
- Mcrypt
- mcrypt_cbc
- mcrypt_cfb
- mcrypt_create_iv
- mcrypt_decrypt
- mcrypt_ecb
- mcrypt_enc_get_algorithms_name
- mcrypt_enc_get_block_size
- mcrypt_enc_get_iv_size
- mcrypt_enc_get_key_size
- mcrypt_enc_get_modes_name
- mcrypt_enc_get_supported_key_sizes
- mcrypt_enc_is_block_algorithm_mode
- mcrypt_enc_is_block_algorithm
- mcrypt_enc_is_block_mode
- mcrypt_enc_self_test
- mcrypt_encrypt
- mcrypt_generic_deinit
- mcrypt_generic_end
- mcrypt_generic_init
- mcrypt_generic
- mcrypt_get_block_size
- mcrypt_get_cipher_name
- mcrypt_get_iv_size
- mcrypt_get_key_size
- mcrypt_list_algorithms
- mcrypt_list_modes
- mcrypt_module_close
- mcrypt_module_get_algo_block_size
- mcrypt_module_get_algo_key_size
- mcrypt_module_get_supported_key_sizes
- mcrypt_module_is_block_algorithm_mode
- mcrypt_module_is_block_algorithm
- mcrypt_module_is_block_mode
- mcrypt_module_open
- mcrypt_module_self_test
- mcrypt_ofb
- mdecrypt_generic
Коментарии
The following code is to
encrypt-->encode
decode-->decrypt
<?php
$stuff="String to enc/enc/dec/dec =,=,";
$key="XiTo74dOO09N48YeUmuvbL0E";
function nl() {
echo "<br/> \n";
}
$iv = mcrypt_create_iv (mcrypt_get_block_size (MCRYPT_TripleDES, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM);
// Encrypting
function encrypt($string, $key) {
$enc = "";
global $iv;
$enc=mcrypt_cbc (MCRYPT_TripleDES, $key, $string, MCRYPT_ENCRYPT, $iv);
return base64_encode($enc);
}
// Decrypting
function decrypt($string, $key) {
$dec = "";
$string = trim(base64_decode($string));
global $iv;
$dec = mcrypt_cbc (MCRYPT_TripleDES, $key, $string, MCRYPT_DECRYPT, $iv);
return $dec;
}
$encrypted = encrypt($stuff, $key);
$decrypted = decrypt($encrypted, $key);
echo "Encrypted is ".$encrypted . nl();
echo "Decrypted is ".$decrypted . nl();
?>
Notes on the result
-o running the script from the command line 1 time
php script.php works correct
-o if i ran the script in a loop i.e.
#!/bin/bash
for x in `seq 100`
do
echo $x
php script.php >> LOG.text
sleep 1
done
it gets slower after the 10th time
+ inconsistent output
sometimes correct some with encrypted characters at the end of the decrypted string
-o Firefox on Linux it decrypts to the original string but appends
some encryption at the end of the decrypted sting
-o running the script on the browser from windows on Firefox,
Google Chrome, IE7 works fine for just few times
"refresh every few seconds"
if refresh fast it doesn't work correctly,
some times returns the decrypted as encrypted!
some times returns mix of encrypted & decrypted
I thought if sharing that test for those functions might be
useful for some body
if you use blowfish and somehow when you compare strings before and after it was (usually when it's shorter than 8 or 16 bytes) you might notice the difference, it comes out that this function does not remove padding, as a result you get a bunch of nulls at the end.
use
$decrypted = rtrim($decrypted,"\0");
to fix it
The PERL libraries have changed a little bit and getting PHP and PERL to mcrypt together can be confusing, so here is a current example;
PHP
===
<?php
$string = 'Some Secret thing I want to encrypt';
$iv = '12345678';
$passphrase = '8chrsLng';
$encryptedString = encryptString($string, $passphrase, $iv);
// Expect: 7DjnpOXG+FrUaOuc8x6vyrkk3atSiAf425ly5KpG7lOYgwouw2UATw==
function encryptString($unencryptedText, $passphrase, $iv) {
$enc = mcrypt_encrypt(MCRYPT_BLOWFISH, $passphrase, $unencryptedText, MCRYPT_MODE_CBC, $iv);
return base64_encode($enc);
}
?>
PERL
====
$encryptedString = '7DjnpOXG+FrUaOuc8x6vyrkk3atSiAf425ly5KpG7lOYgwouw2UATw==';
$iv = '12345678';
$passphrase = '8chrsLng';
$string = &decryptPhpEncrypted $encryptedString, $passphrase, $iv;
# Expect: Some Secret thing I want to encrypt
sub decryptPhpEncrypted() {
my ($encryptedString, $passphrase, $iv) = @_;
my $keysize = length($passphrase);
use Crypt::CBC;
$cipher = Crypt::CBC->new( {'key' => $encryptedString,
'cipher'=> 'Blowfish',
'iv' => $iv,
'keysize' => $keysize,
'regenerate_key' => 0,
'padding' => 'null',
'prepend_iv' => 0});
return $cipher->decrypt($encryptedString);
}