加密

  1. static byte[] EncryptBytes_Aes(byte[] plainText, byte[] Key, byte[] IV)
  2. {
  3. // Check arguments.
  4. if (plainText == null || plainText.Length <= 0)
  5. throw new ArgumentNullException("plainText");
  6. if (Key == null || Key.Length <= 0)
  7. throw new ArgumentNullException("Key");
  8. if (IV == null || IV.Length <= 0)
  9. throw new ArgumentNullException("IV");
  10. byte[] encrypted;
  11. // Create an AesManaged object
  12. // with the specified key and IV.
  13. using (AesManaged aesAlg = new AesManaged())
  14. {
  15. aesAlg.Key = Key;
  16. aesAlg.IV = IV;
  17.  
  18. // Create a decrytor to perform the stream transform.
  19. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
  20.  
  21. // Create the streams used for encryption.
  22. using (MemoryStream msEncrypt = new MemoryStream())
  23. {
  24. using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
  25. {
  26.  
  27. using (BinaryWriter bw = new BinaryWriter(csEncrypt))
  28. {
  29. bw.Write(plainText);
  30. }
  31. }
  32. encrypted = msEncrypt.ToArray();
  33. }
  34. }
  35.  
  36. // Return the encrypted bytes from the memory stream.
  37. return encrypted;
  38. }

解密

  1. static byte[] DecryptBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
  2. {
  3. // Check arguments.
  4. if (cipherText == null || cipherText.Length <= 0)
  5. throw new ArgumentNullException("cipherText");
  6. if (Key == null || Key.Length <= 0)
  7. throw new ArgumentNullException("Key");
  8. if (IV == null || IV.Length <= 0)
  9. throw new ArgumentNullException("IV");
  10.  
  11. // Declare the string used to hold
  12. // the decrypted text.
  13. List<byte> plaintext = new List<byte>();
  14.  
  15. // Create an AesManaged object
  16. // with the specified key and IV.
  17. using (AesManaged aesAlg = new AesManaged())
  18. {
  19. aesAlg.Key = Key;
  20. aesAlg.IV = IV;
  21.  
  22. // Create a decrytor to perform the stream transform.
  23. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
  24.  
  25. // Create the streams used for decryption.
  26. using (MemoryStream msDecrypt = new MemoryStream(cipherText))
  27. {
  28. using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  29. {
  30. using (BinaryReader br = new BinaryReader(csDecrypt))
  31. {
  32. int bufferLen = 1024;
  33. byte[] buffer = new byte[bufferLen];
  34. int read = 0;
  35. while ((read = br.Read(buffer, 0, bufferLen)) > 0)
  36. plaintext.AddRange(buffer.Take(read));
  37. }
  38. }
  39. }
  40.  
  41. }
  42.  
  43. return plaintext.ToArray();
  44. }

演示:

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

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. What should we do when meet a crash in android?

    制造一个crash   为了演示的目的,我在libsensors的open_sensors_device中故意制造了一个crash:   static int open_sensors_device( ...

  2. bach cello

    http://bachlb.blog.163.com/blog/static/1819105120073275251223 一个偶然的机会,卡萨尔斯的父亲来巴塞罗那看卡萨尔斯,并且一起去逛了一间海边的 ...

  3. SQL查询多行合并成一行

    问题描述:无论是在sql 2000,还是在 sql 2005 中,都没有提供字符串的聚合函数,  所以,当我们在处理下列要求时,会比较麻烦:有表tb, 如下:id    value----- ---- ...

  4. Emgu学习笔记(一)安装及运行Sample

    1.简单说明 Emgu是Dot Net平台对OpenCV的封装,本质上没有增加新功能,是通过Dot Net的平台调用技术直接调用OpenCV C++语言写的库,使用我们可以方便用.net平台通过Ope ...

  5. jquery 项目所用

    <script> $(document).ready(function(){ $.ajax({ type:'post', url :'interface.ajax.php', data:{ ...

  6. golang make the first character in a string lowercase/uppercase

    import ( "unicode" ) func UcFirst(str string) string { for i, v := range str { return stri ...

  7. php 格式

    $abc = ($_POST[' : strtotime($_POST['start_time']); 解析:判断接收的数据是否为0,如果等于0赋值0,若不等于,则赋值获取的数值. strtotime ...

  8. 设计师如何为 Android 应用标注尺寸

    http://blog.cutterman.cn/?p=33 1. 画布大小定位 720 x 1280,72 dpi2. 只使用偶数单位的尺寸,比如 96 px 的列表项高度,16 px 的边距,64 ...

  9. fdisk -l 找不到分区怎么办?想办法找到隐藏分区。

    centos6.6 装系统的时候,选取系统默认分区.装好后发现少了一大部分空间,通过fdisk -l 找不到分区,另外使用 parted -l 同样找不到消失的柱面. 如: [root@mysql]# ...

  10. safari的input问题

    切图网用户体验团队QUX在手机移动触屏web前端开发中碰到一个问题 — 纯css3定义的按钮在android安卓系统下显示正常,但是在苹果ios系统下,以ipad为例,ipad下呈现渐变和圆角状态 . ...