当前位置:首页 > 网络编程 > 软件语言 > .NET > C#实现Des加密和解密

C#实现Des加密和解密

点击次数:70 次 发布日期:2008-11-06 07:54:56 作者:源代码网
源代码网推荐
广告载入中

源代码网整理以下using System;
using System.IO;
using System.Security.Cryptography;

源代码网整理以下namespace Vavic
{
/// <summary>
/// Security 的摘要说明。
/// </summary>
public class Security
{
const string KEY_64 = "VavicApp";
const string IV_64 = "VavicApp"; //注意了,是8个字符,64位 软件开发网 www.mscto.com

源代码网整理以下public Security()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

源代码网整理以下public static string Encode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

源代码网整理以下DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms,cryptoProvider.CreateEncryptor(byKey,byIV),CryptoStreamMode.Write);

源代码网整理以下StreamWriter sw = new StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer(),0,(int)ms.Length); 软件开发网 www.mscto.com

源代码网整理以下} 软件开发网 www.mscto.com

源代码网整理以下public static string Decode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

源代码网整理以下byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(data);
}
catch
{
return null;
}

源代码网整理以下DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms,cryptoProvider.CreateDecryptor(byKey,byIV),CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}
}
}


源代码网推荐

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华