在开发中经常使用加密/解密对一些内容进行处理,比如密码在存入数据库之前先经过加密处理等等,这里就把一个加密帮助类代码贴出来,供以后查找使用。

这个帮助类主要功能是对字符串和字节数组进行加密解密处理。

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

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

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

  2. AES加密解密工具类封装(AESUtil)

    package club.codeapes.common.utils; import org.springframework.util.Base64Utils; import javax.crypto ...

  3. php的AES加密、解密类

    <?php /** * php.ios.Android 通用的AES加密.解密方法 */ namespace Common\Business; class AESCrypt { /** * 初始 ...

  4. 自写AES加密解密工具类

    此类主要用于加密与解密,采用128位ECB模式,PKCS5Padding填充补位. 可使用方法为加密返回二进制encryptBin(content, key).加密返回十六进制encryptHex(c ...

  5. 使用AES加密的勒索类软件分析报告

    报告名称:  某勒索类软件分析报告    作者:        李东 报告更新日期: 样本发现日期: 样本类型: 样本文件大小/被感染文件变化长度: 样本文件MD5 校验值: da4ab5e31793 ...

  6. AES加密解密的例子小结

    话不多说,先放上代码,一共有两个文件:AES.php(aes算法类文件)和aesDemo.php(应用实例文件),这里只贴出aesDemo.php,其他的看附件吧!aesDemo.php: 例子,   ...

  7. 【Android工具】DES终结者加密时报——AES加密演算法

    转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 在前面的两篇文章中.我们介绍了DES算法,3DES算法以及他们的Android程序实现,并研究了怎样才干实现 ...

  8. java独立小程序实现AES加密和解密

    一.需求: web项目中配置文件配置的密码是明文的, 现在需要修改成密文, 加密方式采用AES, 于是写了个工具类用于加密和解密. 又因为这个密码是由客户来最终确定, 所以为了部署时方便起见, 写了个 ...

  9. php AES加密解密的例子

    一共有两个文件:AES.php(aes算法类文件)和aesDemo.php(应用实例文件) aesDemo.php:例子, <?php require_once('./AES.php'); // ...

随机推荐

  1. HDU 题目分类收集

    并查集题型简单并查集1213 How Many Tables 1232 畅通工程 (杭电简单的并查集不是很多) 简单最小生成树1233 还是畅通工程 1863 畅通工程 1874 畅通工程再续 187 ...

  2. Roads in the North(POJ 2631 DFS)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  3. Node.js how to respond to an upgrade request?

    You just need to call socket.write with the appropriate HTTP syntax as plain text along these lines ...

  4. 在Git中一定要关注的crlf自动转换

    GitHub 第一坑:换行符自动转换 如果你已经做出了错误的选择,也不需要重新安装,可以直接使用命令行来修改设置.很简单,直接打开这货自带的命令行工具 Git Bash,输入以下命令,再敲回车即可: ...

  5. Android APP开发需求文档范本

    Android  APP开发需求文档范本 软件需求文档格式的标准写法 1.引言 1.1 编写目的 • 阐明开发本软件的目的: 1.2 项目背景 • 标识待开发软件产品的名称.代码: • 列出本项目的任 ...

  6. C++ 11学习(1):lambda表达式

    转载请注明,来自:http://blog.csdn.net/skymanwu #include <iostream> #include <vector> #include &l ...

  7. Entity Framework 技术参考:http://kb.cnblogs.com/zt/ef/

    Entity Framework 技术参考:http://kb.cnblogs.com/zt/ef/

  8. 1 游戏逻辑架构,Cocos2d-x游戏项目创建,HelloWorld项目创建,HelloWorld程序分析,(CCApplicationProtocol,CCApplication,AppDeleg

     1 游戏逻辑架构 具体介绍 A 一个导演同一时间仅仅能执行一个场景,场景其中,能够同一时候载入多个层,一个层能够可载多个精灵.层中亦能够加层. B  场景切换 sceneàaddChild(la ...

  9. MVCC图示

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面:PostgreSQL内部结构与源代码研究索引页    回到顶级页面:PostgreSQL索引页 [作者:高健@博客园 luckyjackgao ...

  10. mysql命令学习笔记(1):show table status like 'user';显示表的相关信息

    show table status like 'user';显示表的相关信息 +------------+--------+---------+------------+------+-------- ...