使用AES加密的帮助类
在开发中经常使用加密/解密对一些内容进行处理,比如密码在存入数据库之前先经过加密处理等等,这里就把一个加密帮助类代码贴出来,供以后查找使用。
这个帮助类主要功能是对字符串和字节数组进行加密解密处理。
public class EncryptionHelper
{
//默认密钥向量
private static readonly byte[] DefaultKey = {0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef}; /// <summary>
/// 解密字节数组
/// </summary>
/// <param name="cipherBytes">密文字节数组</param>
/// <param name="password">密钥</param>
/// <returns>解密后字节数组</returns>
public static byte[] DecryptBytes(byte[] cipherBytes, string password)
{
byte[] buffer;
try
{
using (Aes aes = new AesManaged())
{
//设置密钥及密钥向量
aes.Key = new Rfc2898DeriveBytes(password, DefaultKey).GetBytes(0x10);
aes.IV = aes.Key;
using (var memoryStream = new MemoryStream())
{
using (
var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(),
CryptoStreamMode.Write))
{
cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
cryptoStream.Flush();
}
//得到解密后的字节数组
buffer = memoryStream.ToArray();
}
}
}
catch
{
buffer = null;
}
return buffer;
} /// <summary>
/// 解密字符串
/// </summary>
/// <param name="cipherText">密文</param>
/// <param name="password">密钥</param>
/// <returns>解密后字符串</returns>
public static string DecryptString(string cipherText, string password)
{
byte[] decryptBytes = DecryptBytes(Convert.FromBase64String(cipherText), password);
if (decryptBytes == null)
{
return null;
}
return Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
} /// <summary>
/// 加密字节数组
/// </summary>
/// <param name="plainBytes">明文字节数组</param>
/// <param name="password">密钥</param>
/// <returns>加密后字节数组</returns>
public static byte[] EncryptBytes(byte[] plainBytes, string password)
{
byte[] buffer;
try
{
using (Aes aes = new AesManaged())
{
//设置密钥及密钥向量
aes.Key = new Rfc2898DeriveBytes(password, DefaultKey).GetBytes(0x10);
aes.IV = aes.Key;
using (var memoryStream = new MemoryStream())
{
using (
var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(),
CryptoStreamMode.Write))
{
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
cryptoStream.FlushFinalBlock();
}
//得到加密后的字节数组
buffer = memoryStream.ToArray();
}
}
}
catch
{
buffer = null;
}
return buffer;
} /// <summary>
/// 加密字符串
/// </summary>
/// <param name="plainText">明文</param>
/// <param name="password">密钥</param>
/// <returns>加密后字符串</returns>
public static string EncryptString(string plainText, string password)
{
byte[] cipherBytes = EncryptBytes(Encoding.UTF8.GetBytes(plainText), password);
if (cipherBytes == null)
{
return null;
}
return Convert.ToBase64String(cipherBytes);
}
}
使用AES加密的帮助类的更多相关文章
- AES加密解密 助手类 CBC加密模式
"; string result1 = AESHelper.AesEncrypt(str); string result2 = AESHelper.AesDecrypt(result1); ...
- AES加密解密工具类封装(AESUtil)
package club.codeapes.common.utils; import org.springframework.util.Base64Utils; import javax.crypto ...
- php的AES加密、解密类
<?php /** * php.ios.Android 通用的AES加密.解密方法 */ namespace Common\Business; class AESCrypt { /** * 初始 ...
- 自写AES加密解密工具类
此类主要用于加密与解密,采用128位ECB模式,PKCS5Padding填充补位. 可使用方法为加密返回二进制encryptBin(content, key).加密返回十六进制encryptHex(c ...
- 使用AES加密的勒索类软件分析报告
报告名称: 某勒索类软件分析报告 作者: 李东 报告更新日期: 样本发现日期: 样本类型: 样本文件大小/被感染文件变化长度: 样本文件MD5 校验值: da4ab5e31793 ...
- AES加密解密的例子小结
话不多说,先放上代码,一共有两个文件:AES.php(aes算法类文件)和aesDemo.php(应用实例文件),这里只贴出aesDemo.php,其他的看附件吧!aesDemo.php: 例子, ...
- 【Android工具】DES终结者加密时报——AES加密演算法
转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 在前面的两篇文章中.我们介绍了DES算法,3DES算法以及他们的Android程序实现,并研究了怎样才干实现 ...
- java独立小程序实现AES加密和解密
一.需求: web项目中配置文件配置的密码是明文的, 现在需要修改成密文, 加密方式采用AES, 于是写了个工具类用于加密和解密. 又因为这个密码是由客户来最终确定, 所以为了部署时方便起见, 写了个 ...
- php AES加密解密的例子
一共有两个文件:AES.php(aes算法类文件)和aesDemo.php(应用实例文件) aesDemo.php:例子, <?php require_once('./AES.php'); // ...
随机推荐
- query通用开源框架
Jquery通用开源框架之[ejq.js] 简介 ejq是一款非常小巧的JS工具库,未压缩才50K,在jquery的基础上对jquery缺失部分作了很好的弥补作用. 优点: 1.具有内置的模板解析引擎 ...
- information_schema.collations 学习
information_schema.collations 表中的每一行对应一个排序规则 1.information_schema.collations 表中学用列: 1.id :排序规则的ID 2. ...
- OS-MAC: An Efficient MAC Protocol for Spectrum-Agile Wireless Networks
IEEE TRANSACTIONS ON MOBILE COMPUTING, VOL. 7, NO. 8, AUGUST 2008 正如之前所说的,这是一个out-of-band local cove ...
- Ubuntu14.04LST安装weblogic11g
1:下载链接http://download.oracle.com/otn/nt/middleware/11g/wls/1036/wls1036_generic.jar 2:进行安装(前提已经安装好JD ...
- poj 1699 Best Sequence
http://poj.org/problem?id=1699 题意:给你n个长度为L的序列,求包含这几个序列的最短长度. 先预处理每两个序列之间的关系,然后dfs枚举就行. #include < ...
- js Array数组的使用
js Array数组的使用 Array是javascript中的一个事先定义好的对象(也可以称作一个类),可以直接使用 创建Array对象 var array=new Array(): 创建指定元 ...
- xampp安装时mysql报错
问题描述:以前安装过mysql,后来安装xampp,mysql打不开,出错提示16:04:48 [mysql] MySQL Service detected with wrong path16:0 ...
- C语言的本质(38)——makefile之变量
我们详细看看Makefile中关于变量的语法规则.先看一个简单的例子: foo = $(bar) bar = Huh? all: @echo$(foo) 我们执行make将会打出Huh?.当make读 ...
- AngularJS中文介绍
简介 AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了.所以我做了一些工作(你也可以觉得是小 ...
- C语言中不同类型的循环(Different types of loops in C)
C语言中有三种类型的循环:for,while,do-while. while循环先判断循环条件. while (condition) { //gets executed after condition ...