我们常用的md5算法从理论上来讲是不可逆的,但是有强大的方法还是可以把md5给算出来,只是根据复杂程度需要的时间不同罢了,但有时我们希望自己数据加密传输之后接受可以解密了,下面我来给大家提供一个函数。
对于大部分密码加密,我们可以采用md5、sha1等方法。可以有效防止数据泄露,但是这些方法仅适用于无需还原的数据加密。
对于需要还原的信息,则需要采用可逆的加密解密算法。
下面一组PHP函数是实现此加密解密的方法:
加密算法如下:
代码如下 | 复制代码 |
function encrypt($data, $key) { $key = md5($key); $x = 0; $len = strlen($data); $l = strlen($key); for ($i = 0; $i { if ($x == $l) { $x = 0; } $char .= $key{$x}; $x++; } for ($i = 0; $i { $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256); } return base64_encode($str); } |
解密算法如下:
代码如下 | 复制代码 |
function decrypt($data, $key) { $key = md5($key); $x = 0; $data = base64_decode($data); $len = strlen($data); $l = strlen($key); for ($i = 0; $i { if ($x == $l) { $x = 0; } $char .= substr($key, $x, 1); $x++; } for ($i = 0; $i { if (ord(substr($data, $i, 1)) { $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1))); } else { $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1))); } } return $str; } |
上述加密解密的过程均需要用到一个加密密钥(即参数$key)。
代码如下 | 复制代码 |
$data = 'PHP加密解密算法'; // 被加密信息 |
上述将输出类似如下结果:
gniCSOzZG+HnS9zcFea7SefNGhXF
PHP加密解密算法
从上述结果可以看出,这是一组可逆的加密解密算法,可以用于部分需要还原的数据加密。

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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