c# des方式加密解密完整代码
---------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace DecryptCode
{
public class Decrypt
{
private static readonly string DESIV = "itemperor";
/// <summary>
/// Des加密
/// </summary>
/// <param name="originalValue"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string DESEncrypt(string originalValue, string key)
{
string result;
try
{
key += "51jrft";
key = key.Substring(0, 8);
ICryptoTransform transform = new DESCryptoServiceProvider
{
Key = Encoding.UTF8.GetBytes(key),
IV = Encoding.UTF8.GetBytes(HisDecrypt.DESIV)
}.CreateEncryptor();
byte[] bytes = Encoding.UTF8.GetBytes(originalValue);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
cryptoStream.Write(bytes, 0, bytes.Length);
cryptoStream.FlushFinalBlock();
cryptoStream.Close();
result = Convert.ToBase64String(memoryStream.ToArray());
}
catch
{
result = originalValue;
}
return result;
}
/// <summary>
/// Des解密
/// </summary>
/// <param name="originalValue"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string DESDecrypt(string encryptedValue, string key)
{
string result;
try
{
key += "51jrft";
key = key.Substring(0, 8);
ICryptoTransform transform = new DESCryptoServiceProvider
{
Key = Encoding.UTF8.GetBytes(key),
IV = Encoding.UTF8.GetBytes(HisDecrypt.DESIV)
}.CreateDecryptor();
byte[] array = Convert.FromBase64String(encryptedValue);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
cryptoStream.Write(array, 0, array.Length);
cryptoStream.FlushFinalBlock();
cryptoStream.Close();
result = Encoding.UTF8.GetString(memoryStream.ToArray());
}
catch
{
result = encryptedValue;
}
return result;
}
}
}
原文链接:http://www.jxszl.com/jlmb/Others/23989.html