php对称加密算法示例
这篇文章主要介绍了php对称加密算法示例,需要的朋友可以参考下
php对称加密算法
KEY 是之前定义的常量
复制代码 代码如下:
Mcrypt::encrypt();
Mcrypt::decrypt();
复制代码 代码如下:
defined('ROOT') or exit('Access Denied');
class Mcrypt{
public static function encrypt($code){
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5(KEY), $code, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
}
public static function decrypt($code){
return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5(KEY), base64_decode($code), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND));
}
}
常用对称加密算法(DES/AES)类
xcrypt.php
复制代码 代码如下:
/**
* 常用对称加密算法类
* 支持密钥:64/128/256 bit(字节长度8/16/32)
* 支持算法:DES/AES(根据密钥长度自动匹配使用:DES:64bit AES:128/256bit)
* 支持模式:CBC/ECB/OFB/CFB
* 密文编码:base64字符串/十六进制字符串/二进制字符串流
* 填充方式: PKCS5Padding(DES)
*
* @author: linvo
* @version: 1.0.0
* @date: 2013/1/10
*/
class Xcrypt{
private $mcrypt;
private $key;
private $mode;
private $iv;
private $blocksize;
/**
* 构造函数
*
* @param string 密钥
* @param string 模式
* @param string 向量("off":不使用 / "auto":自动 / 其他:指定值,,长度同密钥)
*/
public function __construct($key, $mode = 'cbc', $iv = "off"){
switch (strlen($key)){
case 8:
$this->mcrypt = MCRYPT_DES;
break;
case 16:
$this->mcrypt = MCRYPT_RIJNDAEL_128;
break;
case 32:
$this->mcrypt = MCRYPT_RIJNDAEL_256;
break;
default:
die("Key size must be 8/16/32");
}
$this->key = $key;
switch (strtolower($mode)){
case 'ofb':
$this->mode = MCRYPT_MODE_OFB;
if ($iv == 'off') die('OFB must give a IV'); //OFB必须有向量
break;
case 'cfb':
$this->mode = MCRYPT_MODE_CFB;
if ($iv == 'off') die('CFB must give a IV'); //CFB必须有向量
break;
case 'ecb':
$this->mode = MCRYPT_MODE_ECB;
$iv = 'off'; //ECB不需要向量
break;
case 'cbc':
default:
$this->mode = MCRYPT_MODE_CBC;
}
switch (strtolower($iv)){
case "off":
$this->iv = null;
break;
case "auto":
$source = PHP_OS=='WINNT' ? MCRYPT_RAND : MCRYPT_DEV_RANDOM;
$this->iv = mcrypt_create_iv(mcrypt_get_block_size($this->mcrypt, $this->mode), $source);
break;
default:
$this->iv = $iv;
}
}
/**
* 获取向量值
* @param string 向量值编码(base64/hex/bin)
* @return string 向量值
*/
public function getIV($code = 'base64'){
switch ($code){
case 'base64':
$ret = base64_encode($this->iv);
break;
case 'hex':
$ret = bin2hex($this->iv);
break;
case 'bin':
default:
$ret = $this->iv;
}
return $ret;
}
/**
* 加密
* @param string 明文
* @param string 密文编码(base64/hex/bin)
* @return string 密文
*/
public function encrypt($str, $code = 'base64'){
if ($this->mcrypt == MCRYPT_DES) $str = $this->_pkcs5Pad($str);
if (isset($this->iv)) {
$result = mcrypt_encrypt($this->mcrypt, $this->key, $str, $this->mode, $this->iv);
} else {
@$result = mcrypt_encrypt($this->mcrypt, $this->key, $str, $this->mode);
}
switch ($code){
case 'base64':
$ret = base64_encode($result);
break;
case 'hex':
$ret = bin2hex($result);
break;
case 'bin':
default:
$ret = $result;
}
return $ret;
}
/**
* 解密
* @param string 密文
* @param string 密文编码(base64/hex/bin)
* @return string 明文
*/
public function decrypt($str, $code = "base64"){
$ret = false;
switch ($code){
case 'base64':
$str = base64_decode($str);
break;
case 'hex':
$str = $this->_hex2bin($str);
break;
case 'bin':
default:
}
if ($str !== false){
if (isset($this->iv)) {
$ret = mcrypt_decrypt($this->mcrypt, $this->key, $str, $this->mode, $this->iv);
} else {
@$ret = mcrypt_decrypt($this->mcrypt, $this->key, $str, $this->mode);
}
if ($this->mcrypt == MCRYPT_DES) $ret = $this->_pkcs5Unpad($ret);
$ret = trim($ret);
}
return $ret;
}
private function _pkcs5Pad($text){
$this->blocksize = mcrypt_get_block_size($this->mcrypt, $this->mode);
$pad = $this->blocksize - (strlen($text) % $this->blocksize);
return $text . str_repeat(chr($pad), $pad);
}
private function _pkcs5Unpad($text){
$pad = ord($text{strlen($text) - 1});
if ($pad > strlen($text)) return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
$ret = substr($text, 0, -1 * $pad);
return $ret;
}
private function _hex2bin($hex = false){
$ret = $hex !== false && preg_match('/^[0-9a-fA-F]+$/i', $hex) ? pack("H*", $hex) : false;
return $ret;
}
}
上面类的使用方法
复制代码 代码如下:

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
