`

JAVA MD5加密

 
阅读更多
package com.tool.udf;

import java.io.File;
import java.io.FileWriter;
import java.security.MessageDigest;

import COM.ibm.db2.app.StoredProc;

public class MD5UDF extends StoredProc {
	public int putLine(String inLine) throws Exception {
		int outCount = 0;
		try {
			outCount = inLine.length();
			File writefile = new File("TraceOut.log");
			if (writefile.exists() == false) {
				writefile.createNewFile();
				writefile = new File("TraceOut.log");
			}
			FileWriter filewriter = new FileWriter(writefile, true);
			filewriter.write("[" + "Timestamp" + "] " + inLine + "\n");
			filewriter.flush();
			filewriter.close();
		} catch (Exception d) {
			System.out.println(d.getMessage());
		}
		return outCount;
	}

	public static String MD5(String s) throws Exception {
		String s1 = new String("");
		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];
			}
			s1 = new String(str);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return s1.toUpperCase();
	}

	public static void main(String[] args) {
		MD5UDF md5udf = new MD5UDF();
		try {
			System.out.println(md5udf.MD5("1"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics