VB.net 2010 视频教程 VB.net 2010 视频教程 VB.net 2010 视频教程
  • c#教程之C#代码实现对AES加密解密

  • 2018-04-24 18:09 来源:未知

新疆25选7开奖号码 www.d1dx0.cn ES(The Advanced Encryption Standard)是美国国家标准与技术研究所用于加密电子数据的规范。它被预期能成为人们公认的加密包括金融、电信和政府数字信息的方法。

本文实例为大家介绍C#实现对AES加密解密的详细代码,分享给大家供大家参考,具体内容如下

?
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
 
 
namespace AESDemo
{
 public static class AESHelper
 {
  /// <summary>
  /// AES加密
  /// </summary>
  /// <param name="Data">被加密的明文</param>
  /// <param name="Key">密钥</param>
  /// <param name="Vector">向量</param>
  /// <returns>密文</returns>
  public static Byte[] AESEncrypt(Byte[] Data, String Key, String Vector)
  {
   Byte[] bKey = new Byte[32];
   Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
   Byte[] bVector = new Byte[16];
   Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
 
 
   Byte[] Cryptograph = null; // 加密后的密文
 
 
   Rijndael Aes = Rijndael.Create();
   try
   {
    // 开辟一块内存流
    using (MemoryStream Memory = new MemoryStream())
    {
     // 把内存流对象包装成加密流对象
     using (CryptoStream Encryptor = new CryptoStream(Memory,
      Aes.CreateEncryptor(bKey, bVector),
      CryptoStreamMode.Write))
     {
      // 明文数据写入加密流
      Encryptor.Write(Data, 0, Data.Length);
      Encryptor.FlushFinalBlock();
 
 
      Cryptograph = Memory.ToArray();
     }
    }
   }
   catch
   {
    Cryptograph = null;
   }
 
 
   return Cryptograph;
  }
 
 
  /// <summary>
  /// AES解密
  /// </summary>
  /// <param name="Data">被解密的密文</param>
  /// <param name="Key">密钥</param>
  /// <param name="Vector">向量</param>
  /// <returns>明文</returns>
  public static Byte[] AESDecrypt(Byte[] Data, String Key, String Vector)
  {
   Byte[] bKey = new Byte[32];
   Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
   Byte[] bVector = new Byte[16];
   Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
 
 
   Byte[] original = null; // 解密后的明文
 
 
   Rijndael Aes = Rijndael.Create();
   try
   {
    // 开辟一块内存流,存储密文
    using (MemoryStream Memory = new MemoryStream(Data))
    {
     // 把内存流对象包装成加密流对象
     using (CryptoStream Decryptor = new CryptoStream(Memory,
     Aes.CreateDecryptor(bKey, bVector),
     CryptoStreamMode.Read))
     {
      // 明文存储区
      using (MemoryStream originalMemory = new MemoryStream())
      {
       Byte[] Buffer = new Byte[1024];
       Int32 readBytes = 0;
       while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
       {
        originalMemory.Write(Buffer, 0, readBytes);
       }
 
 
       original = originalMemory.ToArray();
      }
     }
    }
   }
   catch
   {
    original = null;
   }
 
 
   return original;
  }
 }
}
 
 
不使用向量的方式:
public static class AESCrypto
 {
/// <summary>
/// IV向量为固定值
/// </summary>
  //private static byte[] _iV = {
  // 85, 60, 12, 116,
  // 99, 189, 173, 19,
  // 138, 183, 232, 248,
  // 82, 232, 200, 242
  //};
 
 
  public static byte[] Decrypt(byte[] encryptedBytes, byte[] key)
  {
MemoryStream mStream = new MemoryStream( encryptedBytes );
//mStream.Write( encryptedBytes, 0, encryptedBytes.Length );
//mStream.Seek( 0, SeekOrigin.Begin );
RijndaelManaged aes = new RijndaelManaged( );
   aes.Mode = CipherMode.ECB;
   aes.Padding = PaddingMode.PKCS7;
   aes.KeySize = 128;
aes.Key = key;
//aes.IV = _iV;
CryptoStream cryptoStream = new CryptoStream( mStream, aes.CreateDecryptor( ), CryptoStreamMode.Read );
try {
 
 
byte[] tmp = new byte[ encryptedBytes.Length + 32 ];
int len = cryptoStream.Read( tmp, 0, encryptedBytes.Length + 32 );
byte[] ret = new byte[ len ];
Array.Copy( tmp, 0, ret, 0, len );
return ret;
}
finally {
cryptoStream.Close( );
mStream.Close( );
aes.Clear( );
}
}
 
 
  public static byte[] Encrypt(byte[] plainBytes, byte[] key)
  {
   MemoryStream mStream = new MemoryStream();
   RijndaelManaged aes = new RijndaelManaged();
 
 
   aes.Mode = CipherMode.ECB;
   aes.Padding = PaddingMode.PKCS7;
   aes.KeySize = 128;
   //aes.Key = _key;
 
 
   aes.Key = key;
   //aes.IV = _iV;
   CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
   try
   {
    cryptoStream.Write(plainBytes, 0, plainBytes.Length);
    cryptoStream.FlushFinalBlock();
    return mStream.ToArray();
   }
   finally
   {
    cryptoStream.Close();
    mStream.Close();
    aes.Clear();
   }
  }
 }

希望通过这篇文章大家对AES加密解密有所了解,对C#程序设计有所帮助。

相关教程
  • 新中国第一张彩色报纸《人民日报》 2018-12-13
  • 新编《思维学》续五;《情绪论》 2018-12-13
  • 明年起全面供应国六标准汽柴油 2018-12-12
  • 有神论跟无神论的差别在哪你都不知道,还要扯啥呢。 2018-12-12
  • 互联网与金融 回归·绿色·共享 2018-12-12
  • 中国保险监督管理委员会原主席项俊波受贿案一审开庭 2018-12-10
  • 紫光阁中共中央国家机关工作委员会 2018-12-10
  • 广州市食药监局:端午粽抽检全部合格 2018-12-10
  • 中国足球与中国股市谁最牛?请投下你神圣一票 2018-12-10
  • “碎尸案别墅”拍出后 拖欠的物业费谁付? 2018-12-09
  • 保温杯,嘴边的健康隐患 2018-12-09
  • 马来西亚总理马哈蒂尔 我要去中国看一看,感受无现金社会 2018-12-08
  • 这泼猴总给人萌萌达的感觉 2018-12-08
  • 我们的国际主义义务就是让那受苦受难的中东人去欧洲过上幸福,美好的生活。到中国来,又不自由,又不民主,茶叶蛋都吃不上,来干嘛泥? 2018-12-07
  • 一江清水何以来?(人民眼·生态补偿) 2018-12-07
  • 895| 580| 919| 532| 619| 913| 821| 438| 861| 912|