c#中的有关hash加密算法

lanziqian 2003-08-20 09:49:09
用hash的sha1加密算法:
Hash hash = new Hash ( myAssembly );
Byte[] hashcode = hash.SHA1;
我想问myAssembly是什么类型的。怎样把字符转换成这个类型。
...全文
123 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lanziqian 2003-08-20
  • 打赏
  • 举报
回复
那请问字符加密要转换成那个类型呢。
dawave 2003-08-20
  • 打赏
  • 举报
回复
我的例子,希望对你有帮助:


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) : "");
}

}

}
jjcccc 2003-08-20
  • 打赏
  • 举报
回复
myAssembly应该是指程序集。字符加密无需转换成‘这个类型’(否则,有很特别的用法?)

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧