Aes CBC加密
<?php
namespace app\components;
use yii;
class Aes
{
/**
* This was AES-128 / CBC / PKCS5Padding
* return base64_encode string
* @param string $plaintext
* @param string $key
* @return string
*/
public static function AesEncrypt($plaintext,$key = null)
{
$plaintext = trim($plaintext);
if ($plaintext == '') return '';
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); //PKCS5Padding
$padding = $size - strlen($plaintext) % $size;
// 添加Padding
$plaintext .= str_repeat(chr($padding), $padding); $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$key=self::substr($key, 0, mcrypt_enc_get_key_size($module));
$iv = $key;
// str_repeat($key, $size);
/* Intialize encryption */
mcrypt_generic_init($module, $key, $iv); /* Encrypt data */
$encrypted = mcrypt_generic($module, $plaintext);
/* Terminate encryption handler */
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
return base64_encode($encrypted);
}
/**
* Returns the length of the given string.
* If available uses the multibyte string function mb_strlen.
* @param string $string the string being measured for length
* @return integer the length of the string
*/
private static function strlen($string)
{
return extension_loaded('mbstring') ? mb_strlen($string,'8bit') : strlen($string);
} /**
* Returns the portion of string specified by the start and length parameters.
* If available uses the multibyte string function mb_substr
* @param string $string the input string. Must be one character or longer.
* @param integer $start the starting position
* @param integer $length the desired portion length
* @return string the extracted part of string, or FALSE on failure or an empty string.
*/
private static function substr($string,$start,$length)
{
return extension_loaded('mbstring') ? mb_substr($string,$start,$length,'8bit') : substr($string,$start,$length);
}
/**
* This was AES-128 / CBC / PKCS5Padding
* @param string $encrypted base64_encode encrypted string
* @param string $key
* @throws CException
* @return string
*/
public static function AesDecrypt($encrypted, $key = null)
{
if ($encrypted == '') return '';
$ciphertext_dec = base64_decode($encrypted);
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$key=self::substr($key, 0, mcrypt_enc_get_key_size($module));
$iv = $key;
// str_repeat($key, 16); //解密的初始化向量要和加密时一样。
/* Initialize encryption module for decryption */
mcrypt_generic_init($module, $key, $iv); /* Decrypt encrypted string */
$decrypted = mdecrypt_generic($module, $ciphertext_dec); /* Terminate decryption handle and close module */
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
// echo $decrypted;die;
//$a = rtrim($decrypted,"\0");
return rtrim($decrypted,"\0");
}
}
Aes CBC加密的更多相关文章
- JAVA AES CBC 加密 解密
AES 256 , KEY 的长度为 32字节(32*8=256bit). AES 128 , KEY 的长度为 16字节(16*8=128bit) CBC 模式需要IV, IV的值是固定写死,还是当 ...
- 【java工具】AES CBC加密
一.定义 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先 ...
- java PKCS7Padding 加密Cannot find any provider supporting AES/CBC/PKCS7Padding 解决办法
在java中用aes256进行加密,但是发现java里面不能使用PKCS7Padding,而java中自带的是PKCS5Padding填充,那解决办法是,通过BouncyCastle组件来让java里 ...
- AES在线加密解密-附AES128,192,256,CBC,CFB,ECB,OFB,PCBC各种加密解密源码
一.AES在线加密解密:AES 128/192/256位CBC/CFB/ECB/OFB/PCBC在线加密解密|在线工具|在线助手|在线生成|在线制作 http://www.it399.com/aes ...
- AES加密解密 助手类 CBC加密模式
"; string result1 = AESHelper.AesEncrypt(str); string result2 = AESHelper.AesDecrypt(result1); ...
- AES/CBC/PKCS7Padding加密方式
在网上找了大半天资料,终于找到一个可以用的 public static class AES { // 算法名称 final static String KEY_ALGORITHM = "AE ...
- linux上java解加密(AES/CBC)异常:java.lang.SecurityException: JCE cannot authenticate the provider BC办法
用mapreduce做数据清洗的时候,需要对数据进行解密,加密方法是:AES/CBC/PKCS7Padding,由于java本身不支持,需要添加依赖,用的依赖是: <dependency> ...
- php AES cbc模式 pkcs7 128位加密解密(微信小程序)
PHP AES CBC模式PKCS7 128位加密 加密: $key = '1234567812345678'; $iv = '1234567890123456'; $message = '12345 ...
- php实现AES/CBC/PKCS5Padding加密解密(又叫:对称加密)
今天在做一个和java程序接口的架接,java那边需要我这边(PHP)对传过去的值进行AES对称加密,接口返回的结果也是加密过的(就要用到解密),然后试了很多办法,也一一对应了AES的key密钥值,偏 ...
随机推荐
- 如何运行后台Service?
Unless you specify otherwise, most of the operations you do in an app run in the foreground on a spe ...
- [Code+#4]最短路 解题报告
Luogu · 传送门 Orz THU众大佬,lct(注意不是link-cut-tree,是一个大佬) 这道题很容易让人联想到 最短路,但是最短路需要先 建图: 暴力建出所有边的算法显然是不可行的,因 ...
- 运行make_datafiles的过程
1. 第一个bug 运行 echo "Please tokenize this text." | java edu.stanford.nlp.process.PTBTokenize ...
- mobile_1 物理像素
1 物理像素 需求: border: 1px solid red; 在移动端 dpr 为 2 的屏幕上,实际上是 2 物理像素. 如何实现 1 物理像素? 首先,肯定不能 border: 0.5 ...
- AJAX_违反了同源策略_就是"跨域"——jsonp 和 cors
https 协议 默认端口号 443 http 协议 默认端口号 80 同源策略 由网景公司提出的——浏览器 的 为了浏览器安全而生 同源策略: 协议.域名.端口号 必须完全一致 违 ...
- Jmeter遇到打不开的问题
1.JDK的版本,一定版本的jmeter需要特定版本以上的JDK支持,比如此次运行的apache-jmeter-2.12,就需要JDK1.6以上的版本支持.我原来装的是JDK1.5,配置好JMETER ...
- apt下载open-jdk8报错add-apt-repository: command not found
今天下载jdk8报错 在Ubuntu下,时不时会有这个错误的. add-apt-repository: command not found sudo apt-get install software- ...
- sql server2012远程连接用IP登陆进入设置步骤
第一步:把数据库上要设置的设置好. ↑这步是不是要,我也没弄清楚. 第二部是数据库的配置: 你的电脑图标,右击管理. 以上就是所有步骤. 再次登陆成功.
- 如何追踪产生大量REDO的来源
从10点到12点数据库中对象块变化排名靠前的对象 select to_char(begin_interval_time,'YYYY_MM_DD HH24:MI') snap_time, dhsso.o ...
- 转载--python模块
模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...