MD5

    技术2022-05-11  84

    2006-12-1 00:17:04 今天写了个简单的MD5加密程序。 程序如下: / //  //  程序描述:MD5加密 //  //  作者:略 // //  时间:2006-11-30 // // import java.security.*; import java.security.spec.*; class MD5 {        public final static String Md5 (String s)     {         //声明并初始化一个字符数组,内含16元素        //  char hexDigits[] = new char[16];        char hexDigits[] = {                        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '1', 'b', 'c', 'd', 'e', 'f'};                try         {             //待加密数据拆为字节数组             byte[] strTemp = s.getBytes ("utf-8");             //获取MessageDigest 实例,应用程序提供信息摘要算法的功能             MessageDigest mdTemp = MessageDigest.getInstance ("MD5");                 //处理             mdTemp.update (strTemp);               //处理完毕,将处理结果存入md 字节数组             byte[] md = mdTemp.digest ();               int j = md.length;                 //构造一个字符数组,长度为处理结果的2倍             char str[] = new char[j * 2];                          int k = 0;                        //遍历md,j = 16             for (int i = 0; i < j; i++)             {                 byte temp = md[i];                      str[k++] = hexDigits[temp >>>4 & 0xf];                    str[k++] = hexDigits[temp & 0xf];                }               //结束 k 正好是2j =32             return new String (str);                    }                catch (Exception e)         {                        return null;                    }            }        public static void main (String[] args)     {              System.out.println (MD5.Md5 ("XX"));         } }  

    最新回复(0)