<?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. 运行make_datafiles的过程

    1. 第一个bug 运行 echo "Please tokenize this text." | java edu.stanford.nlp.process.PTBTokenize ...

  2. Linux内核笔记:epoll实现原理(一)

    一.说明 针对的内核版本为4.4.10. 本文只是我自己看源码的简单笔记,如果想了解epoll的实现,强烈推荐下面的文章: The Implementation of epoll(1) The Imp ...

  3. Codeforces Round #524 (Div. 2)

    A. Petya and Origamitime limit per test1 secondmemory limit per test256 megabytesinputstandard input ...

  4. 关于word粘贴图片无法显示的原因

    今天在进行word文档粘贴图片编辑操作的时候,老是无法完整的显示图片.或者干脆就不显示图片,以为是qq截图的图片格式无法粘贴到word文档里面,用Photoshop更改图片的格式仍然无法显示(将png ...

  5. 为Vue.js添加友好日志

    const isDebugEnabled = process.env.NODE_ENV !== "production"; const isInfoEnabled = true; ...

  6. Spring cloud 之Ribbon(一)基本使用

    简介 Spring cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它是基于Netflix的Riboon实现的.Ribbon是客户端负载均衡器,这有别语例如Nginx服务端负载 ...

  7. 2018年年度总结 & 2019年计划

      2018关键词 「探索」 引用以前作文最爱写的开头,时间如白驹过隙,回想上次写17年年度总结,仿佛也就过了几日光景.   首先回顾一下17年定下的目标, 18年我将关键字设为探索,目的有两个,一是 ...

  8. Linux服务器在SSH客户端如何实现免密登录

    一.SSH客户端Setting 配置 key ,  创建生成公钥导出文件. 二.服务器 master 上生成密钥 通过执行命令 ssh-keygen -t rsa 来生成我们需要的密钥. ssh-ke ...

  9. monent API详解

    参考链接:https://www.jianshu.com/p/9c10543420de

  10. 【Pattern】-NO.150.Pattern.1 -【Pattern UML】

    Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...