Aes加密/解密示例
2016-08-04 17:39
246 查看
直接贴出代码,如果有问题可以查看本类的示例项目本项目的git地址。
以下是静态密钥的类。
以下是动态密钥的类
以下是静态密钥的类。
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace AesEncrypt { /// <summary> /// Aes加密算法适用于固定的加解密 /// </summary> public static class AesEncrypt { /// <summary> /// 对称算法的密钥 /// </summary> private readonly static byte[] aesKey = { 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 }; /// <summary> /// 对称算法的初始化向量 /// </summary> private readonly static byte[] aesIV = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; /// <summary> /// 加密 /// </summary> /// <param name="Text">被加密的字符串</param> /// <returns>Base64</returns> public static string Encrypt(string Text) { byte[] encrypted; encrypted = EncryptStringToBytes_Aes(Text, aesKey, aesIV); return Convert.ToBase64String(encrypted); } private static byte[] EncryptStringToBytes_Aes(string 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 Aes object // with the specified key and IV. using (Aes aesAlg = Aes.Create()) { 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 (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { //Write all data to the stream. swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } } // Return the encrypted bytes from the memory stream. return encrypted; } /// <summary> /// 解密 /// </summary> /// <param name="text">被Aes加密后的Base64</param> /// <returns>字符串</returns> public static string Decrypt(string text) { string roundtrip; byte[] myByte = Convert.FromBase64String(text); roundtrip = DecryptStringFromBytes_Aes(myByte, aesKey, aesIV); return roundtrip; } private static string DecryptStringFromBytes_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. string plaintext; // Create an Aes object // with the specified key and IV. using (Aes aesAlg = Aes.Create()) { 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 (StreamReader srDecrypt = new StreamReader(csDecrypt)) { // Read the decrypted bytes from the decrypting stream // and place them in a string. plaintext = srDecrypt.ReadToEnd(); } } } } return plaintext; } } }
以下是动态密钥的类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace AesEncrypt { /// <summary> /// Aes扩展方法 /// </summary> public class AesEncryptExtension { /// <summary> /// 加密 /// </summary> /// <param name="Text">被加密的字符串</param> /// <param name="aesKey">对称算法的密钥</param> /// <param name="aesIV">设置对称算法的初始化向量</param> /// <returns>Base64</returns> public string Encrypt(string Text, byte[] aesKey, byte[] aesIV) { byte[] encrypted; encrypted = EncryptStringToBytes_Aes(Text, aesKey, aesIV); return Convert.ToBase64String(encrypted); } private byte[] EncryptStringToBytes_Aes(string 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 Aes object // with the specified key and IV. using (Aes aesAlg = Aes.Create()) { 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 (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { //Write all data to the stream. swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } } // Return the encrypted bytes from the memory stream. return encrypted; } /// <summary> /// 解密 /// </summary> /// <param name="text">被Aes加密后的Base64</param> /// <param name="aesKey">对称算法的密钥</param> /// <param name="aesIV">设置对称算法的初始化向量</param> /// <returns>字符串</returns> public string Decrypt(string text, byte[] aesKey, byte[] aesIV) { string roundtrip; byte[] myByte = Convert.FromBase64String(text); roundtrip = DecryptStringFromBytes_Aes(myByte, aesKey, aesIV); return roundtrip; } private string DecryptStringFromBytes_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. string plaintext; // Create an Aes object // with the specified key and IV. using (Aes aesAlg = Aes.Create()) { 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 (StreamReader srDecrypt = new StreamReader(csDecrypt)) { // Read the decrypted bytes from the decrypting stream // and place them in a string. plaintext = srDecrypt.ReadToEnd(); } } } } return plaintext; } } }
相关文章推荐
- PHP实现的AES双向加密解密功能示例【128位】
- java使用Hex编码解码实现Aes加密解密功能示例
- openssl aes 加密解密示例程序
- 使用Python进行AES加密和解密的示例代码
- java 下的 AES javax.crypto包 加密解密算法示例
- C#使用DES和AES实现加密解密功能示例
- java 下的 AES org.bouncycastle.crypto 包加密解密算法示例
- AES加密解密源码示例,以及解决:aes在tomcat解密中文乱码,在控制台解密中文不是乱码
- PHP实现的AES加密、解密封装类与用法示例
- Java AES加密解密示例
- Java AES 加密 解密 示例
- Java与Node.js利用AES加密解密出相同结果的方法示例
- AES加密解密->java实现
- 用Python实现AES加密和解密
- java实现AES加密解密
- AES加密和解密工具类
- Aes加密(Android与Java后台可以正常加解密 )
- AES 加密解密
- MD5加密,Base64加密/解密,AES加密/解密
- 使用RSA进行信息加密解密的WebService示例