一个java的md5加密代码, 高手帮忙看看, 要弄成c++的

daviddelphilee 2008-07-28 11:28:05
不太懂java, 这个是别人写的, 对错也不知道, 对比了下c++里的md5算法, 我感觉是错的,
java高手帮忙看下, 是否正确, 是什么意思。





package util.xxx;

import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

public class HashFile {
public static char[] hexChar = {'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'};


public static String getHash(String fileName, String hashType) throws
Exception {
InputStream fis;
fis = new FileInputStream(fileName);
byte[] buffer = new byte[1024];
MessageDigest md5 = MessageDigest.getInstance(hashType);
int numRead = 0;
while ((numRead = fis.read(buffer)) > 0) {
md5.update(buffer, 0, numRead);
}
fis.close();
return toHexString(md5.digest());
}

public static String toHexString(byte[] b) {
StringBuilder sb = new StringBuilder(b.length * 2);
for (int i = 0; i < b.length; i++) {
sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
sb.append(hexChar[b[i] & 0x0f]);
}
return sb.toString();
}
public static void main(String[] args) throws
Exception {
String fileName = "F:\\onecard[1].ear.rar";
String hashType = "MD5";
System.out.println(hashType + " == " +
getHash(fileName, hashType));
}
}
...全文
479 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
gujianjun2006 2008-09-27
  • 打赏
  • 举报
回复

import java.security.*;
import java.security.spec.*;
public class MD5_Test{
public final static String MD5(String s){
char hexDigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f'};
try {
byte[] strTemp = s.getBytes();
MessageDigest mdTemp = MessageDigest.getInstance("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);
}catch (Exception e){
return null;
}
}
public static void main(String[] args){
//MD5_Test aa = new MD5_Test();
System.out.print("MD5:"+MD5_Test.MD5("gujianjun"));//这里可以换成你要加密的文本
}
}
lhmei 2008-08-20
  • 打赏
  • 举报
回复
那你直接用一个c版的md5不就可以了吗?

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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