AES加密类
代码:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text; namespace Utils
{
/// <summary>
/// AES加密解密
/// </summary>
public class AES
{
#region 加密
#region 加密字符串
/// <summary>
/// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
/// </summary>
/// <param name="EncryptString">待加密密文</param>
/// <param name="EncryptKey">加密密钥</param>
public static string AESEncrypt(string EncryptString, string EncryptKey)
{
return Convert.ToBase64String(AESEncrypt(Encoding.Default.GetBytes(EncryptString), EncryptKey));
}
#endregion #region 加密字节数组
/// <summary>
/// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
/// </summary>
/// <param name="EncryptString">待加密密文</param>
/// <param name="EncryptKey">加密密钥</param>
public static byte[] AESEncrypt(byte[] EncryptByte, string EncryptKey)
{
if (EncryptByte.Length == ) { throw (new Exception("明文不得为空")); }
if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
byte[] m_strEncrypt;
byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
byte[] m_salt = Convert.FromBase64String("gsf4jvkyhye5/d7k8OrLgM==");
Rijndael m_AESProvider = Rijndael.Create();
try
{
MemoryStream m_stream = new MemoryStream();
PasswordDeriveBytes pdb = new PasswordDeriveBytes(EncryptKey, m_salt);
ICryptoTransform transform = m_AESProvider.CreateEncryptor(pdb.GetBytes(), m_btIV);
CryptoStream m_csstream = new CryptoStream(m_stream, transform, CryptoStreamMode.Write);
m_csstream.Write(EncryptByte, , EncryptByte.Length);
m_csstream.FlushFinalBlock();
m_strEncrypt = m_stream.ToArray();
m_stream.Close(); m_stream.Dispose();
m_csstream.Close(); m_csstream.Dispose();
}
catch (IOException ex) { throw ex; }
catch (CryptographicException ex) { throw ex; }
catch (ArgumentException ex) { throw ex; }
catch (Exception ex) { throw ex; }
finally { m_AESProvider.Clear(); }
return m_strEncrypt;
}
#endregion
#endregion #region 解密
#region 解密字符串
/// <summary>
/// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
/// </summary>
/// <param name="DecryptString">待解密密文</param>
/// <param name="DecryptKey">解密密钥</param>
public static string AESDecrypt(string DecryptString, string DecryptKey)
{
return Encoding.Default.GetString(AESDecrypt(Convert.FromBase64String(DecryptString), DecryptKey));
}
#endregion #region 解密字节数组
/// <summary>
/// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
/// </summary>
/// <param name="DecryptString">待解密密文</param>
/// <param name="DecryptKey">解密密钥</param>
public static byte[] AESDecrypt(byte[] DecryptByte, string DecryptKey)
{
if (DecryptByte.Length == ) { throw (new Exception("密文不得为空")); }
if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
byte[] m_strDecrypt;
byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
byte[] m_salt = Convert.FromBase64String("gsf4jvkyhye5/d7k8OrLgM==");
Rijndael m_AESProvider = Rijndael.Create();
try
{
MemoryStream m_stream = new MemoryStream();
PasswordDeriveBytes pdb = new PasswordDeriveBytes(DecryptKey, m_salt);
ICryptoTransform transform = m_AESProvider.CreateDecryptor(pdb.GetBytes(), m_btIV);
CryptoStream m_csstream = new CryptoStream(m_stream, transform, CryptoStreamMode.Write);
m_csstream.Write(DecryptByte, , DecryptByte.Length);
m_csstream.FlushFinalBlock();
m_strDecrypt = m_stream.ToArray();
m_stream.Close(); m_stream.Dispose();
m_csstream.Close(); m_csstream.Dispose();
}
catch (IOException ex) { throw ex; }
catch (CryptographicException ex) { throw ex; }
catch (ArgumentException ex) { throw ex; }
catch (Exception ex) { throw ex; }
finally { m_AESProvider.Clear(); }
return m_strDecrypt;
}
#endregion
#endregion }
}
AES加密类的更多相关文章
- PHP的AES加密类
PHP的AES加密类 aes.php <?php /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ...
- php实现aes加密类
php实现的aes加密类,代码中有使用方法. <?php //php aes加密类 class AESMcrypt { public $iv = null; public $key = null ...
- php AES 加密类
<?php class CryptAES { protected $cipher = MCRYPT_RIJNDAEL_128; protected $mode = MCRYPT_MODE_ECB ...
- AES加密解密的例子小结
话不多说,先放上代码,一共有两个文件:AES.php(aes算法类文件)和aesDemo.php(应用实例文件),这里只贴出aesDemo.php,其他的看附件吧!aesDemo.php: 例子, ...
- php AES加密解密的例子
一共有两个文件:AES.php(aes算法类文件)和aesDemo.php(应用实例文件) aesDemo.php:例子, <?php require_once('./AES.php'); // ...
- [PHP]AES加密----PHP服务端和Android客户端
本文采取128位AES-CBC模式加密和解密 1.首先对服务端安装mcrypt: sudo apt-get install php5-mcrypt php5-dev sudo php5enmod mc ...
- AES加密解密 助手类 CBC加密模式
"; string result1 = AESHelper.AesEncrypt(str); string result2 = AESHelper.AesDecrypt(result1); ...
- Java 关于密码处理的工具类[MD5编码][AES加密/解密]
项目中又遇到了加密问题,又去翻了半天,然后做测试,干脆就把常用的两类小结一下. 1.第一种所谓的MD5加密 其实也不算加密,只是基于Hash算法的不可逆编码而已,等于说,一旦经过MD5处理,是不可能从 ...
- Java AES 加密工具类
package com.microwisdom.utils; import java.security.NoSuchAlgorithmException; import java.security.S ...
随机推荐
- 开发者必知的几款App快速开发工具
“我有一个好创意,就差一个CTO……” ,这是今年炒的比较火的一句话. “原生APP开发难度大,周期长,成本高,还没上线市场已经被占领了.这个有没有解决方案?” “APP版本迭代更新,都是企业的一道难 ...
- C#Lite Unity热更新开源解决方案改名C#Light
因为此脚本语言与C#Lite Framework 开源项目重名,故修改名称,发音不变.
- ueditor上传大容量视频报http请求错误的解决方法
故障现象: 当使用百度编辑器ueditor上传大容量视频或大容量图片的时候,编辑器报"http请求错误"的解决方法详解: 原因分析: 目前很多CMS整合了百度的ueditor编辑器 ...
- 移动APP的自动化测试
开发移动应用,最耗时耗力的就是手动测试APP的每个功能点或修复bug.有人就会提议App的业务逻辑可以使用nUnit或xUnit测试单元来辅助完成.那用户界面要如何测试?众所周知,移动设备多种多样,数 ...
- 自制操作系统 (三) 从启动区执行操作系统并进入C世界
qq:992591601 欢迎交流 2016.04.03 2016.05.31 2016.06.29 这一章是有些复杂的,我不太懂作者为什么要把这么多内容都放进一天. 1读入了十个柱面 2从启动区执行 ...
- Nginx+Keepalived(带Nginx监控脚本)
转载于:http://www.itxuexiwang.com/a/liunxjishu/2016/0220/151.html?1456381460 Keepalived+ nginx的安装部署 主机: ...
- 谈谈设计模式~原型模式(Prototype)
返回目录 原型模式是创建型模式的一种,其特点在于通过“复制”一个已经存在的实例来返回新的实例(clone),而不是新建(new)实例.被复制的实例就是我们所称的“原型”,这个原型是可定制的. 原型模式 ...
- [Java集合] 彻底搞懂HashMap,HashTable,ConcurrentHashMap之关联.
注: 今天看到的一篇讲hashMap,hashTable,concurrentHashMap很透彻的一篇文章, 感谢原作者的分享. 原文地址: http://blog.csdn.net/zhanger ...
- IIS集成模式下,URL重写后获取不到Session值
近期给公司网站添加了伪静态功能,但是今天发现了在伪静态的页面中,Session值是获取不到的. 原因是在伪静态请求的时候,Session请求被“过滤”掉了. 开始是把web.config文件中的mod ...
- C++标准库vector类型详解
Vector简介 vector是定义在C++标准模板库,它是一个多功能.能够操作多种数据结构和算法的模板类(关于模板类我们后面会介绍,如何创建自己的模板类).vector是一个容器,能够像容器一样存放 ...