3des用法示例,已测试

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Text;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
// The length of Encryptionstring should be 24 byte and not be a weak key
private string EncryptionString = "v3VC7LfCq6IL5KgIglqZrQ1b";
// The length of initialization vector should be 8 byte
private static Byte[] EncryptionIV = Encoding.Default.GetBytes("jaaaaaaa");
protected void Page_Load(object sender, EventArgs e)
{

}
/// <summary>
/// 加密数据
/// </summary>
/// <param name="SourceData"></param>
/// <returns></returns>
public byte[] EncryptionByteData(byte[] SourceData)
{
byte[] returnData = null;
try
{
// Create TripleDESCryptoServiceProvider object
TripleDESCryptoServiceProvider desProvider = new TripleDESCryptoServiceProvider();

// Set SecureKey and IV of desProvider
byte[] byteKey = Encoding.Default.GetBytes(EncryptionString);
desProvider.Key = byteKey;
desProvider.IV = EncryptionIV;
desProvider.Mode = CipherMode.ECB;

// A MemoryStream object
MemoryStream ms = new MemoryStream();

// Create Encryptor
ICryptoTransform encrypto = desProvider.CreateEncryptor();

// Create CryptoStream object
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);

// Encrypt SourceData
cs.Write(SourceData, 0, SourceData.Length);
cs.FlushFinalBlock();

// Get Encryption result
returnData = ms.ToArray();
}
catch (Exception ex)
{
throw ex;
}

return returnData;
}
public byte[] DecryptionByteData(byte[] SourceData)
{
byte[] returnData = null;
try
{
// Create TripleDESCryptoServiceProvider object
TripleDESCryptoServiceProvider desProvider = new TripleDESCryptoServiceProvider();

// Set SecureKey and IV of desProvider
byte[] byteKey = Encoding.Default.GetBytes(EncryptionString);
desProvider.Key = byteKey;
desProvider.IV = EncryptionIV;
desProvider.Mode = CipherMode.ECB;
// A MemoryStream object
MemoryStream ms = new MemoryStream();

// Create Decryptor
ICryptoTransform encrypto = desProvider.CreateDecryptor();

// Create CryptoStream object
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);

// Decrypt SourceData
cs.Write(SourceData, 0, SourceData.Length);
cs.FlushFinalBlock();

// Get Decryption result
returnData = ms.ToArray();
}
catch (Exception ex)
{
throw ex;
}
return returnData;

}
/// <summary>
/// Encryption method for string
/// </summary>
/// <param name="SourceData">source data</param>
/// <returns>string</returns>
public string EncryptionStringData(string SourceData)
{
try
{
// Convert source data from string to byte array
byte[] SourData = Encoding.Default.GetBytes(SourceData);

// Encrypt byte array
byte[] retData = EncryptionByteData(SourData);

// Convert encryption result from byte array to Base64String
return Convert.ToBase64String(retData, 0, retData.Length);
}
catch (Exception ex)
{
throw ex;
}
}
public string DecryptionStringdata(string SourceData)
{
try
{
// Convert source data from Base64String to byte array
byte[] SourData = Convert.FromBase64String(SourceData);

// Decrypt byte array
byte[] retData = DecryptionByteData(SourData);

// Convert Decryption result from byte array to string
return Encoding.Default.GetString(retData, 0, retData.Length);
}
catch (Exception ex)
{
throw ex;
}
}

protected void btnEncryption_Click(object sender, EventArgs e)
{
string str = EncryptionStringData("在中国这样的情况下我们应在在一工人在一了人在和一人在在在在地地");
Response.Write(str);
}
protected void btnDeCrpty_Click(object sender, EventArgs e)
{
string str = "Flvuxnn/IcNQwLrL/HA8Jfq7Qwrk6uDmSuB4lwW4n2jf9e3gPy52K7BVpiyEStG0ajnLCRERRxdrkWani6K+aA== ";
str = DecryptionStringdata(str);
Response.Write(str);
}
}

posted @ 2016-09-19 15:48  星星c#  阅读(355)  评论(0编辑  收藏  举报