c#中的有关hash加密算法 myAssembly应该是指程序集。字符加密无需转换成‘这个类型’(否则,有很特别的用法?) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我的例子,希望对你有帮助:namespace Test {using System.Security.Cryptography;using System.IO;public class Crypto { public static string EncryptString(string src) { if (src.Length==0) return ""; byte[] HaKey = System.Text.Encoding.ASCII.GetBytes((src+"Test").ToCharArray()); byte[] HaData = new byte[20]; HMACSHA1 Hmac = new HMACSHA1(HaKey); CryptoStream cs = new CryptoStream(Stream.Null, Hmac, CryptoStreamMode.Write); try { cs.Write(HaData, 0, HaData.Length); } finally { cs.Close(); } string HaResult = System.Convert.ToBase64String(Hmac.Hash).Substring(0,16); byte[] RiKey = System.Text.Encoding.ASCII.GetBytes(HaResult.ToCharArray()); byte[] RiDataBuf = System.Text.Encoding.ASCII.GetBytes(src.ToCharArray()); byte[] EncodedBytes = {}; MemoryStream ms = new MemoryStream(); RijndaelManaged rv = new RijndaelManaged(); cs = new CryptoStream(ms, rv.CreateEncryptor(RiKey, RiKey), CryptoStreamMode.Write); try { cs.Write(RiDataBuf, 0, RiDataBuf.Length); cs.FlushFinalBlock(); EncodedBytes = ms.ToArray(); } finally { ms.Close(); cs.Close(); } return HaResult+System.Convert.ToBase64String(EncodedBytes); } public static string DecryptString(string src) { if (src.Length<40) return ""; byte[] SrcBytes = System.Convert.FromBase64String(src.Substring(16)); byte[] RiKey = System.Text.Encoding.ASCII.GetBytes(src.Substring(0,16).ToCharArray()); byte[] InitialText = new byte[SrcBytes.Length]; RijndaelManaged rv = new RijndaelManaged(); MemoryStream ms = new MemoryStream(SrcBytes); CryptoStream cs = new CryptoStream(ms, rv.CreateDecryptor(RiKey, RiKey), CryptoStreamMode.Read); try { cs.Read(InitialText, 0, InitialText.Length); } finally { ms.Close(); cs.Close(); } System.Text.StringBuilder Result = new System.Text.StringBuilder(); for(int i=0; i < InitialText.Length; ++i) if (InitialText[i]>0) Result.Append((char) InitialText[i]); return Result.ToString(); } public static string Encode(string s) { string e = EncryptString(s); return ((e.Length>16) ? e.Substring(16) : ""); }}} hashtable 排序问题 【求帮助】懂C#和WPF的来帮下忙,关于RichTextBox控件鼠标悬停的问题 关于GridView的编辑问题。 问一个很弱智的问题 C# 上传文件问题 C# 如何删除计算机内指定证书 如何实现对于TreeView中的不同树节点有不同的右键菜单,如何实现(等ing online) 请帮我把这个C#代码转成C的! ===求二个简单的正则表达式====(解决立刻结帖) 急!!!!!!!!!!!!!!! 求救:一个access数据插入的问题 高分求救~
namespace Test {using System.Security.Cryptography;
using System.IO;public class Crypto { public static string EncryptString(string src) { if (src.Length==0) return ""; byte[] HaKey = System.Text.Encoding.ASCII.GetBytes((src+"Test").ToCharArray()); byte[] HaData = new byte[20]; HMACSHA1 Hmac = new HMACSHA1(HaKey);
CryptoStream cs = new CryptoStream(Stream.Null, Hmac, CryptoStreamMode.Write);
try {
cs.Write(HaData, 0, HaData.Length);
} finally {
cs.Close();
} string HaResult = System.Convert.ToBase64String(Hmac.Hash).Substring(0,16);
byte[] RiKey = System.Text.Encoding.ASCII.GetBytes(HaResult.ToCharArray()); byte[] RiDataBuf = System.Text.Encoding.ASCII.GetBytes(src.ToCharArray());
byte[] EncodedBytes = {}; MemoryStream ms = new MemoryStream();
RijndaelManaged rv = new RijndaelManaged(); cs = new CryptoStream(ms, rv.CreateEncryptor(RiKey, RiKey), CryptoStreamMode.Write); try {
cs.Write(RiDataBuf, 0, RiDataBuf.Length);
cs.FlushFinalBlock();
EncodedBytes = ms.ToArray();
} finally {
ms.Close();
cs.Close();
} return HaResult+System.Convert.ToBase64String(EncodedBytes);
} public static string DecryptString(string src) { if (src.Length<40) return ""; byte[] SrcBytes = System.Convert.FromBase64String(src.Substring(16));
byte[] RiKey = System.Text.Encoding.ASCII.GetBytes(src.Substring(0,16).ToCharArray()); byte[] InitialText = new byte[SrcBytes.Length]; RijndaelManaged rv = new RijndaelManaged();
MemoryStream ms = new MemoryStream(SrcBytes);
CryptoStream cs = new CryptoStream(ms, rv.CreateDecryptor(RiKey, RiKey), CryptoStreamMode.Read); try {
cs.Read(InitialText, 0, InitialText.Length);
} finally {
ms.Close();
cs.Close();
} System.Text.StringBuilder Result = new System.Text.StringBuilder();
for(int i=0; i < InitialText.Length; ++i) if (InitialText[i]>0) Result.Append((char) InitialText[i]); return Result.ToString();
} public static string Encode(string s) {
string e = EncryptString(s);
return ((e.Length>16) ? e.Substring(16) : "");
}}}