易截截图软件、单文件、免安装、纯绿色、仅160KB

java 和.net的MD5加密不一样?!

由于工作需要,要用.net的程序调用一个java编写的web service接口,接口的一个参数要求md5方式加密。
.net中的md5加密是很容易的,采用msdn中给出的方法:
// 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.
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
// 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();
}
 
可是,当输入的字符串是中文的时候,问题出现了,加密出来的字符串不一样:
输入 工作
java 8c87df93416ebdf251bcb6b328ec3c3b
.net 12e709e3a9a82b441b739a5c8ba035de
首先想到的是对中文的编码方式不一样,可是无论用utf8还是unicode结果都不同。
用google搜索 “java MD5 算法”找到的一篇文章解释了我的疑惑
http://blog.csdn.net/Peter_K/archive/2007/03/13/1527629.aspx
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray); 
在.net中用同样的方式
private string getMd5Hash(string input)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
// Convert the input string to a byte array and compute the hash.
char[] temp = input.ToCharArray();


相关文档:

java 调用命令 备份mysql数据库

 代码如下:
  1  String command  =   " cmd /c C:/Program Files/MySQL/MySQL Server 5.0/bin>mysqldump -h localhost -u root -p aijia > E:/aijia.dmp " ;
  2      try    {
  3     Process process& ......

java中类的限定词的作用范围比较

public(公共访问控制):可以被所有类访问。
protected(保护访问控制):可以被这个类本身、它的子类(包括同一个包中以及不同包中的子类)和同一个包中的所有其他类访问。
default(友好访问控制):可以被这个类本身和同一个包中的其他类所访问。
private(私有访问控制):只能被该类自身访问和修改。 ......

Java学习心得(转自张国宝)


1.    数组有没有length()这个方法? String有没有length()这个方法?
        答:数组没有length()这个方法,有length的属性。
        String有length()这个方法。
2.     String s = new String("xyz") ......

java profiler

Dear han luoxing,
Thank you for trying YourKit Java Profiler.
Please find your personal evaluation license key below.
With this key, you can evaluate YourKit Java Profiler for 15 days, starting today.
When asked to provide license key, copy and paste the following entire block of lines:
13c970a ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号