博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AES加解密
阅读量:4634 次
发布时间:2019-06-09

本文共 4476 字,大约阅读时间需要 14 分钟。

public class AESHelper    {        ///         /// 获取密钥        ///         private static string IDKey        {            get            {                return "3vZ8rRLUME4b3K8xXovptGodOUXSCjrc";            }        }        private static string nameKey        {            get            {                return "1a54CRZ7WdwGYdNPonWkSMtHyRhlnv6q";             }        }        //默认密钥向量        private static byte[] _key1 = { 0x7f, 0x0a, 0x2d, 0x96, 0x94, 0xa5, 0xc2, 0x7b, 0xaa, 0x89, 0x00, 0x8b, 0xf3, 0xab, 0x15, 0xfd };      ///         /// AES加密算法        ///         /// 明文字符串        /// 
将加密后的密文转换为Base64编码,以便显示
public static string AESEncryptID(string plainText) { //分组加密算法 SymmetricAlgorithm aes = Rijndael.Create(); byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字节数组 //设置密钥及密钥向量 aes.Key = Encoding.UTF8.GetBytes(IDKey); aes.IV = _key1; byte[] cipherBytes = null; using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); cipherBytes = ms.ToArray();//得到加密后的字节数组 cs.Close(); ms.Close(); } } return Convert.ToBase64String(cipherBytes); } public static string AESEncryptName(string plainText) { //分组加密算法 SymmetricAlgorithm aes = Rijndael.Create(); byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字节数组 //设置密钥及密钥向量 aes.Key = Encoding.UTF8.GetBytes(nameKey); aes.IV = _key1; byte[] cipherBytes = null; using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); cipherBytes = ms.ToArray();//得到加密后的字节数组 cs.Close(); ms.Close(); } } return Convert.ToBase64String(cipherBytes); } /// /// AES解密 /// /// 密文字符串 ///
返回解密后的明文字符串
public static string AESDecryptId(string showText) { byte[] cipherText = Convert.FromBase64String(showText); SymmetricAlgorithm aes = Rijndael.Create(); aes.Key = Encoding.UTF8.GetBytes(IDKey); aes.IV = _key1; byte[] decryptBytes = new byte[cipherText.Length]; using (MemoryStream ms = new MemoryStream(cipherText)) { using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read)) { cs.Read(decryptBytes, 0, decryptBytes.Length); cs.Close(); ms.Close(); } } return Encoding.UTF8.GetString(decryptBytes).Replace("\0", ""); ///将字符串后尾的'\0'去掉 } public static string AESDecryptName(string showText) { byte[] cipherText = Convert.FromBase64String(showText); SymmetricAlgorithm aes = Rijndael.Create(); aes.Key = Encoding.UTF8.GetBytes(nameKey); aes.IV = _key1; byte[] decryptBytes = new byte[cipherText.Length]; using (MemoryStream ms = new MemoryStream(cipherText)) { using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read)) { cs.Read(decryptBytes, 0, decryptBytes.Length); cs.Close(); ms.Close(); } } return Encoding.UTF8.GetString(decryptBytes).Replace("\0", ""); ///将字符串后尾的'\0'去掉 } public static void genKey() { var generator = new RijndaelManaged(); var key = Convert.ToBase64String(generator.Key); byte[] iv = generator.IV; Console.WriteLine(key); Console.WriteLine(generator.IV); } }

   Key:为32位的字符

 byte:0-255之间的16进制数字

转载于:https://www.cnblogs.com/25miao/p/6867798.html

你可能感兴趣的文章
mysql中sql语句
查看>>
head/tail实现
查看>>
sql语句的各种模糊查询语句
查看>>
vlc 学习网
查看>>
Python20-Day05
查看>>
Real World Haskell 第七章 I/O
查看>>
C#操作OFFICE一(EXCEL)
查看>>
【js操作url参数】获取指定url参数值、取指定url参数并转为json对象
查看>>
ABAP 程序间的调用
查看>>
git分支管理
查看>>
移动端单屏解决方案
查看>>
一位资深Java架构师的晋级心得
查看>>
ass1_1
查看>>
senfile函数实例的运行过程截图
查看>>
程序编辑SHP文件并应用更改到数据源
查看>>
VS中C#读取app.config数据库配置字符串的三种方法(转)
查看>>
读取 android的内存、cpu、流量等 信息
查看>>
Python入门系列教程(三)列表和元组
查看>>
关于linux安装前规划分区二三事
查看>>
Educational Codeforces Round 39 B Weird Subtraction Process
查看>>