在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
加密和解密常用于保护敏感的数据,比如url中的查询参数,防止被人手动修改等等。。。 /// <summary> /// DES加密(密文64位,密钥64位,IV向量64位) /// </summary> /// <param name="data"></param> /// <returns></returns> public string DesEnCryptData(string data) { const string key = "a1b2c3d4"; const string iv = "a1b2c3d4"; byte[] keyByte = Encoding.UTF8.GetBytes(key); byte[] ivByte = Encoding.UTF8.GetBytes(iv); DESCryptoServiceProvider desEncrypt = new DESCryptoServiceProvider(); MemoryStream memoryStream=new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, desEncrypt.CreateEncryptor(keyByte, ivByte), CryptoStreamMode.Write); byte[] dataByte = Encoding.UTF8.GetBytes(data); cryptoStream.Write(dataByte, 0, dataByte.Length); cryptoStream.FlushFinalBlock(); byte[] tmp = memoryStream.ToArray(); return Convert.ToBase64String(tmp); ; } /// <summary> /// DES解密 /// </summary> /// <param name="data"></param> /// <returns></returns> public string DesDeCryptData(string data) { byte[] dataByte = Convert.FromBase64String(data); MemoryStream memoryStream = new MemoryStream(dataByte); const string key = "a1b2c3d4"; const string iv = "a1b2c3d4"; byte[] keyByte = Encoding.UTF8.GetBytes(key); byte[] ivByte = Encoding.UTF8.GetBytes(iv); DESCryptoServiceProvider desEncrypt = new DESCryptoServiceProvider(); CryptoStream cryptoStream = new CryptoStream(memoryStream, desEncrypt.CreateDecryptor(keyByte, ivByte), CryptoStreamMode.Read); byte[] tmp = new byte[dataByte.Length]; cryptoStream.Read(tmp, 0, tmp.Length); return Encoding.UTF8.GetString(tmp); } /// <summary> /// Rijndael加密(密文128位,最长196位,IV向量16位,密钥16、24、32位) /// </summary> /// <param name="data"></param> /// <returns></returns> public string EncryptData(string data) { string key = "12345678123456781234567812345678"; string iv = "1234567812345678"; byte[] keyByte = Encoding.UTF8.GetBytes(key); byte[] ivByte = Encoding.UTF8.GetBytes(iv); Rijndael rijndael = Rijndael.Create(); MemoryStream mStream=new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(mStream, rijndael.CreateEncryptor(keyByte, ivByte), CryptoStreamMode.Write); byte[] dataByte = Encoding.UTF8.GetBytes(data); cryptoStream.Write(dataByte,0,dataByte.Length); cryptoStream.FlushFinalBlock(); byte[] tmp = mStream.ToArray(); return Convert.ToBase64String(tmp); } /// <summary> /// Rijndael解密 /// </summary> /// <param name="data"></param> /// <returns></returns> public string DecryptData(string data) { byte[] dataByte = Convert.FromBase64String(data); string key = "12345678123456781234567812345678"; string iv = "1234567812345678"; byte[] keyByte = Encoding.UTF8.GetBytes(key); byte[] ivByte = Encoding.UTF8.GetBytes(iv); MemoryStream memoryStream=new MemoryStream(dataByte); Rijndael rijndael = Rijndael.Create(); CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(keyByte, ivByte), CryptoStreamMode.Read); byte[] tmp=new byte[dataByte.Length]; cryptoStream.Read(tmp,0,tmp.Length); return Encoding.UTF8.GetString(tmp); }
|
请发表评论