1 /// AES加密 2 /// 3 /// 输入的数据 4 /// 向量128位 5 /// 加密密钥 6 ///7 public static byte[] AESEncrypt(byte[] inputdata, byte[] iv, string strKey) 8 { 9 //分组加密算法 10 SymmetricAlgorithm des = Rijndael.Create();11 byte[] inputByteArray = inputdata;//得到需要加密的字节数组 12 //设置密钥及密钥向量13 des.Key = Encoding.UTF8.GetBytes(strKey.Substring(0, 32));14 des.IV = iv;15 using (MemoryStream ms = new MemoryStream())16 {17 using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))18 {19 cs.Write(inputByteArray, 0, inputByteArray.Length);20 cs.FlushFinalBlock();21 byte[] cipherBytes = ms.ToArray();//得到加密后的字节数组 22 cs.Close();23 ms.Close(); 24 return cipherBytes;25 }26 }27 }28 29 30 /// 31 /// AES解密32 /// 33 /// 输入的数据34 /// 向量12835 /// key36 ///37 public static byte[] AESDecrypt(byte[] inputdata, byte[] iv, string strKey)38 {39 SymmetricAlgorithm des = Rijndael.Create();40 des.Key = Encoding.UTF8.GetBytes(strKey.Substring(0, 32));41 des.IV = iv;42 byte[] decryptBytes = new byte[inputdata.Length];43 using (MemoryStream ms = new MemoryStream(inputdata))44 {45 using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read))46 {47 cs.Read(decryptBytes, 0, decryptBytes.Length);48 cs.Close();49 ms.Close();50 }51 }52 return decryptBytes;53 }
posted on 2013-02-20 17:47 阅读( ...) 评论( ...)