宁波市建设集团股份有限公司,贵州二级站seo整站优化排名,网站不兼容怎么办,网上怎么查自己是不是团员介绍#xff1a; Rijndael 是一种对称加密算法#xff0c;也是 AES#xff08;Advanced Encryption Standard#xff09;的前身。它用于数据的加密和解密#xff0c;并提供了安全且高效的加密功能。 在.NET Framework 中#xff0c;Rijndael 类是一个实现了 Rijndael 算法…介绍 Rijndael 是一种对称加密算法也是 AESAdvanced Encryption Standard的前身。它用于数据的加密和解密并提供了安全且高效的加密功能。 在.NET Framework 中Rijndael 类是一个实现了 Rijndael 算法的具体加密器。它是 SymmetricAlgorithm 抽象类的子类之一可以用于对数据进行加密和解密操作。该算法汇聚了强安全性、高性能、高效率、易用和灵活等优点。算法支持128位16个字节、192位24个字节和256位32个字节的密钥长度。
加密代码 /// summary/// AES 加密/// /summary/// param nameencryptData待加密数据流/param/// param nameencryptKey加密密钥/param/// returns加密的数据流/returnspublic static byte[] Encrypt(byte[] encryptData, string encryptKey){if (encryptData.Length 0) { throw (new Exception(密文不得为空)); }if (string.IsNullOrEmpty(encryptKey)) { throw (new Exception(密钥不得为空)); }byte[] m_btEncrypt;//Rijndael 是一种对称加密算法也是 AESAdvanced Encryption Standard的前身。它用于数据的加密和解密并提供了安全且高效的加密功能。 在.NET Framework 中Rijndael 类是一个实现了 Rijndael 算法的具体加密器。它是 SymmetricAlgorithm 抽象类的子类之一可以用于对数据进行加密和解密操作。Rijndael m_AESProvider Rijndael.Create();try{SetKeyAndIV(m_AESProvider, encryptKey);//CryptoStream 是 .NET Framework 中的一个类用于在加密和解密过程中提供对数据流的加密或解密转换。它是 System.IO.Stream 类的派生类。CryptoStream 通过将其包装在一个输入或输出流中提供了对数据流进行加密或解密的能力。它可以与任何实现了 SymmetricAlgorithm 或 AsymmetricAlgorithm 的加密算法一起使用。using (MemoryStream m_stream new MemoryStream())using (CryptoStream m_csstream new CryptoStream(m_stream, m_AESProvider.CreateEncryptor(), CryptoStreamMode.Write)){m_csstream.Write(encryptData, 0, encryptData.Length);m_csstream.FlushFinalBlock();m_btEncrypt m_stream.ToArray();//m_stream.Close();//m_stream.Dispose();//m_csstream.Close();//m_csstream.Dispose();}}catch (IOException ex) { throw ex; }catch (CryptographicException ex) { throw ex; }catch (ArgumentException ex) { throw ex; }catch (Exception ex) { throw ex; }finally { m_AESProvider.Clear(); }return m_btEncrypt;}/// summary/// 设置密钥和初始化向量/// /summary/// param namealgorithm/param/// param namekey密钥/paramprivate static void SetKeyAndIV(SymmetricAlgorithm algorithm, string key){// 生成随机密钥和初始化向量//algorithm.GenerateKey();//algorithm.GenerateIV();byte[] m_btIV new byte[16]{2,3,73,192,68,245,71,131,2,142,97,11,230,110,216,247};byte[] m_btSalt new byte[2] { 35, 25 };//PasswordDeriveBytes 是 .NET Framework 中的一个类用于从密码派生加密密钥。它是 System.Security.Cryptography.DeriveBytes 类的子类。PasswordDeriveBytes 通过在密码和盐值salt之间进行迭代哈希操作来生成强大的加密密钥。它可用于生成符合特定算法要求的密钥例如 AES、TripleDES 和 RC2。PasswordDeriveBytes passwordDeriveBytes new PasswordDeriveBytes(key, m_btSalt);//Key是一个字节数组表示对称算法所使用的密钥。密钥的长度通常由所选的对称算法确定通常为 128 比特、192 比特或 256 比特。对于相同的密钥和相同的输入在相同的对称算法下会产生相同的输出。algorithm.Key passwordDeriveBytes.GetBytes(32);//IV 是一个字节数组表示对称算法的初始化向量。初始化向量是在加密过程中用于生成随机性的初始输入值。它与密钥一起用于变换数据并确保即使相同的输入也能得到不同的加密结果。algorithm.IV m_btIV;}
解密代码 /// summary/// AES 解密/// /summary/// param namedecryptData待解密数据流/param/// param namedecryptKey解密密钥/param/// returns解密的数据流/returnspublic static byte[] Decrypt(byte[] decryptData, string decryptKey){if (decryptData.Length 0) { throw (new Exception(密文不得为空)); }if (string.IsNullOrEmpty(decryptKey)) { throw (new Exception(密钥不得为空)); }byte[] m_btDecrypt;Rijndael m_AESProvider Rijndael.Create();try{SetKeyAndIV(m_AESProvider, decryptKey);using (MemoryStream m_stream new MemoryStream())using (CryptoStream m_csstream new CryptoStream(m_stream, m_AESProvider.CreateDecryptor(), CryptoStreamMode.Write)){m_csstream.Write(decryptData, 0, decryptData.Length);m_csstream.FlushFinalBlock();m_btDecrypt m_stream.ToArray();//m_stream.Close();//m_stream.Dispose();//m_csstream.Close();//m_csstream.Dispose();}}catch (IOException ex) { throw ex; }catch (CryptographicException ex) { throw ex; }catch (ArgumentException ex) { throw ex; }catch (Exception ex) { throw ex; }finally { m_AESProvider.Clear(); }return m_btDecrypt;}
应用示例
1、加解密字符串
string key ae125efkk4_54eeff444ferfkny6ox22666;
string encryptText 张三;
byte[] encryptData Encoding.Default.GetBytes(encryptText);byte[] btencryptResult Encrypt(encryptData, key);
Console.WriteLine(Convert.ToBase64String(btencryptResult)); //结果显示: XXXXXXXbyte[] btdecryptResult Decrypt(btencryptResult, key);
Console.WriteLine(Encoding.Default.GetString(btdecryptResult)); //结果显示: 张三
2、加解密文件
string key ae125efkk4_54eeff444ferfkny6ox22666;
//针对文件的加解密
string filePath D:\data\test.aes;
string encryptPath filePath en;
int dataSize 104096;
AESFile(filePath, encryptPath, dataSize, key, Encrypt);int deSize 104112;//Rijndael 算法也称为 AES以固定大小的数据块进行加密默认情况下为 128 位16 字节。
string decryptPath filePath de;
AESFile(encryptPath, decryptPath, deSize, key, Decrypt);
/// summary
/// AES加解密文件
/// /summary
/// param namesourcePath源文件路径/param
/// param nametarPath目标路径/param
/// param namedataSize数据流大小/param
/// param namekey密钥/param
/// param nameaction方法/param
private static void AESFile(string sourcePath, string tarPath, int dataSize, string key, Funcbyte[], string, byte[] action)
{using (FileStream fileStream new FileStream(sourcePath, FileMode.Open, FileAccess.Read)){using (FileStream fileStream2 new FileStream(tarPath, FileMode.OpenOrCreate, FileAccess.Write)){int num (int)((fileStream.Length - 1) / dataSize 1);for (int i 0; i num; i){int relSize dataSize;if (i num - 1){relSize (int)(fileStream.Length - i * dataSize);}byte[] array new byte[relSize];fileStream.Read(array, 0, relSize);byte[] array2 action?.Invoke(array, key);fileStream2.Write(array2, 0, array2.Length);fileStream2.Flush();}fileStream2.Close();fileStream2.Dispose();}}
} 黑云压城城欲摧甲光向日金鳞开。--李贺《雁门太守行》 文章转载自: http://www.morning.gypcr.cn.gov.cn.gypcr.cn http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.ypqwm.cn.gov.cn.ypqwm.cn http://www.morning.xbbrh.cn.gov.cn.xbbrh.cn http://www.morning.htpjl.cn.gov.cn.htpjl.cn http://www.morning.ppzgr.cn.gov.cn.ppzgr.cn http://www.morning.bnrnb.cn.gov.cn.bnrnb.cn http://www.morning.cbpkr.cn.gov.cn.cbpkr.cn http://www.morning.wklhn.cn.gov.cn.wklhn.cn http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.gjzwj.cn.gov.cn.gjzwj.cn http://www.morning.hhfqk.cn.gov.cn.hhfqk.cn http://www.morning.bwmm.cn.gov.cn.bwmm.cn http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.nhdmh.cn.gov.cn.nhdmh.cn http://www.morning.bflws.cn.gov.cn.bflws.cn http://www.morning.qkdcb.cn.gov.cn.qkdcb.cn http://www.morning.ryxdr.cn.gov.cn.ryxdr.cn http://www.morning.wdnkp.cn.gov.cn.wdnkp.cn http://www.morning.kbqqn.cn.gov.cn.kbqqn.cn http://www.morning.kwwkm.cn.gov.cn.kwwkm.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.hmdyl.cn.gov.cn.hmdyl.cn http://www.morning.mdxwz.cn.gov.cn.mdxwz.cn http://www.morning.nbsbn.cn.gov.cn.nbsbn.cn http://www.morning.brmbm.cn.gov.cn.brmbm.cn http://www.morning.xznrk.cn.gov.cn.xznrk.cn http://www.morning.ylkkh.cn.gov.cn.ylkkh.cn http://www.morning.dnbhd.cn.gov.cn.dnbhd.cn http://www.morning.wzwyz.cn.gov.cn.wzwyz.cn http://www.morning.rjynd.cn.gov.cn.rjynd.cn http://www.morning.qhmhz.cn.gov.cn.qhmhz.cn http://www.morning.mnjyf.cn.gov.cn.mnjyf.cn http://www.morning.qbdsx.cn.gov.cn.qbdsx.cn http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.qmbgb.cn.gov.cn.qmbgb.cn http://www.morning.ygflz.cn.gov.cn.ygflz.cn http://www.morning.hxmqb.cn.gov.cn.hxmqb.cn http://www.morning.kgphc.cn.gov.cn.kgphc.cn http://www.morning.iiunion.com.gov.cn.iiunion.com http://www.morning.rqlf.cn.gov.cn.rqlf.cn http://www.morning.wlgpz.cn.gov.cn.wlgpz.cn http://www.morning.lmnbp.cn.gov.cn.lmnbp.cn http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.c7493.cn.gov.cn.c7493.cn http://www.morning.bntgy.cn.gov.cn.bntgy.cn http://www.morning.lwcgh.cn.gov.cn.lwcgh.cn http://www.morning.wmmqf.cn.gov.cn.wmmqf.cn http://www.morning.shprz.cn.gov.cn.shprz.cn http://www.morning.tssmk.cn.gov.cn.tssmk.cn http://www.morning.mytmn.cn.gov.cn.mytmn.cn http://www.morning.fpxms.cn.gov.cn.fpxms.cn http://www.morning.rxnl.cn.gov.cn.rxnl.cn http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.ykklw.cn.gov.cn.ykklw.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.khxyx.cn.gov.cn.khxyx.cn http://www.morning.bnkcl.cn.gov.cn.bnkcl.cn http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn http://www.morning.fqqlq.cn.gov.cn.fqqlq.cn http://www.morning.rwlnk.cn.gov.cn.rwlnk.cn http://www.morning.bydpr.cn.gov.cn.bydpr.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.kkgbs.cn.gov.cn.kkgbs.cn http://www.morning.jqcrf.cn.gov.cn.jqcrf.cn http://www.morning.yzdth.cn.gov.cn.yzdth.cn http://www.morning.xhgcr.cn.gov.cn.xhgcr.cn http://www.morning.grwgw.cn.gov.cn.grwgw.cn http://www.morning.kqpxb.cn.gov.cn.kqpxb.cn http://www.morning.bssjp.cn.gov.cn.bssjp.cn http://www.morning.dzqyn.cn.gov.cn.dzqyn.cn http://www.morning.jlboyuan.cn.gov.cn.jlboyuan.cn http://www.morning.hngmg.cn.gov.cn.hngmg.cn http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.chehb.com.gov.cn.chehb.com http://www.morning.zjrnq.cn.gov.cn.zjrnq.cn http://www.morning.ljbch.cn.gov.cn.ljbch.cn http://www.morning.jkszt.cn.gov.cn.jkszt.cn http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn http://www.morning.jcrfm.cn.gov.cn.jcrfm.cn