AES
AES 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法
Rijndael(读做rain-dahl)是由美国国家标准与技术协会(NIST)所选的高级加密标准(AES)的候选算法。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。算法
Rijndael 算法首先是一个密钥分组加密的算法,经过置换(permutations )和替换(substitutions)迭代加密,进过多轮操做造成密文。函数
AES算是Rijndael算法的一种特殊实现,选的分组为128bit(16字节),密钥能够使用12八、192 和 256bit三种,而Rijndael使用的密钥和区块长度能够是32位的整数倍,以128位为下限,256比特为上限。加密过程当中使用的密钥是由Rijndael密钥生成方案产生。加密
AES加密过程是在一个4×4的字节矩阵上运做,这个矩阵又称为“状态(state)”,其初值就是一个明文区块(矩阵中一个元素大小就是明文区块中的一个Byte)。(Rijndael加密法因支持更大的区块,其矩阵行数可视状况增长)加密时,各轮AES加密循环(除最后一轮外)均包含4个步骤:
AddRoundKey — 矩阵中的每个字节都与该次轮秘钥(round key)作XOR运算;每一个子密钥由密钥生成方案产生。
SubBytes — 经过非线性的替换函数,用查找表的方式把每一个字节替换成对应的字节。
ShiftRows — 将矩阵中的每一个横列进行循环式移位。
MixColumns — 为了充分混合矩阵中各个直行的操做。这个步骤使用线性转换来混合每列的四个字节。spa
RijndaelManager代码实现
-
-
using System.Collections.Generic;
-
-
using System.Security.Cryptography;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public static String AESEncrypt(String Data, String Key, String Vector)
-
-
Byte[] plainBytes = Encoding.UTF8.GetBytes(Data);
-
-
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();
-
-
-
-
using (MemoryStream Memory =
new MemoryStream())
-
-
-
using (CryptoStream Encryptor =
new CryptoStream(Memory,
-
Aes.CreateEncryptor(bKey, bVector),
-
-
-
-
Encryptor.Write(plainBytes,
0, plainBytes.Length);
-
Encryptor.FlushFinalBlock();
-
-
Cryptograph = Memory.ToArray();
-
-
-
-
-
-
-
-
-
return Convert.ToBase64String(Cryptograph);
-
-
-
-
-
-
-
-
-
-
public static String AESDecrypt(String Data, String Key, String Vector)
-
-
Byte[] encryptedBytes = Convert.FromBase64String(Data);
-
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);
-
-
-
-
Rijndael Aes = Rijndael.Create();
-
-
-
-
using (MemoryStream Memory =
new MemoryStream(encryptedBytes))
-
-
-
using (CryptoStream Decryptor =
new CryptoStream(Memory,
-
Aes.CreateDecryptor(bKey, bVector),
-
-
-
-
using (MemoryStream originalMemory =
new MemoryStream())
-
-
Byte[] Buffer =
new Byte[
1024];
-
-
while ((readBytes = Decryptor.Read(Buffer,
0, Buffer.Length)) >
0)
-
-
originalMemory.Write(Buffer,
0, readBytes);
-
-
-
original = originalMemory.ToArray();
-
-
-
-
-
-
-
-
-
return Encoding.UTF8.GetString(original);
-
-
-
-
-
-
-
-
-
-
-
public static string AESEncrypt(String Data, String Key)
-
-
MemoryStream mStream =
new MemoryStream();
-
RijndaelManaged aes =
new RijndaelManaged();
-
-
byte[] plainBytes = Encoding.UTF8.GetBytes(Data);
-
Byte[] bKey =
new Byte[
32];
-
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
-
-
aes.Mode = CipherMode.ECB;
-
aes.Padding = PaddingMode.PKCS7;
-
-
-
-
-
CryptoStream cryptoStream =
new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
-
-
-
cryptoStream.Write(plainBytes,
0, plainBytes.Length);
-
cryptoStream.FlushFinalBlock();
-
return Convert.ToBase64String(mStream.ToArray());
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public static string AESDecrypt(String Data, String Key)
-
-
Byte[] encryptedBytes = Convert.FromBase64String(Data);
-
Byte[] bKey =
new Byte[
32];
-
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
-
-
MemoryStream mStream =
new MemoryStream(encryptedBytes);
-
-
-
RijndaelManaged aes =
new RijndaelManaged();
-
aes.Mode = CipherMode.ECB;
-
aes.Padding = PaddingMode.PKCS7;
-
-
-
-
CryptoStream cryptoStream =
new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Read);
-
-
-
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 Encoding.UTF8.GetString(ret);
-
-
-
-
-
-
-
-
-
-
AesManager代码实现
-
-
-
using System.Security.Cryptography;
-
-
-
-
-
-
public static void Main()
-
-
-
-
string original =
"Here is some data to encrypt!";
-
-
-
-
-
using (AesManaged myAes =
new AesManaged())
-
-
-
byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);
-
-
-
string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
-
-
-
Console.WriteLine(
"Original: {0}", original);
-
Console.WriteLine(
"Round Trip: {0}", roundtrip);
-
-
-
-
-
-
Console.WriteLine(
"Error: {0}", e.Message);
-
-
-
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
-
-
-
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");
-
-
-
-
using (AesManaged aesAlg =
new AesManaged())
-
-
-
-
-
-
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
-
-
-
using (MemoryStream msEncrypt =
new MemoryStream())
-
-
using (CryptoStream csEncrypt =
new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
-
-
using (StreamWriter swEncrypt =
new StreamWriter(csEncrypt))
-
-
-
-
swEncrypt.Write(plainText);
-
-
encrypted = msEncrypt.ToArray();
-
-
-
-
-
-
-
-
-
-
-
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
-
-
-
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");
-
-
-
-
-
-
-
-
using (AesManaged aesAlg =
new AesManaged())
-
-
-
-
-
-
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
-
-
-
using (MemoryStream msDecrypt =
new MemoryStream(cipherText))
-
-
using (CryptoStream csDecrypt =
new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
-
-
using (StreamReader srDecrypt =
new StreamReader(csDecrypt))
-
-
-
-
-
plaintext = srDecrypt.ReadToEnd();
-
-
-
-
-
-
-
-
-
-