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