#AesEncrypt:Aes加密/解密示例项目 <br>
  附件中的“AesEncrypt.zip”是本项目的exe文件,可直接下载下来运行和查看。
  *高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。*<br>
  好的百科已普及,现在来看看这Aes怎么使用,由于需要把加密后的byte[]输出成字符串,但是普通字符串无法兼容,所以这里使用的base64字符串。详见代码AesEncrypt\AesEncrypt.cs。<br>
  为了直观的看到Aes的加解密过程,现在给出几个示例,如下图<br>
  1.在“未加密”的文本框中输入一个普通的字符串,点击“加密”按钮,将在”已加密“的文本框中得到一个Aes加密后的base64字符串。**这个“已加密”的base64字符串就可以放到系统中使用了,例如可以填到数据库链接字符串,也可以存储得到数据中。**<br>
  2.第一步完成后,“已加密”文本框中已经被填写了Aes加密后的base64字符串,为了看到效果,可以把“未加密”文本框中的普通文本删掉,然后点击“解密”按钮,就可以在“未加密”文本框中看到之前的普通文本。<br>
  3.经过以上两步操作,我想这个窗体已经完成了示例的作用,Aes加密需要两个byte[],一个是IV,它是对称算法的初始化向量,一个是Key,它是对称算法的密钥。IV是一个Length=16的byte[],Key是一个Length=32的byte[],**因此可以在AesEncrypt\AesEncrypt.cs类中设置自己的固定密钥用以在程序中加密/解密,如以下代码中所示,可以填写任意的数字,来当做密钥。**<br>
```C#
/// <summary>
/// 对称算法的密钥
/// </summary>
private readonly static byte[] aesKey = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 };
/// <summary>
/// 对称算法的初始化向量
/// </summary>
private readonly static byte[] aesIV = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
```

  以上步骤是在程序中的基本使用方式,Aes还可以有更多的用途,以下来个抛砖引玉。<br>
  1.在“玩玩解密”窗体中点击“发送”按钮之后,将把“未加密”中的文本使用Aes加密后转换成base64字符串填写到“已加密”文本框中,同时将利用以下代码将Aes类可随机生成的IV和Key(注意IV和Key都是byte[]),转换为base64后分别填写到“向量(IV)”和“密钥(Key)”中。<br>
```C#
private void button1_Click(object sender, EventArgs e)
{
    using (Aes myAes = Aes.Create())
    {
        AesEncryptExtension aes = new AesEncryptExtension();
        textBox2.Text = aes.Encrypt(textBox1.Text, myAes.Key, myAes.IV);
        textBox3.Text = Convert.ToBase64String(myAes.IV);
        textBox4.Text = Convert.ToBase64String(myAes.Key);
    }
    Form3 form = new Form3();
    form.Show();
}

```

  2.第一步完成后,同时会弹出“接收密文”窗体,如下图。将“玩玩解密”窗体中的“已加密”、“向量(IV)”和“密钥(key)”填写到“接收密文”窗体对应文本框中,将获取到未加密的普通文本。<br>

  3.以上所示,是手动填写密文、IV和key来获取内容。当然你也可以自动传过去,只要两个系统的iv和key相同就可以实现解密,前提是两个系统中的动态密钥规律要相同,说到这里我想聪明的你应该已经知道Aes加密怎么玩了,好好想想,可以有多种应用。<br>
  举个例子,图例解释,IV和key合称为钥匙,IV和Key生成算法称为钥匙机,每一把被生产出来的钥匙都刻着钥匙编号,Aes本身称为保险柜,被加密内容则会被放进“保险柜”。两方同时使用钥匙机生产钥匙,每一把钥匙都刻着钥匙编号,运输的时候只运输保险柜和钥匙编号,接收到保险柜的一方,用钥匙编号从钥匙机中拿钥匙打开保险柜,取得加密内容。以上例可称为一个较为简单的Aes应用方式。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks; namespace AesEncrypt
{
/// <summary>
/// Aes加密算法适用于固定的加解密
/// </summary>
public static class AesEncrypt
{
/// <summary>
/// 对称算法的密钥
/// </summary>
private readonly static byte[] aesKey = { , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , };
/// <summary>
/// 对称算法的初始化向量
/// </summary>
private readonly static byte[] aesIV = { , , , , , , , , , , , , , , , }; /// <summary>
/// 加密
/// </summary>
/// <param name="Text">被加密的字符串</param>
/// <returns>Base64</returns>
public static string Encrypt(string Text)
{
byte[] encrypted;
encrypted = EncryptStringToBytes_Aes(Text, aesKey, aesIV);
return Convert.ToBase64String(encrypted);
} private static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= )
{
throw new ArgumentNullException("plainText");
}
if (Key == null || Key.Length <= )
{
throw new ArgumentNullException("Key");
}
if (IV == null || IV.Length <= )
{
throw new ArgumentNullException("IV");
}
byte[] encrypted;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV; // Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{ //Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
} /// <summary>
/// 解密
/// </summary>
/// <param name="text">被Aes加密后的Base64</param>
/// <returns>字符串</returns>
public static string Decrypt(string text)
{
string roundtrip; byte[] myByte = Convert.FromBase64String(text);
roundtrip = DecryptStringFromBytes_Aes(myByte, aesKey, aesIV); return roundtrip;
}
private static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= )
{
throw new ArgumentNullException("cipherText");
}
if (Key == null || Key.Length <= )
{
throw new ArgumentNullException("Key");
}
if (IV == null || IV.Length <= )
{
throw new ArgumentNullException("IV");
}
// Declare the string used to hold
// the decrypted text.
string plaintext; // Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV; // Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{ // Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks; namespace AesEncrypt
{
/// <summary>
/// Aes扩展方法
/// </summary>
public class AesEncryptExtension
{ /// <summary>
/// 加密
/// </summary>
/// <param name="Text">被加密的字符串</param>
/// <param name="aesKey">对称算法的密钥</param>
/// <param name="aesIV">设置对称算法的初始化向量</param>
/// <returns>Base64</returns>
public string Encrypt(string Text, byte[] aesKey, byte[] aesIV)
{
byte[] encrypted;
encrypted = EncryptStringToBytes_Aes(Text, aesKey, aesIV);
return Convert.ToBase64String(encrypted);
} private byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= )
{
throw new ArgumentNullException("plainText");
}
if (Key == null || Key.Length <= )
{
throw new ArgumentNullException("Key");
}
if (IV == null || IV.Length <= )
{
throw new ArgumentNullException("IV");
}
byte[] encrypted;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV; // Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{ //Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
} /// <summary>
/// 解密
/// </summary>
/// <param name="text">被Aes加密后的Base64</param>
/// <param name="aesKey">对称算法的密钥</param>
/// <param name="aesIV">设置对称算法的初始化向量</param>
/// <returns>字符串</returns>
public string Decrypt(string text, byte[] aesKey, byte[] aesIV)
{
string roundtrip; byte[] myByte = Convert.FromBase64String(text);
roundtrip = DecryptStringFromBytes_Aes(myByte, aesKey, aesIV); return roundtrip;
}
private string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= )
{
throw new ArgumentNullException("cipherText");
}
if (Key == null || Key.Length <= )
{
throw new ArgumentNullException("Key");
}
if (IV == null || IV.Length <= )
{
throw new ArgumentNullException("IV");
}
// Declare the string used to hold
// the decrypted text.
string plaintext; // Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV; // Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{ // Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace AesEncrypt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{ textBox2.Text = AesEncrypt.Encrypt(textBox1.Text);
} private void button2_Click(object sender, EventArgs e)
{ textBox1.Text = AesEncrypt.Decrypt(textBox2.Text);
} private void 玩玩解密ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.Show();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace AesEncrypt
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
using (Aes myAes = Aes.Create())
{
AesEncryptExtension aes = new AesEncryptExtension();
textBox2.Text = aes.Encrypt(textBox1.Text, myAes.Key, myAes.IV);
textBox3.Text = Convert.ToBase64String(myAes.IV);
textBox4.Text = Convert.ToBase64String(myAes.Key);
}
Form3 form = new Form3();
form.Show();
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace AesEncrypt
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
AesEncryptExtension aes = new AesEncryptExtension();
byte[] aesIV = Convert.FromBase64String(textBox3.Text);
byte[] aesKey = Convert.FromBase64String(textBox4.Text); textBox1.Text = aes.Decrypt(textBox2.Text, aesKey, aesIV);
}
}
}

Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法

Aes加密/解密示例项目的更多相关文章

  1. 你真的了解字典(Dictionary)吗? C# Memory Cache 踩坑记录 .net 泛型 结构化CSS设计思维 WinForm POST上传与后台接收 高效实用的.NET开源项目 .net 笔试面试总结(3) .net 笔试面试总结(2) 依赖注入 C# RSA 加密 C#与Java AES 加密解密

    你真的了解字典(Dictionary)吗?   从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点.为了便于描述,我把上面 ...

  2. AES加密解密——AES在JavaWeb项目中前台JS加密,后台Java解密的使用

    一:前言 在软件开发中,经常要对数据进行传输,数据在传输的过程中可能被拦截,被监听,所以在传输数据的时候使用数据的原始内容进行传输的话,安全隐患是非常大的.因此就要对需要传输的数据进行在客户端进行加密 ...

  3. 非对称技术栈实现AES加密解密

    非对称技术栈实现AES加密解密 正如前面的一篇文章所述,https协议的SSL层是实现在传输层之上,应用层之下,也就是说在应用层上看到的请求还是明码的,对于某些场景下要求这些http请求参数是非可读的 ...

  4. ruby AES加密解密

    最近和京东合作做一个项目,在接口对接传递参数时,参数需要通过AES加密解密. 本来想到用gem 'aescrypt'处理,但是aescrypt的编码方式用的base64,而京东那边用的是16进制.所以 ...

  5. Java 关于密码处理的工具类[MD5编码][AES加密/解密]

    项目中又遇到了加密问题,又去翻了半天,然后做测试,干脆就把常用的两类小结一下. 1.第一种所谓的MD5加密 其实也不算加密,只是基于Hash算法的不可逆编码而已,等于说,一旦经过MD5处理,是不可能从 ...

  6. .Net Core AES加密解密

    一.AES说明 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替 ...

  7. C# 实现 JAVA AES加密解密[原创]

    以下是网上普遍能收到的JAVA AES加密解密方法. 因为里面用到了KeyGenerator 和 SecureRandom,但是.NET 里面没有这2个类.无法使用安全随机数生成KEY. 我们在接收J ...

  8. crypto AES 加密 解密

    1.aes加密解密说明 https://juejin.im/entry/59eea48e6fb9a0451968c25f aes是对称加密算法 md5是摘要算法,不算是加密,主要用来对信息一致性和完整 ...

  9. C#中使用DES和AES加密解密

    C#中使用DES和AES加密解密 2008-01-12 09:37 using System;using System.Text;using System.Security.Cryptography; ...

随机推荐

  1. Python main()函数解析

    __main__ — Top-level script environment '__main__'是顶级代码执行的作用域的名字. 当一个模块从标准input, 一个脚本文件,或一个交互命令read读 ...

  2. 《Python基础教程》第五章:条件、循环和其他语句

    在Python中赋值运算和比较运算是可以连接的,运算符可以连在一起使用,如:0<age<100 ==运算符判定两个对象是否相等,is判定两者是否等同(同一个对象) 断言,在错误条件出现时直 ...

  3. vmware虚拟机新增磁盘及挂载详细步骤

    虚拟机新增磁盘及挂载步骤 1.新增磁盘 (1)  编辑虚拟机设置->添加 (2)  选择硬盘->下一步 (3)  选择SCSI格式,下一步 (4)  创建新虚拟磁盘,下一步 (5)  设置 ...

  4. GIT和SVN的区别(面试)

    Cit是分布式,而SVN不是分布式 存储内容的时候,Git按元数据方式存储,而SVN是按文件 Git没有一个全局版本号,SVN有,目前为止这是SVN相比Git缺少的最大的一个特征 Git的内容完整性要 ...

  5. python中的list,tuple,dict,set简介---陈雨童

    变量和对象 变量把对象和自己连接起来(指针连接对象空间),引用建立了变量和对象之间的映射关系,这就是引用.引用完成,就实现了赋值.变量通过对象的内存地址指向对象,类似于软链接 将变量a赋值给变量b,其 ...

  6. Spark配置详解

    Spark提供三个位置用来配置系统: Spark属性:控制大部分的应用程序参数,可以用SparkConf对象或者Java系统属性设置 环境变量:可以通过每个节点的 conf/spark-env.sh脚 ...

  7. vue路由登录拦截(vue router登录权限控制)

    实现原理: 哪些路由需要验证需要在路由文件router/index.js中指定: { path: '/', component: Home, name: 'Home', iconCls: 'fa fa ...

  8. jmeter-模板报错-Fatal Error! com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 2 字节的 UTF-8 序列的字节 2 无效

    使用的模板不能有中文!!!!检查模板.

  9. JVM(一),谈谈你对java的理解

    一.谈谈你对java的理解 1.Java特性 (1)平台无关性 一次编译到处运行 (2)GC 垃圾回收机制 (3)语言特性 泛型-反射机制-lambda表达式 (4)面向对象 面向对象语言-三大特性( ...

  10. java+Word图片上传控件

    这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用 后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下) ...