展会信息港展会大全

php实现aes加密类分享 android软件开发教程
来源:互联网   发布日期:2016-03-01 14:02:28   浏览:1691次  

导读:这篇文章主要介绍了php实现的aes加密类,代码中有使用方法,需要的朋友可以参考下代码如下:<?phpclass AESMcrypt {public $iv = null;...

这篇文章主要介绍了php实现的aes加密类,代码中有使用方法,需要的朋友可以参考下

代码如下:

<?php

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), "x00..x1F");

return $data;

}

}

//使用方法

$aes = new AESMcrypt($bit = 128, $key = 'abcdef1234567890', $iv = '0987654321fedcba', $mode = 'cbc');

$c = $aes->encrypt('haowei.me');

var_dump($aes->decrypt($c));

赞助本站

人工智能实验室

相关热词: 开发 编程 android

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港