Mcrypt 関数
目次
- mcrypt_create_iv — Creates an initialization vector (IV) from a random source
- mcrypt_decrypt — Decrypts crypttext with given parameters
- 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 — オープンされたアルゴリズムの IV の大きさを返す
- mcrypt_enc_get_key_size — オープンされたモードでサポートされる最大キー長を返す
- mcrypt_enc_get_modes_name — オープンされたモードの名前を返す
- mcrypt_enc_get_supported_key_sizes — オープンされたアルゴリズムでサポートされるキー長を配列にして返す
- mcrypt_enc_is_block_algorithm — オープンされたモードの暗号がブロックアルゴリズムであるかどうかを調べる
- mcrypt_enc_is_block_algorithm_mode — オープンされたモードの暗号がブロックモードで動作するかどうかを調べる
- mcrypt_enc_is_block_mode — オープンされたモードがブロック出力を行うかどうかを調べる
- mcrypt_enc_self_test — オープンしたモジュールのセルフテストを実行する
- mcrypt_encrypt — 指定したパラメータでプレーンテキストを暗号化する
- mcrypt_generic — データを暗号化する
- mcrypt_generic_deinit — 暗号化モジュールを終了する
- mcrypt_generic_init — 暗号化に必要な全てのバッファを初期化する
- mcrypt_get_block_size — 指定した暗号のブロックサイズを得る
- mcrypt_get_cipher_name — 指定した暗号の名前を得る
- mcrypt_get_iv_size — 指定した暗号/モードの組み合わせに属する IV の大きさを返す
- mcrypt_get_key_size — 指定した暗号のキーの長さを得る
- mcrypt_list_algorithms — サポートされる全ての暗号を配列として取得する
- mcrypt_list_modes — サポートされる全てのモードの配列を取得する
- mcrypt_module_close — mcrypt モジュールを閉じる
- mcrypt_module_get_algo_block_size — 指定したアルゴリズムのブロック長を返す
- mcrypt_module_get_algo_key_size — オープンされたモードでサポートされる最大キー長を返す
- mcrypt_module_get_supported_key_sizes — オープンされたアルゴリズムでサポートされるキーのサイズを配列として返す
- mcrypt_module_is_block_algorithm — 指定したアルゴリズムがブロックアルゴリズムであるかを調べる
- mcrypt_module_is_block_algorithm_mode — 指定したモジュールがブロックアルゴリズムであるかどうかを返す
- mcrypt_module_is_block_mode — 指定したモードがブロック出力を行うかどうかを返す
- mcrypt_module_open — 使用するアルゴリズムおよびモードのモジュールをオープンする
- mcrypt_module_self_test — 指定したモジュールのセルフテストを実行する
- mdecrypt_generic — データを復号する
+add a note
User Contributed Notes 24 notes
rolf at winmutt dot com ¶
18 years ago
mysql AES_ENCRYPT() compatibly function for PHP :
<?php
function mysql_aes_encrypt($val,$ky) {
$mode=MCRYPT_MODE_ECB;
$enc=MCRYPT_RIJNDAEL_128;
$val=str_pad($val, (16*(floor(strlen($val) / 16)+(strlen($val) % 16==0?2:1))), chr(16-(strlen($val) % 16)));
return mcrypt_encrypt($enc, $ky, $val, $mode, mcrypt_create_iv( mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM));
}
?>
Please note that if the strlen($ky)>16 then this function will not be compatible.
groundzero at zuavra dot net ¶
20 years ago
If you've ever compiled PHP from source (any version) you may be familiar with the [in]famous MCRYPT_BLOWFISH_128 compilation error that appears when you attempt to compile --with-mcrypt. It occurs often on Debian but not only there. The problem: during compilation, the PHP configure script always assumes that libmcrypt has been built in conjunction with libltdl. Whenever that is not the case, PHP compilation will fail later saying certain headers (such as the above blowfish example) are missing from mcrypt.h (which is misleading, they're not supposed to be there nor looked after if libltdl was properly involved). Solution: make sure your libmcrypt was linked against libltdl before you even start configuring PHP. You can check by running 'ldd lybmcrypt.so' and verifying that libltdl appears in the output. libltdl can be found in libltld[3][-dev] on Debian or in libtool-libs on Red Hat.
manyagain ¶
15 years ago
if you don't have mcrypt installed, try phpseclib - http://phpseclib.sourceforge.net
includes pure-php implementations of des, 3des, rc4, aes, and rijndael. uses mcrypt if it's available and a pure-php implementation otherwise. it also has the distinction of having the fastest existent pure-php aes implementation as per section 3.5.5. "Speed Comparisons" of the documentation.
duerra_NOT_THIS_ at pushitlive dot net ¶
18 years ago
For those of you that need to use PKCS#5 padding, the mcrypt API's for PHP do not support it. However, you can DIY using the following:
<?php
function encrypt_something($input)
{
$size = mcrypt_get_block_size('des', 'ecb');
$input = pkcs5_pad($input, $size);
$key = 'YOUR SECRET KEY HERE';
$td = mcrypt_module_open('des', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data;
}
function pkcs5_pad ($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
function pkcs5_unpad($text)
{
$pad = ord($text{strlen($text)-1});
if ($pad > strlen($text)) return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
return substr($text, 0, -1 * $pad);
}
?>
triptripon at gmail dot com ¶
18 years ago
Regarding storing the result on a postgres DB that uses Unicode (follow up to a post below).
You don't need to change the DB's encoding to ASCII.
Simply use BASE64 to encode the result, it's perfectly safe.
use base64_decode before you decrypt.
-- Tomer Levinboim
herowekker at hotmail dot com ¶
21 years ago
mcrypt_ecb with base64_decode gave some problems, i found out that it had to be chopped to work:
<?php
chop(mcrypt_ecb(MCRYPT_BLOWFISH,$key,base64_decode($input),MCRYPT_DECRYPT));
?>
Kevin ¶
16 years ago
Solved Problem:
when compiling php --with-mcrypt, phpinfo() says, that mcrypt ist enabled, but
"Supported ciphers none" and
"Supported modes none"
In order to get mcrypt to work in php, you have to configure and compile the libmcrypt source package with the following options:
./configure --disable-posix-threads --enable-dynamic-loading
simms ¶
21 years ago
DEBIAN users: avoid mcrypt installation headaches.
to add mcrypt support to an existing php installation, get root and run
apt-get install php4-mcrypt
restart your webserver, and voila.
pawelNOSPAM at rsc dot pl ¶
23 years ago
If you compiled mcrypt and php without problem, but phpinfo() shows there are no supported ciphers and modes, try to change mode to 755 on libdirs (/usr/local/libmcrypt, /usr/local/libcrypt).
patrickdk at patrickdk dot com ¶
14 years ago
This works completely with mysql aes, even long keys.
<?php
function mysql_aes_decrypt($val,$ky)
{
$key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
for($a=0;$a<strlen($ky);$a++)
$key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
$mode = MCRYPT_MODE_ECB;
$enc = MCRYPT_RIJNDAEL_128;
$dec = @mcrypt_decrypt($enc, $key, $val, $mode, @mcrypt_create_iv( @mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM ) );
return rtrim($dec,(( ord(substr($dec,strlen($dec)-1,1))>=0 and ord(substr($dec, strlen($dec)-1,1))<=16)? chr(ord( substr($dec,strlen($dec)-1,1))):null));
}
function mysql_aes_encrypt($val,$ky)
{
$key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
for($a=0;$a<strlen($ky);$a++)
$key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
$mode=MCRYPT_MODE_ECB;
$enc=MCRYPT_RIJNDAEL_128;
$val=str_pad($val, (16*(floor(strlen($val) / 16)+(strlen($val) % 16==0?2:1))), chr(16-(strlen($val) % 16)));
return mcrypt_encrypt($enc, $key, $val, $mode, mcrypt_create_iv( mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM));
}
?>
mountie at paygate dot net ¶
24 years ago
the encrypted result data maybe binary data and It make errors in sql query.
so use the base64_encode/base64_decode function with mcrypt()
try below
<?php
base64_encode(mcrypt_ecb(MCRYPT_BLOWFISH,$key,$input,MCRYPT_ENCRYPT));
mcrypt_ecb(MCRYPT_BLOWFISH,$key,base64_decode($input),MCRYPT_DECRYPT);
?>
p ete postma - googlemail.com ¶
14 years ago
mcrypt module initialization failed
I found this circumstance:
usemcrypt.php:
<?php
function protect() {
//some mcrypt/decrypt command
}
?>
filea.php:
<?php
include_once('usemcrypt.php');
function a() {
protect();
} ?>
fileb.php
<?php
function b() {
include_once('usemcrypt.php');
protect();
}
?>
filea.php and a() work fine
fileb.php and b() will report a mcrypt module initialization failed
I am assuming you can't pull that module in on the fly. If you use a file that calls mcrypt, it has to be included at the head, not in a function.
Rafael M. Salvioni ¶
15 years ago
The follow function is a implementation of the RC4 cypher algorithm in pure PHP code.
The function is used to encrypt and decrypt data.
<?php
/**
* Crypt/decrypt strings with RC4 stream cypher algorithm.
*
* @param string $key Key
* @param string $data Encripted/pure data
* @see http://pt.wikipedia.org/wiki/RC4
* @return string
*/
function rc4($key, $data)
{
// Store the vectors "S" has calculated
static $SC;
// Function to swaps values of the vector "S"
$swap = create_function('&$v1, &$v2', '
$v1 = $v1 ^ $v2;
$v2 = $v1 ^ $v2;
$v1 = $v1 ^ $v2;
');
$ikey = crc32($key);
if (!isset($SC[$ikey])) {
// Make the vector "S", basead in the key
$S = range(0, 255);
$j = 0;
$n = strlen($key);
for ($i = 0; $i < 255; $i++) {
$char = ord($key{$i % $n});
$j = ($j + $S[$i] + $char) % 256;
$swap($S[$i], $S[$j]);
}
$SC[$ikey] = $S;
} else {
$S = $SC[$ikey];
}
// Crypt/decrypt the data
$n = strlen($data);
$data = str_split($data, 1);
$i = $j = 0;
for ($m = 0; $m < $n; $m++) {
$i = ($i + 1) % 256;
$j = ($j + $S[$i]) % 256;
$swap($S[$i], $S[$j]);
$char = ord($data[$m]);
$char = $S[($S[$i] + $S[$j]) % 256] ^ $char;
$data[$m] = chr($char);
}
return implode('', $data);
}
?>
Ivan Frederiks ¶
17 years ago
To enable mcrypt extension under Windows you need to:
1) uncomment line "extension=php_mcrypt.dll" in php.ini
2) download libmcrypt.dll from http://files.edin.dk/php/win32/mcrypt/ and put it to System32 directory (for example C:\Windows\System32).
Tested on Windows XP+Apache 1.3.37+PHP 4.4.6 (as SAPI module!!!)
P.S.
I wrote this because I got "Cannot load mcrypt extension. Please check your PHP configuration." from phpMyAdmin when I simply uncommented "extension=php_mcrypt.dll" line.
vincent at verbrugh dot nl ¶
18 years ago
If you plan to use Mcrypt Encryption to store encrypted data (e.g. passwords) in a (MySQL) database make sure to set the column to BLOB rather than VARCHAR. Otherwise the data may change which can give unexpected results if you decrypt the value.
Jerry Hathaway ¶
18 years ago
After benchmarking AES in 256-bit operation, I've concluded that CBC is far faster than OFB. Using a 14.9 MiB file, on average...
Encrypt in CBC: 1.9 seconds
Encrypt in OFB: 45.7 seconds (same as CFB)
Just reading the file: ~.53 seconds
After some research, I've concluded that OFB and CFB are slightly more secure than CBC, however I believe the performance difference to be due to an implementation issue.
As a side note on ECB: As stated in the wiki linked to below, ECB is wholly inadequate. It not use an IV (whether it was supplied ot MCrypt or not), meaning the same key and plaintext always produce the same ciphertext and it doesn't hide patterns. The site shows an excellent example of this.
Very useful info:
http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
coz AT metamule D0T com ¶
18 years ago
If you're going to encrypt data with something like this and store it in postgres.
<?php
function encrypt($string, $key){
$result = '';
for($i=1; $i<=strlen($string); $i++){
$char = substr($string, $i-1, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)+ord($keychar));
$result.=$char;
}
return $result;
}
?>
Make sure when you create your database to set the encoding to SQL_ASCII because you won't be able to store this data in a database that uses UNICODE
joseph-at-digiweb-dot-net-dot-nz ¶
18 years ago
A further note for those doing interop between PHP and Java:
If you're using the BouncyCastle library on the Java side, then you can use the ZeroBytePadding mode now available in it. Mcrypt pads the data with Nulls rather than spaces....
I've sucessfully done interop using Blowfish/CBC/ZeroBytePadding between PHP and Java this way.
paul dot lewis at absolutegenius dot co dot uk ¶
19 years ago
I've spent the majority of the day attempting to get mcrypt to work under IIS6 with Windows Server 2003 (Web Edition) and PHP 5.0.4
There seems to be some incompatability with enabling certain extensions (mcrypt being one) when you are running PHP as ISAPI in this environment.
The way to solve the problem (the error will be that it cannot load php_mcrypt.dll - access is denied) is to run in CGI. While this isn't supposed to be as good performance wise, if you need the mcrypt support (or Oracle support, too, I believe) then this is the only way I've found to do it.
Jurgen Schwietering ¶
19 years ago
Attention when using keys longer than the actual key size (i.e. 160 bit instead of 128 bit).
It will work inbetween PHP scripts, but might cause problems when using openssl or other packages with this integration of mcrypt. Cut them always to the supported size (mcrypt_enc_get_key_size) to avoid sleepless hours.
scott at boothcreek dot com ¶
22 years ago
If you are using ECB mode to encrypt it does not seem to use the iv (initialization vector) for much of anything, given the same key it will always decrypt it no matter what the iv is. If you use CBC mode you must decrypt with the same iv that you encrypted with.
If you use a different iv before decrypting, your decrypt will not work. IMHO it seems better to use CBC mode than ECB as ECB will always encrypt to the same cipher text given the same plain text (leaving you open to know plaintext attacks). CBC uses the random iv which means text encrypts to different things. You probably could get the same effect from using random keys in ECB mode.
Read that in the Schneier book - Applied Cryptography (ISBN 0-471-11709-9) This book is a must for anyone seriously using any type of encryption.