【顶贴给分】:谁能帮忙把这段JAVA代码转换成C#的?

m777 2009-04-27 10:08:30
public final static String getMd5(String strSrc) throws Exception {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };

byte[] strTemp = strSrc.getBytes(Constant.ENC);
MessageDigest mdTemp = MessageDigest.getInstance(Constant.MD5);
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);

}
...全文
141 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sunshined 2009-04-27
  • 打赏
  • 举报
回复
可惜不会JAVA
wujinjian2008n 2009-04-27
  • 打赏
  • 举报
回复
up
blestcc 2009-04-27
  • 打赏
  • 举报
回复
直接上網找個C#的md5類不就行了
himoggy 2009-04-27
  • 打赏
  • 举报
回复
饿。。好像差不多……
huming_h 2009-04-27
  • 打赏
  • 举报
回复
2楼的方法不错。
feifeiyiwen 2009-04-27
  • 打赏
  • 举报
回复
我也是来接分的
cc123456 2009-04-27
  • 打赏
  • 举报
回复
据说顶贴有分。。。我就来了,我也是
caorenlong 2009-04-27
  • 打赏
  • 举报
回复
先顶一下
sxmonsy 2009-04-27
  • 打赏
  • 举报
回复
据说顶贴有分。。。我就来了
freewind0521 2009-04-27
  • 打赏
  • 举报
回复
up
zhazha0304 2009-04-27
  • 打赏
  • 举报
回复
我来推荐一个java视频课程在线试听:http://www.cdlanhai.com/zxst/
dengyun_1223 2009-04-27
  • 打赏
  • 举报
回复
up
LemIST 2009-04-27
  • 打赏
  • 举报
回复
up, 楼上的解答好了~~
米か臹 2009-04-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ericzhangbo1982111 的回复:]
public static String getMd5(String strSrc)
{
try
{
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };

byte[] strTemp = strSrc.getBytes(Constant.ENC);
MessageDigest mdTemp = MessageDigest.getInstance(Constant.MD5);
mdTemp.updat…
[/Quote]


这个可以
ericzhangbo1982111 2009-04-27
  • 打赏
  • 举报
回复
public string GetMD5Hash(string input)
{
System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
bs = x.ComputeHash(bs);
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (byte b in bs)
{
s.Append(b.ToString("x2").ToLower());
}
string password = s.ToString();
return password;
}
m777 2009-04-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ericzhangbo1982111 的回复:]
public static String getMd5(String strSrc)
{
try
{
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };

byte[] strTemp = strSrc.getBytes(Constant.ENC);
MessageDigest mdTemp = MessageDigest.getInstance(Constant.MD5);
mdTemp.updat…
[/Quote]

晕,似乎没法运行吧?
yangqidong 2009-04-27
  • 打赏
  • 举报
回复
public static String getMd5(String strSrc)
{
byte[] oldData = System.Text.Encoding.UTF8.GetBytes(strSrc);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] newData = md5.ComputeHash(oldData);
return System.Text.Encoding.UTF8.GetString(newData);
}
ericzhangbo1982111 2009-04-27
  • 打赏
  • 举报
回复
public static String getMd5(String strSrc)
{
try
{
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };

byte[] strTemp = strSrc.getBytes(Constant.ENC);
MessageDigest mdTemp = MessageDigest.getInstance(Constant.MD5);
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char[] str = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++)
{
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
}
catch
{

}
return new String(str);

}
soaringbird 2009-04-27
  • 打赏
  • 举报
回复
用不着上网找,.Net已经自带md5类啊

using System;
using System.Security.Cryptography;
using System.Text;

class Example
{
// Hash an input string and return the hash as
// a 32 character hexadecimal string.
static string getMd5Hash(string input)
{
// Create a new instance of the MD5CryptoServiceProvider object.
MD5 md5Hasher = MD5.Create();

// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();

// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}

// Return the hexadecimal string.
return sBuilder.ToString();
}

// Verify a hash against a string.
static bool verifyMd5Hash(string input, string hash)
{
// Hash the input.
string hashOfInput = getMd5Hash(input);

// Create a StringComparer an comare the hashes.
StringComparer comparer = StringComparer.OrdinalIgnoreCase;

if (0 == comparer.Compare(hashOfInput, hash))
{
return true;
}
else
{
return false;
}
}


static void Main()
{
string source = "Hello World!";

string hash = getMd5Hash(source);

Console.WriteLine("The MD5 hash of " + source + " is: " + hash + ".");

Console.WriteLine("Verifying the hash...");

if (verifyMd5Hash(source, hash))
{
Console.WriteLine("The hashes are the same.");
}
else
{
Console.WriteLine("The hashes are not same.");
}

}
}
// This code example produces the following output:
//
// The MD5 hash of Hello World! is: ed076287532e86365e841e92bfc50d8c.
// Verifying the hash...
// The hashes are the same.
hm020 2009-04-27
  • 打赏
  • 举报
回复
前来接分
加载更多回复(1)

111,126

社区成员

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

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

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