php AES 加密类
<?php
class CryptAES
{
protected $cipher = MCRYPT_RIJNDAEL_128;
protected $mode = MCRYPT_MODE_ECB;
protected $pad_method = NULL;
protected $secret_key = '';
protected $iv = ''; public function set_cipher($cipher)
{
$this->cipher = $cipher;
} public function set_mode($mode)
{
$this->mode = $mode;
} public function set_iv($iv)
{
$this->iv = $iv;
} public function set_key($key)
{
$this->secret_key = $key;
} public function require_pkcs5()
{
$this->pad_method = 'pkcs5';
} protected function pad_or_unpad($str, $ext)
{
if ( is_null($this->pad_method) )
{
return $str;
}
else
{
$func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad';
if ( is_callable($func_name) )
{
$size = mcrypt_get_block_size($this->cipher, $this->mode);
return call_user_func($func_name, $str, $size);
}
}
return $str;
} protected function pad($str)
{
return $this->pad_or_unpad($str, '');
} protected function unpad($str)
{
return $this->pad_or_unpad($str, 'un');
} public function encrypt($str)
{
$str = $this->pad($str);
$td = mcrypt_module_open($this->cipher, '', $this->mode, ''); if ( empty($this->iv) )
{
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
}
else
{
$iv = $this->iv;
} mcrypt_generic_init($td, $this->secret_key, $iv);
$cyper_text = mcrypt_generic($td, $str);
//$rt=base64_encode($cyper_text);
$rt = bin2hex($cyper_text);
mcrypt_generic_deinit($td);
mcrypt_module_close($td); return $rt;
} public function decrypt($str){
$td = mcrypt_module_open($this->cipher, '', $this->mode, ''); if ( empty($this->iv) )
{
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
}
else
{
$iv = $this->iv;
} mcrypt_generic_init($td, $this->secret_key, $iv);
$decrypted_text = mdecrypt_generic($td, self::hex2bin($str));
//$decrypted_text = mdecrypt_generic($td, base64_decode($str));
$rt = $decrypted_text;
mcrypt_generic_deinit($td);
mcrypt_module_close($td); return $this->unpad($rt);
} public static function hex2bin($hexdata) {
$bindata = '';
$length = strlen($hexdata);
for ($i=0; $i < $length; $i += 2)
{
$bindata .= chr(hexdec(substr($hexdata, $i, 2)));
}
return $bindata;
} public static function pkcs5_pad($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
} public static function pkcs5_unpad($text)
{
$pad = ord($text{strlen($text) - 1});
if ($pad > strlen($text)) return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
return substr($text, 0, -1 * $pad);
}
}
?>
require_once("CryptAES.class.php");
$keyStr = 'ss4fs4skfhksk';
$aes = new CryptAES();
$keyStr = $aes->hex2bin($keyStr);
$aes->set_key($keyStr);
$aes->require_pkcs5();
$d = $aes->encrypt($data);
php AES 加密类的更多相关文章
- PHP的AES加密类
PHP的AES加密类 aes.php <?php /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ...
- php实现aes加密类
php实现的aes加密类,代码中有使用方法. <?php //php aes加密类 class AESMcrypt { public $iv = null; public $key = null ...
- AES加密类
代码: using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace ...
- AES加密解密的例子小结
话不多说,先放上代码,一共有两个文件:AES.php(aes算法类文件)和aesDemo.php(应用实例文件),这里只贴出aesDemo.php,其他的看附件吧!aesDemo.php: 例子, ...
- php AES加密解密的例子
一共有两个文件:AES.php(aes算法类文件)和aesDemo.php(应用实例文件) aesDemo.php:例子, <?php require_once('./AES.php'); // ...
- [PHP]AES加密----PHP服务端和Android客户端
本文采取128位AES-CBC模式加密和解密 1.首先对服务端安装mcrypt: sudo apt-get install php5-mcrypt php5-dev sudo php5enmod mc ...
- AES加密解密 助手类 CBC加密模式
"; string result1 = AESHelper.AesEncrypt(str); string result2 = AESHelper.AesDecrypt(result1); ...
- Java 关于密码处理的工具类[MD5编码][AES加密/解密]
项目中又遇到了加密问题,又去翻了半天,然后做测试,干脆就把常用的两类小结一下. 1.第一种所谓的MD5加密 其实也不算加密,只是基于Hash算法的不可逆编码而已,等于说,一旦经过MD5处理,是不可能从 ...
- Java AES 加密工具类
package com.microwisdom.utils; import java.security.NoSuchAlgorithmException; import java.security.S ...
随机推荐
- cisco 密码重置
密码重置 分类: 转贴技术资料 2007-12-28 16:38 http://www.cisco.com/en/US/products/hw/routers/ps259/products_passw ...
- json optString getString
optString 和 getString 区别. optString 当接收到的为空时候 不会报错
- zf-关于被发牌人没有显示环节的那个被发牌人的解决办法
是存储过程里的字段没有插入进去,添加个presonName即可--修改的时候可以执行 dbo.dingshi_fapai 来进行存储 如果添加presonName 必须在临时表里加上这个字段,然后在进 ...
- oracle sql 分页
Oracle实现分页时,需要引入一个rownum的函数,rownum可以给记录一个类似于id的字段. 以下收整理了常用的几种sql分页算法,数据库以Oracle中emp为例.查询结果如下: SQL&g ...
- android脚步---UI界面修改,增加按钮和监听
我的UU界面,其布局如下: 需要修改的部分: 意见反馈居中,还有增加backbutton 首先在mainactivity中找到我的UU的定义:dialogue public void showAbou ...
- (转)mahout中k-means例子的运行
首先简单说明下,mahout下处理的文件必须是SequenceFile格式的,所以需要把txtfile转换成sequenceFile.SequenceFile是hadoop中的一个类,允 ...
- Android--->Button按钮操作
main.xml中设置两个按钮 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xm ...
- 签名“未签名”apk文件命令
在发布至360.oppo应用市场时分别遇到了需要"应用加固"和"应用认领"的情况, 流程都是需要下载一个未签名的apk文件(安装包),然后签名后再上传. 我的做 ...
- Linux批量部署工具Expect
既然没有遇到过,做好准备总是好的.这是自己送给自己的话,现在运维做自动话越来越多,自己就学以下,记录笔记.目前主流的有puppet.Expect.pssh等等,今天就用Expect做自动部署和日常管理 ...
- JAVA基础--toString, equals方法
==比较的是地址 equals比较的是内容. 所以要重写object的equals方法. public class TestEquals { public static void main(Strin ...