Home > php教程 > php手册 > 在PHP中使用AES加密算法加密数据

在PHP中使用AES加密算法加密数据

WBOY
Release: 2016-06-06 19:45:32
Original
1322 people have browsed it

在研究Discuz 的时候,发现Discuz有一套相当完美的加密算法(相对而言)。这个算法可以将数据加密后,储存起来,到需要用的时候,用之前加密的秘钥将之还原。 除了这个之外,还有AES这个算法能够将数据很好的加密起来,在传输过程中不容易被破解。 在PHP中,我

在研究Discuz 的时候,发现Discuz有一套相当完美的加密算法(相对而言)。这个算法可以将数据加密后,储存起来,到需要用的时候,用之前加密的秘钥将之还原。

    除了这个之外,还有AES这个算法能够将数据很好的加密起来,在传输过程中不容易被破解。

    在PHP中,我们必须先安装好mcrypt这个模块,并且添加相应版本的扩展到php中,详情可以看 不重新编译PHP安装Mcrypt扩展

AES加密模式和填充方式有以下之中,但不是全部

 

算法/模式/填充                16字节加密后数据长度        不满16字节加密后长度
AES/CBC/NoPadding             16                          不支持
AES/CBC/PKCS5Padding          32                          16
AES/CBC/ISO10126Padding       32                          16
AES/CFB/NoPadding             16                          原始数据长度
AES/CFB/PKCS5Padding          32                          16
AES/CFB/ISO10126Padding       32                          16
AES/ECB/NoPadding             16                          不支持
AES/ECB/PKCS5Padding          32                          16
AES/ECB/ISO10126Padding       32                          16
AES/OFB/NoPadding             16                          原始数据长度
AES/OFB/PKCS5Padding          32                          16
AES/OFB/ISO10126Padding       32                          16
AES/PCBC/NoPadding            16                          不支持
AES/PCBC/PKCS5Padding         32                          16
AES/PCBC/ISO10126Padding      32                          16
Copy after login

下面就是在PHP中使用AES对数据加密

 AES-CBC 加密方案

<code><span><span><?php <span>$privateKey <span>= <span>"1234567812345678"<span>;
<span>$iv 	<span>= <span>"1234567812345678"<span>;
<span>$data 	<span>= <span>"Test String"<span>;
<span>
//加密
<span>$encrypted <span>= <span>mcrypt_encrypt<span>(<span>MCRYPT_RIJNDAEL_128<span>, <span>$privateKey<span>, <span>$data<span>, <span>MCRYPT_MODE_CBC<span>, <span>$iv<span>)<span>;
<span>echo<span>(<span>base64_encode<span>(<span>$encrypted<span>)<span>)<span>;
<span>echo '<span><span><span><span><br>/>'<span>;
<span>
//解密
<span>$encryptedData <span>= <span>base64_decode<span>(<span>"2fbwW9+8vPId2/foafZq6Q=="<span>)<span>;
<span>$decrypted <span>= <span>mcrypt_decrypt<span>(<span>MCRYPT_RIJNDAEL_128<span>, <span>$privateKey<span>, <span>$encryptedData<span>, <span>MCRYPT_MODE_CBC<span>, <span>$iv<span>)<span>;
<span>echo<span>(<span>$decrypted<span>)<span>;
<span>?></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
Copy after login

AES-ECB加密方案 

<code><span><?php <span>
//加密    
<span>$key <span>= <span>'1234567890123456'<span>;    
<span>$content <span>= <span>'hello'<span>;    
<span>$padkey <span>= <span>pad2Length<span>(<span>$key<span>,<span>16<span>)<span>;    
<span>$cipher <span>= <span>mcrypt_module_open<span>(<span>MCRYPT_RIJNDAEL_128<span>, <span>''<span>, <span>MCRYPT_MODE_ECB<span>, <span>''<span>)<span>;    
<span>$iv_size <span>= <span>mcrypt_enc_get_iv_size<span>(<span>$cipher<span>)<span>;    
<span>$iv <span>= <span>mcrypt_create_iv<span>(<span>$iv_size<span>, <span>MCRYPT_RAND<span>)<span>;<span> #IV自动生成?    
<span>echo <span>'自动生成iv的长度:'<span>.<span>strlen<span>(<span>$iv<span>)<span>.<span>'位:'<span>.<span>bin2hex<span>(<span>$iv<span>)<span>.'<span><span><span><span><br>>'<span>;    
<span>if <span>(<span>mcrypt_generic_init<span>(<span>$cipher<span>, <span>pad2Length<span>(<span>$key<span>,<span>16<span>)<span>, <span>$iv<span>) <span>!<span>= <span>-<span>1<span>)    
<span>{    
   <span> // PHP pads with NULL bytes if $content is not a multiple of the block size..    
    <span>$cipherText <span>= <span>mcrypt_generic<span>(<span>$cipher<span>,<span>pad2Length<span>(<span>$content<span>,<span>16<span>) <span>)<span>;    
    <span>mcrypt_generic_deinit<span>(<span>$cipher<span>)<span>;    
    <span>mcrypt_module_close<span>(<span>$cipher<span>)<span>;    
       
   <span> // Display the result in hex.    
    <span>printf<span>(<span>"128-bit encrypted result:n%snn"<span>,<span>bin2hex<span>(<span>$cipherText<span>)<span>)<span>;    
    <span>print<span>("<span><span><span><span><br>/>"<span>)<span>;    
       
<span>}    <span>
//解密    
<span>$mw <span>= <span>bin2hex<span>(<span>$cipherText<span>)<span>;    
<span>$td <span>= <span>mcrypt_module_open<span>(<span>MCRYPT_RIJNDAEL_128<span>, <span>''<span>, <span>MCRYPT_MODE_ECB<span>, <span>''<span>)<span>;    
<span>if <span>(<span>mcrypt_generic_init<span>(<span>$td<span>, <span>$padkey<span>, <span>$iv<span>) <span>!<span>= <span>-<span>1<span>)    
<span>{    
    <span>$p_t <span>= <span>mdecrypt_generic<span>(<span>$td<span>, <span>hexToStr<span>(<span>$mw<span>)<span>)<span>;    
    <span>mcrypt_generic_deinit<span>(<span>$td<span>)<span>;    
    <span>mcrypt_module_close<span>(<span>$td<span>)<span>;    
       
    <span>$p_t <span>= <span>trimEnd<span>(<span>$p_t<span>)<span>;    
    <span>echo <span>'解密:'<span>;    
    <span>print<span>(<span>$p_t<span>)<span>;    
    <span>print<span>("<span><span><span><span><br>/>"<span>)<span>;    
    <span>print<span>(<span>bin2hex<span>(<span>$p_t<span>)<span>)<span>;    
    <span>echo '<span><span><span><span><br>><span><span><span><span><br>>'<span>;    
<span>}    <span>
//将$text补足$padlen倍数的长度    
<span>function <span>pad2Length<span>(<span>$text<span>, <span>$padlen<span>)<span>{    
    <span>$len <span>= <span>strlen<span>(<span>$text<span>)<span>%<span>$padlen<span>;    
    <span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template