加密

static byte[] EncryptBytes_Aes(byte[] plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an AesManaged object
// with the specified key and IV.
using (AesManaged aesAlg = new AesManaged())
{
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 (BinaryWriter bw = new BinaryWriter(csEncrypt))
{
bw.Write(plainText);
}
}
encrypted = msEncrypt.ToArray();
}
} // Return the encrypted bytes from the memory stream.
return encrypted;
}

解密

static byte[] DecryptBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV"); // Declare the string used to hold
// the decrypted text.
List<byte> plaintext = new List<byte>(); // Create an AesManaged object
// with the specified key and IV.
using (AesManaged aesAlg = new AesManaged())
{
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 (BinaryReader br = new BinaryReader(csDecrypt))
{
int bufferLen = 1024;
byte[] buffer = new byte[bufferLen];
int read = 0;
while ((read = br.Read(buffer, 0, bufferLen)) > 0)
plaintext.AddRange(buffer.Take(read));
}
}
} } return plaintext.ToArray();
}

演示:

string str = "Test ase,我们一起来测试AES";
byte[] plainBytes = Encoding.UTF8.GetBytes(str);
using (AesManaged aes = new AesManaged())
{
byte[] eBytes = EncryptBytes_Aes(plainBytes, aes.Key, aes.IV);
byte[] dBytes = DecryptBytes_Aes(eBytes, aes.Key, aes.IV); //OutputBytes(plainBytes);
//OutputBytes(eBytes);
//OutputBytes(dBytes); Console.WriteLine("明文:{0}", str);
Console.WriteLine("明文数组:{0}", FormatBytes(plainBytes));
Console.WriteLine("加密后的数组:{0}", FormatBytes(eBytes));
Console.WriteLine("解密后的数组:{0}", FormatBytes(dBytes));
Console.WriteLine("解密的明文:{0}", Encoding.UTF8.GetString(dBytes));
}
static string FormatBytes(byte[] bytes,byte countInLine = 10)
{
StringBuilder sb = new StringBuilder();
int i = 0;
foreach (byte b in bytes)
{
if (i++ % countInLine == 0)
sb.Append('\n');
sb.Append(String.Format("{0:X2} ", b));
}
sb.Append('\b'); return sb.ToString();
}

C# AES,AesManaged使用学习的更多相关文章

  1. Crypto++入门学习笔记(DES、AES、RSA、SHA-256)(加解密)

    转自http://www.cppblog.com/ArthasLee/archive/2010/12/01/135186.html 最近,基于某些原因和需要,笔者需要去了解一下Crypto++库,然后 ...

  2. Crypto++入门学习笔记(DES、AES、RSA、SHA-256)

    最先附上 下载地址 背景(只是个人感想,技术上不对后面的内容构成知识性障碍,可以skip): 最近,基于某些原因和需要,笔者需要去了解一下Crypto++库,然后对一些数据进行一些加密解密的操作. 笔 ...

  3. 学习加密(四)spring boot 使用RSA+AES混合加密,前后端传递参数加解密

      学习加密(四)spring boot 使用RSA+AES混合加密,前后端传递参数加解密 技术标签: RSA  AES  RSA AES  混合加密  整合   前言:   为了提高安全性采用了RS ...

  4. go标准库的学习-crypto/aes

    参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/aes" aes包实现了AES加密算法,参见U.S. Federal ...

  5. java学习-AES加解密之AES-128-CBC算法

    AES算法简介 AES是一种对称加密算法,或称分组对称加密算法.  是Advanced Encryption Standard高级加密标准,简称AES AES的基本要求是,采用对称分组密码体制.分组密 ...

  6. iOS客户端学习之AES加密

    数据加密在解密在软件开发过程中举足轻重的作用,可能有的公司在加密的时候有自己公司内部一套设计的算法,而在这方面不想浪费太大精力就可以去考虑使用第三方提供的加密算法,如AES加密算法,本篇内容介绍开源中 ...

  7. AES学习小结

    AES是基于数据块的加密方式,即每次处理的数据是一块(16字节),当数据不是16字节的倍数时填充,这就是所谓的分组密码(区别于基于比特位的流密码),16字节是分组长度. AES支持五种模式:CBC,C ...

  8. 学习Java AES加解密字符串和文件方法,然后写个简单工具类

    Reference Core Java Volume Ⅱ 10th Edition 1 对称加密 "Java密码扩展"包含了一个Cipher,它是所有密码算法的超类.通过getIn ...

  9. 【密码学】AES简单学习

    欧拉函数  公式 φ(n)=(p-1)(q-1) 小于x并且和x互质的数的个数   相关概念 因数:a*b=c 那么就称 a.b 是 c 的因数 素数:一个数如果除了1与它本身之外没有其他的因数,那么 ...

随机推荐

  1. 修改vim中的tab为4个空格

    记录一下,避免用时还得搜........ 1.临时修改 在vi中,set tabstop=4 或 set ts=4 2.永久修改 vi --version 查看要修改的文件 如果是vim的话,修改~/ ...

  2. .net如何后台批量删除

    button_Click(Sender sender,Event e){foreach (DataListItem item in DataList1.Items){CheckBox cbox=(Ch ...

  3. cookie 和 session 基本使用 以及 封装

    Cookie: 是一小段文本信息,用户请求页面的时候,在浏览器和服务器之间传递.用户每次访问的时候都会记录cookie,cookie里可以包含用户信息,浏览的历史记录等等:Cookie是由服务器端生成 ...

  4. 常用SQL Server分页方式

    假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询每页30条,查询第1500页(即第45001-45030条数据),字段ID聚集索引,YE ...

  5. js 去掉空格

    写成类的方法格式如下:(str.trim();)<script language="javascript"> String.prototype.trim=functio ...

  6. PHP学习笔记十一【数组】

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/h ...

  7. optimize table 表优化问题

    语法: optimize table '表名' 一,原始数据 1,数据量 2,存放在硬盘中的表文件大小 3,查看一下索引信息 索引信息中的列的信息说明. Table :表的名称.Non_unique: ...

  8. PLSQL实例(游标)

    在PLSQL块中执行SQL语句 A.   数据定义DDL: create,drop,truncate,不能直接执行,truncate执行时只做数据删除,不写日起,不维护索引 在PLSQL块中执行字符串 ...

  9. php.ini 全站,和htaccess web目录 默认头部和尾部 auto_prepend_file

    php.ini ; Automatically add files before PHP document. ; http://php.net/auto-prepend-file auto_prepe ...

  10. ~/microwindows-0.89pre8/src/bin$ ./nano-X error:Cannot bind to named socket

    GUI:microwindows-0.89pre8+nona-X you are successful compiling, run nano-X,below is information: ~/mi ...