using System;
using System.Security.Cryptography;
using System.Text;
using System.IO; namespace AES
{
public class AESEncryption
{
private static string strKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
/// <summary>
/// AES加密算法
/// </summary>
/// <param name="plainText">明文字符串</param>
/// <param name="strKey">密钥</param>
/// <returns>返回加密后的密文字节数组</returns>
public static string AESEncrypt(string plainText)
{
try
{
//分组加密算法
RijndaelManaged aes = new RijndaelManaged();
byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字节数组 //设置密钥及密钥向量
aes.Key = Encoding.UTF8.GetBytes(GetMD5(strKey));
aes.IV = Encoding.UTF8.GetBytes(GetMD5(strKey).Substring(, ));
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7; ICryptoTransform transform = aes.CreateEncryptor();
byte[] buffer= transform.TransformFinalBlock(inputByteArray, , inputByteArray.Length); //System.IO.MemoryStream ms = new System.IO.MemoryStream(); //CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write); //cs.Write(inputByteArray, 0, inputByteArray.Length);
//cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder();
foreach (byte b in buffer)
{
ret.AppendFormat("{0:x2}", b);
}
return ret.ToString();
}
catch
{
return plainText;
}
} /// <summary>
/// AES解密
/// </summary>
/// <param name="cipherText">密文字节数组</param>
/// <param name="strKey">密钥</param>
/// <returns>返回解密后的字符串</returns>
public static string AESDecrypt(string cipherText)
{
try
{
RijndaelManaged aes = new RijndaelManaged(); int len;
len = cipherText.Length / ;
byte[] inputByteArray = new byte[len];
int x, i;
for (x = ; x < len; x++)
{
i = Convert.ToInt32(cipherText.Substring(x * , ), );
inputByteArray[x] = (byte)i;
} //byte[] inputByteArray = Encoding.UTF8.GetBytes(cipherText);
//设置密钥及密钥向量
aes.Key = Encoding.UTF8.GetBytes(GetMD5(strKey));
aes.IV = Encoding.UTF8.GetBytes(GetMD5(strKey).Substring(, ));
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7; ICryptoTransform transform = aes.CreateDecryptor();
byte[] buffer = transform.TransformFinalBlock(inputByteArray, , inputByteArray.Length); //System.IO.MemoryStream ms = new System.IO.MemoryStream();
//CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write);
//cs.Write(inputByteArray, 0, inputByteArray.Length);
//cs.FlushFinalBlock();
string strDecrypt = Encoding.UTF8.GetString(buffer);
return strDecrypt; }
catch
{
return cipherText;
}
} public static string GetMD5(string str)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(str)))
{
sb.Append(b.ToString("X2"));
}
return sb.ToString();
} }
}

AES加密和解密的更多相关文章

  1. Php AES加密、解密与Java互操作的问题

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...

  2. 探讨数据进行AES加密和解密以及.NET Core对加密和解密为我们提供了什么?

    前言 对于数据加密和解密每次我都是从网上拷贝一份,无需有太多了解,由于在.net core中对加密和解密目前全部是统一了接口,只是做具体的实现,由于遇到过问题,所以将打算基本了解下其原理,知其然足矣, ...

  3. 探讨.NET Core中实现AES加密和解密以及.NET Core为我们提供了什么方便!

    前言 对于数据加密和解密每次我都是从网上拷贝一份,无需有太多了解,由于在.net core中对加密和解密目前全部是统一了接口,只是做具体的实现,由于遇到过问题,所以将打算基本了解下其原理,知其然足矣, ...

  4. Oracle的AES加密与解密用法

    Oracle的AES加密与解密用法2013年12月11日 11:50:35 iteye_751 阅读数:428--加密字符串create or replace function des3_enc( i ...

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

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

  6. php的AES加密、解密类

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

  7. java与C#、.NET AES加密、解密 解决方案

      1.情景展示 Java提供的密钥,C#无法解密. 2.原因分析 在Java中,AES的实际密钥需要用到KeyGenerator 和 SecureRandom,但是C#和.NET 里面没有这2个类, ...

  8. java AES加密、解密(兼容windows和linux)

      java AES加密.解密 CreationTime--2018年7月14日10点06分 Author:Marydon 1.准备工作 updateTime--2018年8月10日15点28分 up ...

  9. polarssl rsa & aes 加密与解密

    上周折腾加密与解密,用了openssl, crypto++, polarssl, cyassl, 说起真的让人很沮丧,只有openssl & polarssl两个库的RSA & AES ...

  10. C# AES 加密与解密

    AES 算法加密(ECB模式) 将明文加密,加密后进行base64编码,返回密文 /// <summary> /// AES 算法加密(ECB模式) 将明文加密,加密后进行base64编码 ...

随机推荐

  1. hbuilder的aptana php插件无法提示命名空间之外函数和对象的解决办法

  2. JavaScript 闭包详解

    一.Javascript闭包的用途 事实上,通过使用闭包,我们可以做很多事情.比如模拟面向对象的代码风格:更优雅,更简洁的表达出代码:在某些方面提升代码的执行效率. 1.匿名自执行函数 我们知道所有的 ...

  3. 网络服务器带宽Mbps、Mb/s、MB/s有什么区别?10M、100M到底是什么概念?

    网络服务器带宽Mbps.Mb/s.MB/s有什么区别?我们经常听到IDC提供的服务器接入带宽是10M独享,或者100M独享,100M共享之类的数据.这的10M.100M到底是什么概念呢? 工具/原料 ...

  4. php 设计API之优化 记

    服务器端 可以考虑使用rest实现,清晰url:put http://aa.com/news 客户端 curl实现muliti机制,实现多线程并发,节省多接口调用的时间 curl实现keepalive ...

  5. PHP 扩展库

    表 6.1. PHP 扩展库 扩展库 说明 注解 php_bz2.dll bzip2 压缩函数库 无 php_calendar.dll 历法转换函数库 自 PHP 4.0.3 起内置 php_cpdf ...

  6. DSP中的cmd文件

    一.CMD文件 链接命令文件(Link Command Files),以后缀.cmd结尾,简称CMD文件. CMD文件的两大功能是指示存储空间和分配段到存储空间. 在编写CMD文件时,主要采用MEMO ...

  7. Linux软件安装方法小结(附:rpm详解)(转载)

    在使用Linux系统的过程中,软件包的安装是避免不了的,在Linux下,软件安装程序的种类很多,安装方法也各式各样,(舒适性自然比不上windows :-))不过我们常见的软件包有两种: 1)含有软件 ...

  8. SQL Server 2008连接字符串写法大全{转}

    一..NET Framework Data Provider for SQL Server 类型:.NET Framework类库使用:System.Data.SqlClient.SqlConnect ...

  9. set gameobject Icons by Script

    有很多时候我们需要在编辑器查看一个Gameobject的移动,有些人采用Gizoms类,可是如果不想用,可以使用U3D内置的Icon类. 但是如果想在脚本中设置而不是通过手动选择呢? Google之, ...

  10. unity3d 自动保存

    using UnityEngine; using UnityEditor; using System; public class AutoSave : EditorWindow { private b ...