具体怎么加密?
是不是要调用个什么东西啊
是不是要调用个什么东西啊
解决方案 »
- C/S怎么做系统在线升级呀!?(急/求助)
- 关于picturbox 与imagelist
- 简单问题:如何通过WEB控件的改变触发改变TEXTBOX里文字的颜色
- c# app.exe.config文件与app.config的关系
- 用C#写一段代码,使用递归算法从一个数组中找出最大值和最小值.
- 关于vs2005中的不能加载外接程序的问题
- JScript问题
- .net c#反射的问题
- 有人下载过"Microsoft_SharePoint Portal Web Server中文版"这个软件安装用过吗?
- 改Datagrid的标题!
- c# windows程序 用户控件如何访问父窗口函数
- 询问一个正则表达的问题
也可以用DES的方式,这是专门给字符串加密的(using System.Security;
using System.Security.Cryptography;
)
不过我建议你可以自己设计一个,也不是很复杂的
C#里面加密在System.Security.Cryptogrphy命名空间里。
DESCryptoServiceProvider进行DES加密。
RSACryptoServiceProvider进行RSA加密。
MD5CryptoServiceProvider进行MD5加密。
具体代码怎么写,请参考MSDN,有例子
encryptstring 需要加密的字符串 函数返回值为加密过后的字符串encryptkey 为密钥
Decryptkey 同上
他们是多少位,请参考msdn
public string DESEncrypt(string encryptstring)
{
string strRtn;
try
{
// DESCryptoServiceProvider desc = new DESCryptoServiceProvider();//des进行加密
RijndaelManaged alg = new RijndaelManaged();
byte[] rgbKey = System.Text.Encoding.Unicode.GetBytes(encryptkey);
byte[] rgbIV = System.Text.Encoding.Unicode.GetBytes(Decryptkey);
// MessageBox.Show(rgbKey.Length.ToString(), rgbIV.Length.ToString());
byte[] data = System.Text.Encoding.Unicode.GetBytes(encryptstring);
// alg.Key = rgbKey;
// alg.IV = rgbIV;
MemoryStream ms = new MemoryStream();//存储加密后的数据
CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cs.Write(data, 0, data.Length);//进行加密
cs.FlushFinalBlock();
strRtn = Convert.ToBase64String(ms.ToArray());
return strRtn;
} catch (Exception ex)
{
MessageBox.Show("错误:" + ex.Message, "错误消息提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
return null;
}
}数据解密public string DESDecrypt(string decryptstring)
{
string strRtn;
try
{
// DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
RijndaelManaged alg = new RijndaelManaged();
byte[] rgbKey = System.Text.Encoding.Unicode.GetBytes(encryptkey);
byte[] rgbIV = System.Text.Encoding.Unicode.GetBytes(Decryptkey);
byte[] data = Convert.FromBase64String(decryptstring);
// alg.Key = rgbKey;
// alg.IV = rgbIV;
MemoryStream ms = new MemoryStream();//存储解密后的数据
CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cs.Write(data, 0, data.Length);//解密数据
cs.FlushFinalBlock();
strRtn = System.Text.Encoding.Unicode.GetString(ms.ToArray());
return strRtn;
}
catch (Exception ex)
{
MessageBox.Show("错误:" + ex.Message, "错误消息提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
return null;
}
}
lz 给分哈
/// <summary>
/// 字符串加密类
/// </summary>
public partial class StringEncrypt
{
#region MD5Security
/// <summary>
/// 32位MD5加密方法
/// </summary>
/// <param name="strSource">待加密字串</param>
/// <returns>加密后的字串</returns>
public static string MD5Encrypt(string strSource)
{
return MD5Encrypt(strSource, 32);
} /// <summary>
/// MD5加密
/// </summary>
/// <param name="strSource">待加密字串</param>
/// <param name="length">16或32值之一,其它则采用.Net默认MD5加密算法</param>
/// <returns>加密后的字串</returns>
public static string MD5Encrypt(string strSource, int length)
{
byte[] bytes = Encoding.Unicode.GetBytes(strSource);
byte[] hashValue = MD5.Create().ComputeHash(bytes);
StringBuilder hashString = new StringBuilder();
switch (length)
{
case 16:
for (int i = 4; i < 12; i++)
{
hashString.Append(hashValue[i].ToString("x2"));
}
break;
case 32:
for (int i = 0; i < 16; i++)
{
hashString.Append(hashValue[i].ToString("x2"));
}
break;
default:
for (int i = 0; i < hashValue.Length; i++)
{
hashString.Append(hashValue[i].ToString("x2"));
}
break;
}
return hashString.ToString();
} /// <summary>
/// MD5字符串验证
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="hash">对应的哈希码</param>
/// <returns></returns>
public static bool verifyMd5Hash(string input, string hash)
{
// Hash the input.
string hashOfInput = MD5Encrypt(input); // Create a StringComparer an comare the hashes.
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
if (0 == comparer.Compare(hashOfInput, hash))
{
return true;
}
else
{
return false;
}
}