Mcrypt库支持20多种加密算法和8种加密模式,具体可以通过函数mcrypt_list_algorithms()和mcrypt_list_modes()来显示
Mcrypt扩展库可以实现加密解密功能,就是既能将明文加密,也可以密文还原。
1.PHP加密扩展库Mcrypt安装
在标准的PHP安装过程中并没有把Mrcypt安装上,免备案空间,但PHP的主目录下包含了libmcrypt.dll和libmhash.dll文件 (libmhash.dll是Mhash扩展库,这里可以一起装上)。首先,将这两个文件复制到系统目录windows\system32下,然后在 PHP.ini文件中按Ctrl+F快捷键跳出查找框,并找到;extension=php-mcrypt.dll和; extension=php_mhash.dll这两个语句,接着将前面的“;”去掉;最后,保存并重启Apache服务器即可生效。
2.PHP加密扩展库Mcrypt的算法和加密模式
Mcrypt库支持20多种加密算法和8种加密模式,具体可以通过函数mcrypt_list_algorithms()和mcrypt_list_modes()来显示,结果如下:
Mcrypt支持的算法有:cast-128 gost rijndael-128 twofish arcfour cast-256 loki97 rijndael-192 saferplus wake blowfish-compat des rijndael-256 serpent xtea blowfish enigma rc2 tripledes
Mcrypt支持的加密模式有:cbc cfb ctr ecb ncfb nofb ofb stream
这些算法和模式在应用中要以常量来表示,写的时候加上前缀MCRYPT_和MCRYPT_来表示,如下面Mcrypt应用的例子:
DES算法表示为MCRYPT_DES;
ECB模式表示为MCRYPT_MODE_ECB;
3.PHP加密扩展库Mcrypt应用
先看一个例子,了解Mcrypt的工作流程,香港服务器,再来看看部分流程使用的函数:
复制代码 代码如下:
$str = "我是李云";
$key = "123qwe.019860905061X";
$cipher = MCRYPT_RIJNDAEL_128;
$mode = MCRYPT_MODE_ECB;
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$mode),MCRYPT_RAND);
echo "原文:".$str."
";
$str_encrypt = mcrypt_encrypt($cipher,$key,$str,$mode,$iv);
echo "加密后的内容是:".$str_encrypt."
";
$str_decrypt = mcrypt_decrypt($cipher,$key,$str_encrypt,$mode,$iv);
echo "解密后的内容:".$str_decrypt."
";
运行结果:
原文:我是李云
加密后的内容是:B@鴹�=(I辩蝣Z%
解密后的内容:我是李云
由例子中可看到,使用PHP加密扩展库Mcrypt对数据加密和解密之前,首先创建了一个初始化向量,美国服务器,简称为iv。由 $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$modes),MCRYPT_RAND);可见创建初始化 向 量需要两个参数:size指定了iv的大小;source为iv的源,其中值MCRYPT_RAND为系统随机数。
函数mcrypt_get_iv_size($cipher,$modes)返回初始化向量大小,参数cipher和mode分别指算法和加 密模式。
加密函数$str_encrypt = mcrypt_encrypt($cipher,$key,$str,$modes,$iv); 该函数的5个参数分 别如下:cipher——加密算法、key——密钥、data(str)——需要加密的数据、mode——算法模式、 iv——初始化向量
解密函数 mcrypt_decrypt($cipher,$key,$str_encrypt,$modes,$iv); 该函数和加密函数的参数几乎 一样,唯一不同的是data,也就是说data为需要解密的数据$str_encrypt,而不是原始数据$str。
//手册里的写法:
复制代码 代码如下:
//指定初始化向量iv的大小:
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
//创建初始化向量:
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
//加密密码:
$key = "123qwe.019860905061x";
//原始内容(未加密):
$text = "My name is Adam Li!";
echo $text. "
\n";
//加密后的内容:
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
echo $crypttext. "\n
";
//解密已经加密的内容:
$str_decrypt = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
echo $str_decrypt;
下面是一个加/解密请求的例子:
复制代码 代码如下:
$request_params = array(
'controller' => 'todo',
'action' => 'read',
'username' => "bl",
'userpass' => "a1"
);
$private_key = "28e336ac6c9423d946ba02d19c6a2632";
//encrypt request
$enc_request = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $private_key, json_encode($request_params), MCRYPT_MODE_ECB));
echo "CRYPT:".$enc_request."
";
//decrypt request
$params = json_decode(trim(mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $private_key, base64_decode($enc_request), MCRYPT_MODE_ECB )),true);
echo "ENCRYPT:
";
//print result
var_dump($params);
注:加密和解密函数中的参数cipher、key和mode必须一一对应,否则数据不能被还原。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)
