<?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加密的更多相关文章

  1. JAVA AES CBC 加密 解密

    AES 256 , KEY 的长度为 32字节(32*8=256bit). AES 128 , KEY 的长度为 16字节(16*8=128bit) CBC 模式需要IV, IV的值是固定写死,还是当 ...

  2. 【java工具】AES CBC加密

    一.定义 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先 ...

  3. java PKCS7Padding 加密Cannot find any provider supporting AES/CBC/PKCS7Padding 解决办法

    在java中用aes256进行加密,但是发现java里面不能使用PKCS7Padding,而java中自带的是PKCS5Padding填充,那解决办法是,通过BouncyCastle组件来让java里 ...

  4. AES在线加密解密-附AES128,192,256,CBC,CFB,ECB,OFB,PCBC各种加密解密源码

    一.AES在线加密解密:AES 128/192/256位CBC/CFB/ECB/OFB/PCBC在线加密解密|在线工具|在线助手|在线生成|在线制作 http://www.it399.com/aes ...

  5. AES加密解密 助手类 CBC加密模式

    "; string result1 = AESHelper.AesEncrypt(str); string result2 = AESHelper.AesDecrypt(result1); ...

  6. AES/CBC/PKCS7Padding加密方式

    在网上找了大半天资料,终于找到一个可以用的 public static class AES { // 算法名称 final static String KEY_ALGORITHM = "AE ...

  7. linux上java解加密(AES/CBC)异常:java.lang.SecurityException: JCE cannot authenticate the provider BC办法

    用mapreduce做数据清洗的时候,需要对数据进行解密,加密方法是:AES/CBC/PKCS7Padding,由于java本身不支持,需要添加依赖,用的依赖是: <dependency> ...

  8. php AES cbc模式 pkcs7 128位加密解密(微信小程序)

    PHP AES CBC模式PKCS7 128位加密 加密: $key = '1234567812345678'; $iv = '1234567890123456'; $message = '12345 ...

  9. php实现AES/CBC/PKCS5Padding加密解密(又叫:对称加密)

    今天在做一个和java程序接口的架接,java那边需要我这边(PHP)对传过去的值进行AES对称加密,接口返回的结果也是加密过的(就要用到解密),然后试了很多办法,也一一对应了AES的key密钥值,偏 ...

随机推荐

  1. XVIII Open Cup named after E.V. Pankratiev. Ukrainian Grand Prix

    A. Accommodation Plan 对于已知的$K$个点,离它们距离都不超过$L$的点在树上是一个连通块,考虑在每种方案对应的离$1$最近的点统计. 即对于每个点$x$,统计离它距离不超过$L ...

  2. Django中Q搜索的简单应用

    本节涉及: 1.Q搜索在前后端的设计 2.Django中Queryset对象的序列化(由后端扔给前端的数据必然会经过序列化) 3.前端动态地构造表格以便显示(动态创建DOM对象) 思路: 用户通过前端 ...

  3. webpack3_脚手架

    webpack    记得 --save-dev 装入开发依赖 更新迭代快,需要有根据报错解决问题的能力,来融会贯通这个工具 这里的是 webpack3,其实已经到了 webpack4 了 采用了 w ...

  4. java--List、Set、Map的基础

    好像面试很多面试官都喜欢问这它们的一些问题,所以在这里我稍微总结一下,并把大佬们的文章链接贴在后面. 首先我们借鉴了https://www.cnblogs.com/SnowingYXY/p/67273 ...

  5. 【Python基础】lpthw - Exercise 40 模块、类和对象

    一. 模块(module) 模块中包含一些函数和变量,在其他程序中使用该模块的内容时,需要先将模块import进去,再使用.操作符获取函数或变量,如 # This goes in mystuff.py ...

  6. sublime 将tab替换为4个空格 & 显示空格

    preferences -> settings -> 在右侧的json中加入(左侧的默认配置是无法修改的,可以在默认配置中搜到这几个配置) // The number of spaces ...

  7. linux1

    虚拟内存:内核通过磁盘上的存储空间来实现虚拟内存,这块区域称为交换空间.内核不断交换空间和实际的物理内存之间反复交换虚拟内存中的内容 linux运行中的程序叫做进程. 内核创建了第一个进程,叫做Ini ...

  8. Apache Arrow

    https://www.kdnuggets.com/2017/02/apache-arrow-parquet-columnar-data.html https://arrow.apache.org/ ...

  9. 实验八 Web基础 SQL注入原理

    实验八 Web基础 实验要求 (1)Web前端HTML 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML. (2)Web前端javascipt ...

  10. eclemma怎么安装 eclemma的安装与简单使用图文教程(附下载)

    来自于:https://www.jb51.net/softjc/628026.html 一. 安装 有两种安装方法 1. 下载安装(推荐) 地址: http://sourceforge.net/pro ...