search
Homephp教程php手册深入理解PHP中mcrypt的可逆加密算法

数据加密在我们生活中的地位已经越来越重要了,尤其是考虑到在网络上发生的大量交易和传输的大量数据。对于不需要还原为原始数据的信息我们可以使用MD5、sha1等不可逆加密算法对数据进行加密处理,但对于交易信息等需要还原为原始数据的重要信息则必须使用可还原的加密算法进行加密了。当然你可以自己写一个可逆加密的算法进行加密和解密计算。本文中我们介绍的是使用 mcrypt模块进行加密解密操作。 

Mcrypt的优点不仅仅在于其提供的加密算法较多,在windows下随PHP包一起发布,还在于它可以对数据进行加/解密处理,此外,它还提供了包括DES算法在内的35种处理数据用的函数。

代码如下:

/** 
+----------------------------------------------------- 
* Mcrypt 加密/解密 
* @param String $date 要加密和解密的数据 
* @param String $mode encode 默认为加密/decode 为解密 
* @return String 
* @author zxing@97md.net Mon Sep 14 22:59:28 CST 2009 
+----------------------------------------------------- 
* @example 
*/ 
function ZxingCrypt($date,$mode = 'encode'){ 
$key = md5('zxing');//用MD5哈希生成一个密钥,注意加密和解密的密钥必须统一 
if ($mode == 'decode'){ 
$date = base64_decode($date); 
} 
if (function_exists('mcrypt_create_iv')){ 
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); 
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); 
} 
if (isset($iv) && $mode == 'encode'){ 
$passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $date, MCRYPT_MODE_ECB, $iv); 
}elseif (isset($iv) && $mode == 'decode'){ 
$passcrypt = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $date, MCRYPT_MODE_ECB, $iv); 
} 
if ($mode == 'encode'){ 
$passcrypt = base64_encode($passcrypt); 
} 
return $passcrypt; 
}

别的网友的代码

代码如下:

<?php 
$td = mcrypt_module_open(MCRYPT_DES,&#39;&#39;,&#39;ecb&#39;,&#39;&#39;); //使用MCRYPT_DES算法,ecb模式 
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); 
$ks = mcrypt_enc_get_key_size($td); 
$key = "ery secret key";//密钥 
$key = substr(md5($key), 0, $ks); 
mcrypt_generic_init($td, $key, $iv); //初始处理 
//加密 
$encrypted = mcrypt_generic($td, &#39;This is very important data&#39;); 
//结束处理 
mcrypt_generic_deinit($td); 
//初始解密处理 
mcrypt_generic_init($td, $key, $iv); 
//解密 
$decrypted = mdecrypt_generic($td, $encrypted); 
//结束 
mcrypt_generic_deinit($td); 
mcrypt_module_close($td); 
//解密后,可能会有后续的\0,需去掉 
echo trim($decrypted) . "\n"; 
?>

【相关教程推荐】

1. php编程从入门到精通全套视频教程 

2. php从入门到精通  

3. bootstrap教程 

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

mPDF

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),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

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.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor