首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

JAVA中简略调用MD5算法进行加密

2012-12-22 
JAVA中简单调用MD5算法进行加密.加密算法简单代码.引入两个包.无需知道MD5源码.package MyWebServiceJavaC

JAVA中简单调用MD5算法进行加密.
加密算法简单代码.引入两个包.无需知道MD5源码.

package MyWebServiceJavaClient;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/** * @author King_wangyao */public class MD5Main {public static byte[] getKeyedDigest(byte[] buffer, byte[] key) {        try {            MessageDigest md5 = MessageDigest.getInstance("MD5");            md5.update(buffer);            return md5.digest(key);        } catch (NoSuchAlgorithmException e) {        }        return null;    }public static String getKeyedDigest(String strSrc, String key) {        try {            MessageDigest md5 = MessageDigest.getInstance("MD5");            md5.update(strSrc.getBytes("UTF8"));                        String result="";            byte[] temp;            temp=md5.digest(key.getBytes("UTF8"));    for (int i=0; i<temp.length; i++){    result+=Integer.toHexString((0x000000ff & temp[i]) | 0xffffff00).substring(6);    }        return result;            } catch (NoSuchAlgorithmException e) {                e.printStackTrace();                }catch(Exception e)        {          e.printStackTrace();        }        return null;    }/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubString mi;        String s = "hf1000";        //第二个参数请填空字符串mi=MD5Main.getKeyedDigest(s,"");System.out.println("mi:"+mi);}}



热点排行