C# AES,AesManaged使用学习

加密

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使用学习,布布扣,bubuko.com

时间: 2024-08-25 03:51:14

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

Go学习——密码学

package main import ( "crypto/aes" "fmt" "strings" ) func main() { //////////////------AES加密------////////////// //秘钥 16/24/32bytes对应AES-128/AES-192/AES-256. key := []byte{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8

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

最先附上 下载地址 背景(只是个人感想,技术上不对后面的内容构成知识性障碍,可以skip): 最近,基于某些原因和需要,笔者需要去了解一下Crypto++库,然后对一些数据进行一些加密解密的操作. 笔者之前没接触过任何加密解密方面的知识(当然,把每个字符的ASCII值加1之流对明文进行加密的“趣事”还是干过的,当时还很乐在其中.),甚至一开始连Crypto++的名字都没有听过,被BS了之后,就开始了Crypto++的入门探索过程. 最初,大概知道了要了解两大类算法中的几个算法——对称加密算法:D

AES分组对称加密算法学习笔记

State说明 S盒变换SubBytes 行变换ShiftRows 列变换MixColumns 与扩展密钥的异或运算AddRoundKey 密钥扩展程序Key Expansion 参考文献 我们首先对AES分组对称加密算法有一个宏观的了解,它的模式主要为 以下是AES主要加密过程的伪代码. Cipher(byte in[4*4] , byte out[4*4] , word w[4*(Nr+1)]) begin byte state[4,4] state=in AddRoundKey(state

iOS客户端学习之AES加密

数据加密在解密在软件开发过程中举足轻重的作用,可能有的公司在加密的时候有自己公司内部一套设计的算法,而在这方面不想浪费太大精力就可以去考虑使用第三方提供的加密算法,如AES加密算法,本篇内容介绍开源中国iOS客户端使用ASE算法加密密码: AES   GitHub 下载地址  https://github.com/Gurpartap/AESCrypt-ObjC 对一个比较大的工程我们可能都不知道某个类库或者方法在哪被使用,但是智能的Xcode给我们提供了一个全局搜索的功能,我们可以在真个工程中来

iOS客户端学习 AES加密和解密

数据加密在解密在软件开发过程中举足轻重的作用,可能有的公司在加密的时候有自己公司内部一套设计的算法,而在这方面不想浪费太大精力就可以去考虑使用第三方提供的加密算法,如AES加密算法,本篇内容介绍开源中国iOS客户端使用ASE算法加密密码: AES   GitHub 下载地址  https://github.com/Gurpartap/AESCrypt-ObjC 对一个比较大的工程我们可能都不知道某个类库或者方法在哪被使用,但是智能的Xcode给我们提供了一个全局搜索的功能,我们可以在真个工程中来

.net aes加密视频等文件

公司学习平台在app端下载下来的视频需要加密 随查找资料参考一些写法 写了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 ConsoleApplication

运维学习之加密和解密

运维学习之加密与解密: 众所周知,在网络的世界里不存在绝对的安全性.各种钓鱼网站,病毒等等危害着我们的网 络环境.所以,作为一个运维人员,在我们利用网络进行通信时,保证通信的机密性.完整性.可用性是必要的. 我们的日常生活中有以下三点威胁网络安全的行为: 1.威胁机密性的攻击行为,它的途径是窃听.嗅探.扫描和通信量分析 2.威胁完整性的攻击行为,它的途径是更改.伪装.重放.否认 3.威胁可用性的攻击行为,它的途径是拒绝服务 为应对以上问题,我们在技术和服务两方面提出了解决方案: 从技术上我们使用

在PHP中使用AES加密算法加密数据

原文地址: http://blog.shiniv.com/2013/11/use-aes-encryption-algorithm-to-encrypt-data-in-php/ 在研究Discuz 的时候,发现Discuz有一套相当完美的加密算法(相对而言).这个算法可以将数据加密后,储存起来,到需要用的时候,用之前加密的秘钥将之还原. 除了这个之外,还有AES这个算法能够将数据很好的加密起来,在传输过程中不容易被破解. 在PHP中,我们必须先安装好mcrypt这个模块,并且添加相应版本的扩展

(zigbee学习总结二)Z-stack按键机制

本文是自己学习zigbee时的知识梳理. 参考书:<ZigBee技术与实训教程--基于CC2530的无线传感网技术>----姜仲.刘丹 编著 Z-stack中提供了两种方式采集按键数据:轮询方式和中断方式.轮询方式:每隔一定时间,检测按键状态,进行相应处理:中断方式:按键引起按键中断,进行相应处理.Zstack在默认情况下,使用轮询方式进行处理. 一.按键的宏定义 在HAL/include/hal_key.h中对按键进行了基本的定义: /* 中断使能和禁用*/ #define HAL_KEY_