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();
        }
aesmanaged

转载于:https://www.cnblogs.com/bushuosx/p/3905616.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值