标题:字符串加密,解密一例-->hmhz转移
取消只看楼主
tml327
Rank: 1
等 级:新手上路
帖 子:510
专家分:0
注 册:2007-10-30
 问题点数:0 回复次数:0 
字符串加密,解密一例-->hmhz转移
.net 里面提供了丰富的加密解密东东,这里只不过用了一个。

public class StringDEProvider
{
#region 内部字段

private SymmetricAlgorithm mCSP;

/// <summary>
/// 密钥|注:必须长度12
/// </summary>
private const string CIV = "MoLiQingCha=";
/// <summary>
/// 初始化向量|注:必须长度12
/// </summary>
private const string CKEY = "Xingmai0216=";

#endregion

#region 实例构造函数

public StringDEProvider()
{
mCSP = new DESCryptoServiceProvider();
}

#endregion

#region 实例方法

/// <summary>
/// 加密字符串
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public string EncryptString(string Value)
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;

ct = mCSP.CreateEncryptor(Convert.FromBase64String(CKEY), Convert.FromBase64String(CIV));

byt = Encoding.UTF8.GetBytes(Value);

ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();

cs.Close();

return Convert.ToBase64String(ms.ToArray());
}


/// <summary>
/// 解密字符串
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public string DecryptString(string Value)
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;

ct = mCSP.CreateDecryptor(Convert.FromBase64String(CKEY), Convert.FromBase64String(CIV));

byt = Convert.FromBase64String(Value);

ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();

cs.Close();

return Encoding.UTF8.GetString(ms.ToArray());
}

#endregion
}

做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
搜索更多相关主题的帖子: hmhz 字符 解密 
2007-11-09 01:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-184682-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044489 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved