php中AES加密解密的例子小结_PHP
aesDemo.php:
例子,
复制代码 代码如下:
require_once('./AES.php');
//$aes = new AES();
$aes = new AES(true);// 把加密后的字符串按十六进制进行存储
//$aes = new AES(true,true);// 带有调试信息且加密字符串按十六进制存储
$key = "this is a 32 byte key";// 密钥
$keys = $aes->makeKey($key);
$encode = "123456";// 被加密的字符串
$ct = $aes->encryptString($encode, $keys);
echo "encode = ".$ct."
";
$cpt = $aes->decryptString($ct, $keys);
echo "decode = ".$cpt;
?>
例子、AES加密类
复制代码 代码如下:
//php aes加密类
class AESMcrypt {
public $iv = null;
public $key = null;
public $bit = 128;
private $cipher;
public function __construct($bit, $key, $iv, $mode) {
if(empty($bit) || empty($key) || empty($iv) || empty($mode))
return NULL;
$this->bit = $bit;
$this->key = $key;
$this->iv = $iv;
$this->mode = $mode;
switch($this->bit) {
case 192:$this->cipher = MCRYPT_RIJNDAEL_192; break;
case 256:$this->cipher = MCRYPT_RIJNDAEL_256; break;
default: $this->cipher = MCRYPT_RIJNDAEL_128;
}
switch($this->mode) {
case 'ecb':$this->mode = MCRYPT_MODE_ECB; break;
case 'cfb':$this->mode = MCRYPT_MODE_CFB; break;
case 'ofb':$this->mode = MCRYPT_MODE_OFB; break;
case 'nofb':$this->mode = MCRYPT_MODE_NOFB; break;
default: $this->mode = MCRYPT_MODE_CBC;
}
}
public function encrypt($data) {
$data = base64_encode(mcrypt_encrypt( $this->cipher, $this->key, $data, $this->mode, $this->iv));
return $data;
}
public function decrypt($data) {
$data = mcrypt_decrypt( $this->cipher, $this->key, base64_decode($data), $this->mode, $this->iv);
$data = rtrim(rtrim($data), "..");
return $data;
}
}
//使用方法
$aes = new AESMcrypt($bit = 128, $key = 'abcdef1234567890', $iv = '0987654321fedcba', $mode = 'cbc');
$c = $aes->encrypt('haowei.me');
var_dump($aes->decrypt($c));
例子、附一个可加密可解密类
复制代码 代码如下:
/**
* AES加密、解密类
* @author hushangming
*
* 用法:
*
<br> * // 实例化类<br> * // 参数$_bit:格式,支持256、192、128,默认为128字节的<br> * // 参数$_type:加密/解密方式,支持cfb、cbc、nofb、ofb、stream、ecb,默认为ecb<br> * // 参数$_key:密钥,默认为abcdefghijuklmno<br> * $tcaes = new TCAES(); <br> * $string = 'laohu';<br> * // 加密<br> * $encodeString = $tcaes->encode($string);<br> * // 解密<br> * $decodeString = $tcaes->decode($encodeString);<br> *
*/
class TCAES{
private $_bit = MCRYPT_RIJNDAEL_256;
private $_type = MCRYPT_MODE_CBC;
//private $_key = 'abcdefghijuklmno0123456789012345';
private $_key = 'abcdefghijuklmno'; // 密钥
private $_use_base64 = true;
private $_iv_size = null;
private $_iv = null;
/**
* @param string $_key 密钥
* @param int $_bit 默认使用128字节
* @param string $_type 加密解密方式
* @param boolean $_use_base64 默认使用base64二次加密
*/
public function __construct($_key = '', $_bit = 128, $_type = 'ecb', $_use_base64 = true){
// 加密字节
if(192 === $_bit){
$this->_bit = MCRYPT_RIJNDAEL_192;
}elseif(128 === $_bit){
$this->_bit = MCRYPT_RIJNDAEL_128;
}else{
$this->_bit = MCRYPT_RIJNDAEL_256;
}
// 加密方法
if('cfb' === $_type){
$this->_type = MCRYPT_MODE_CFB;
}elseif('cbc' === $_type){
$this->_type = MCRYPT_MODE_CBC;
}elseif('nofb' === $_type){
$this->_type = MCRYPT_MODE_NOFB;
}elseif('ofb' === $_type){
$this->_type = MCRYPT_MODE_OFB;
}elseif('stream' === $_type){
$this->_type = MCRYPT_MODE_STREAM;
}else{
$this->_type = MCRYPT_MODE_ECB;
}
// 密钥
if(!empty($_key)){
$this->_key = $_key;
}
// 是否使用base64
$this->_use_base64 = $_use_base64;
$this->_iv_size = mcrypt_get_iv_size($this->_bit, $this->_type);
$this->_iv = mcrypt_create_iv($this->_iv_size, MCRYPT_RAND);
}
/**
* 加密
* @param string $string 待加密字符串
* @return string
*/
public function encode($string){
if(MCRYPT_MODE_ECB === $this->_type){
$encodeString = mcrypt_encrypt($this->_bit, $this->_key, $string, $this->_type);
}else{
$encodeString = mcrypt_encrypt($this->_bit, $this->_key, $string, $this->_type, $this->_iv);
}
if($this->_use_base64)
$encodeString = base64_encode($encodeString);
return $encodeString;
}
/**
* 解密
* @param string $string 待解密字符串
* @return string
*/
public function decode($string){
if($this->_use_base64)
$string = base64_decode($string);
$string = $this->toHexString($string);
if(MCRYPT_MODE_ECB === $this->_type){
$decodeString = mcrypt_decrypt($this->_bit, $this->_key, $string, $this->_type);
}else{
$decodeString = mcrypt_decrypt($this->_bit, $this->_key, $string, $this->_type, $this->_iv);
}
return $decodeString;
}
/**
* 将$string转换成十六进制
* @param string $string
* @return stream
*/
private function toHexString ($string){
$buf = "";
for ($i = 0; $i $val = dechex(ord($string{$i}));
if(strlen($val) $val = "0".$val;
$buf .= $val;
}
return $buf;
}
/**
* 将十六进制流$string转换成字符串
* @param stream $string
* @return string
*/
private function fromHexString($string){
$buf = "";
for($i = 0; $i $val = chr(hexdec(substr($string, $i, 2)));
$buf .= $val;
}
return $buf;
}
}

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

AI Hentai Generator
Generate AI Hentai for free.

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

Decrypting HTTP status code 460: Why does this error occur? Introduction: In daily network use, we often encounter various error prompts, including HTTP status codes. These status codes are a mechanism defined by the HTTP protocol to indicate the processing of a request. Among these status codes, there is a relatively rare error code, namely 460. This article will delve into this error code and explain why this error occurs. Definition of HTTP status code 460: First, we need to understand the basics of HTTP status code

In Apple mobile phones, users can encrypt photo albums according to their own needs. Some users don't know how to set it up. You can add the pictures that need to be encrypted to the memo, and then lock the memo. Next, the editor will introduce the method of setting up the encryption of mobile photo albums for users. Interested users, come and take a look! Apple mobile phone tutorial How to set up iPhone photo album encryption A: After adding the pictures that need to be encrypted to the memo, go to lock the memo for detailed introduction: 1. Enter the photo album, select the picture that needs to be encrypted, and then click [Add to] below. 2. Select [Add to Notes]. 3. Enter the memo, find the memo you just created, enter it, and click the [Send] icon in the upper right corner. 4. Click [Lock Device] below

Folder encryption is a common data protection method that encrypts the contents of a folder so that only those who have the decryption password can access the files. When encrypting a folder, there are some common ways to set a password without compressing the file. First, we can use the encryption function that comes with the operating system to set a folder password. For Windows users, you can set it up by following the following steps: Select the folder to be encrypted, right-click the folder, and select "Properties"

In today's work environment, everyone's awareness of confidentiality is getting stronger and stronger, and encryption operations are often performed to protect files when using software. Especially for key documents, the awareness of confidentiality should be increased, and the security of documents should be given top priority at all times. So I don’t know how well everyone understands word decryption. How to operate it specifically? Today we will actually show you the process of word decryption through the explanation below. Friends who need to learn word decryption knowledge should not miss today's course. A decryption operation is first required to protect the file, which means that the file is processed as a protective document. After doing this to a file, a prompt pops up when you open the file again. The way to decrypt the file is to enter the password, so you can directly

The editor will introduce to you three methods of encryption and compression: Method 1: Encryption The simplest encryption method is to enter the password you want to set when encrypting the file, and the encryption and compression are completed. Method 2: Automatic encryption Ordinary encryption method requires us to enter a password when encrypting each file. If you want to encrypt a large number of compressed packages and the passwords are the same, then we can set automatic encryption in WinRAR, and then just When compressing files normally, WinRAR will add a password to each compressed package. The method is as follows: Open WinRAR, click Options-Settings in the setting interface, switch to [Compression], click Create Default Configuration-Set Password Enter the password we want to set here, click OK to complete the setting, we only need to correct

Decrypting the tricks added by the PyCharm interpreter PyCharm is the integrated development environment (IDE) preferred by many Python developers, and it provides many powerful features to improve development efficiency. Among them, the setting of the interpreter is an important part of PyCharm. Correctly setting the interpreter can help developers run the code smoothly and debug the program. This article will introduce some techniques for decrypting the PyCharm interpreter additions, and combine it with specific code examples to show how to correctly configure the interpreter. Adding and selecting interpreters in Py

Original author: Meteor, ChainCatcher Original editor: Marco, ChainCatcher Recently, the full-chain interoperability protocol Analog has entered the public eye with the disclosure of US$16 million in financing. Investment institutions include TribeCapital, NGCVentures, Wintermute, GSR, NEAR, OrangeDAO, and Mike Novogratz’s Alternative asset management companies Samara Asset Group, Balaji Srinivasan, etc. At the end of 2023, Analog caused some excitement in the industry. They released information on the open testnet registration event on the X platform.

How to de-encrypt videos on EZVIZ Cloud: There are many ways to de-encrypt videos on EZVIZ Cloud, one of which is by using the EZVIZ Cloud Mobile App. Users only need to enter the device list, select the camera to be decrypted and enter the device details page. On the device details page, find the "Settings" option, and then select "Video Encryption" to make relevant settings. In the video encryption settings interface, you can choose the option to turn off video encryption, and save the settings to complete the decryption operation. This simple step allows users to easily decrypt videos and improves the convenience of using the camera. If you use the computer client of EZVIZ Cloud, you can also cancel video encryption through similar steps. Just log in and select the corresponding camera, enter the device details interface, and then look for video addition in the settings.
