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. github/gitlab 管理多个ssh key

    github/gitlab 管理多个ssh key 以前只使用一个 ssh key 在github上提交代码,由于工作原因,需要再添加一个ssh key在公司的 gitlab上提交代码,下面记录下配置 ...

  2. From 《Soft Skill》——Chapter 69. My personal success book list

    There have been many excellent books that have greatly influenced what I believe and how I behave. I ...

  3. c++ 缺少动态库

    http://www.cnblogs.com/smartvessel/archive/2011/01/21/1940868.html 总结下来主要有3种方法: . 用ln将需要的so文件链接到/usr ...

  4. Leetcode#108 Convert Sorted Array to Binary Search Tree

    原题地址 对于已排序数组,二分法递归构造BST 代码: TreeNode *buildBST(vector<int> &num, int i, int j) { if (i > ...

  5. iNode for linux install

    http://wenku.baidu.com/link?url=953T6GZCnaBzwr4YqPFUT4oOyYr4wyOnXlCLO1OUYZkaJWh2fTs634SM7ZpYiTKkpmYX ...

  6. 服务器NPC的ID如何分配的

    服务器ID分配包括NPC,Monster,Pet的ID分配都是调用allocateUID然后自动保存的ID加一,pet说是通过玩家的ID移位获得的,调试一下发现还是调用allocateUID,如果通过 ...

  7. try-catch语句讲解

    try-catch 语句由一个 try 块后跟一个或多个 catch 子句构成,这些子句指定不同的异常处理程序. 引发异常时,公共语言运行时 (CLR) 会查找处理此异常的 catch 语句. 如果当 ...

  8. Properties的用法

    /** * Description:Properties是HashTable的子類,其存儲形式鍵值對. */ import java.util.*; public class PropertysTes ...

  9. POJ 2140

    #include<iostream> #include<stdio.h> using namespace std; int main() { int num; int i; i ...

  10. [C++]类的继承与派生

    继承性是面向对象程序设计的第二大特性,它允许在既有类的基础上创建新类,新类可以继承既有类的数据成员和成员函数,可以添加自己特有的数据成员和成员函数,还可以对既有类中的成员函数重新定义.利用类的继承和派 ...