MD5 算法的Java Bean

  1. /************************************************  
  2. MD5 算法的Java Bean  
  3. @author:killsun:170781  
  4. Last Modified:10,Mar,2002  
  5. *************************************************/   
  6. package com.HandcnCMS.utility;   
  7. import java.lang.reflect.*;   
  8. /*************************************************  
  9. md5 类实现了RSA Data Security, Inc.在提交给IETF  
  10. 的RFC1321中的MD5 message-digest 算法。  
  11. *************************************************/   
  12.    
  13. /************************************************  
  14. 引用方法:  
  15. 要import com.XXX.util.MD5;  
  16. 然后  
  17. String loginPassword;//存倒数据库中的密码  
  18. String s;//用户输入的密码  
  19. MD5 oMD5= new MD5();  
  20. String pwdmd5 = oMD5.getMD5ofStr(s);    
  21. loginPassword=pwdmd5;  
  22.   
  23. *************************************************/   
  24. public class MD5 {   
  25.  /* 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的,  
  26.  这里把它们实现成为static final是表示了只读,切能在同一个进程空间内的多个  
  27.  Instance间共享*/   
  28.         static final int S11 = 7;   
  29.         static final int S12 = 12;   
  30.         static final int S13 = 17;   
  31.         static final int S14 = 22;   
  32.    
  33.         static final int S21 = 5;   
  34.         static final int S22 = 9;   
  35.         static final int S23 = 14;   
  36.         static final int S24 = 20;   
  37.    
  38.         static final int S31 = 4;   
  39.         static final int S32 = 11;   
  40.         static final int S33 = 16;   
  41.         static final int S34 = 23;   
  42.    
  43.         static final int S41 = 6;   
  44.         static final int S42 = 10;   
  45.         static final int S43 = 15;   
  46.         static final int S44 = 21;   
  47.    
  48.         static final byte[] PADDING = { -12800000000,   
  49.         000000000000000000,   
  50.         000000000000000000,   
  51.         0000000000000000000 };   
  52.         /* 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中  
  53.            被定义到MD5_CTX结构中  
  54.           
  55.          */   
  56.         private long[] state = new long[4];  // state (ABCD)    
  57.         private long[] count = new long[2];  // number of bits, modulo 2^64 (lsb first)    
  58.         private byte[] buffer = new byte[64]; // input buffer    
  59.            
  60.  /* digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的  
  61.    16进制ASCII表示.  
  62.  */   
  63.         public String digestHexStr;   
  64.            
  65.         /* digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值.  
  66.  */   
  67.         private byte[] digest = new byte[16];   
  68.            
  69.  /*  
  70.    getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串  
  71.    返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的.  
  72.  */   
  73.         public String getMD5ofStr(String inbuf) {   
  74.                 md5Init();   
  75.                 md5Update(inbuf.getBytes(), inbuf.length());   
  76.                 md5Final();   
  77.                 digestHexStr = "";   
  78.                 for (int i = 0; i < 16; i++) {   
  79.                         digestHexStr += byteHEX(digest[i]);   
  80.                 }   
  81.                 return digestHexStr;   
  82.    
  83.         }   
  84.         // 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数    
  85.         public MD5() {   
  86.                 md5Init();   
  87.    
  88.                 return;   
  89.         }   
  90.            
  91.    
  92.    
  93.         /* md5Init是一个初始化函数,初始化核心变量,装入标准的幻数 */   
  94.         private void md5Init() {   
  95.                 count[0] = 0L;   
  96.                 count[1] = 0L;   
  97.                 ///* Load magic initialization constants.    
  98.    
  99.                 state[0] = 0x67452301L;   
  100.                 state[1] = 0xefcdab89L;   
  101.                 state[2] = 0x98badcfeL;   
  102.                 state[3] = 0x10325476L;   
  103.    
  104.                 return;   
  105.         }   
  106.         /* F, G, H ,I 是4个基本的MD5函数,在原始的MD5的C实现中,由于它们是   
  107.         简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们   
  108.        实现成了private方法,名字保持了原来C中的。 */   
  109.    
  110.         private long F(long x, long y, long z) {   
  111.                 return (x & y) | ((~x) & z);   
  112.    
  113.         }   
  114.         private long G(long x, long y, long z) {   
  115.                 return (x & z) | (y & (~z));   
  116.    
  117.         }   
  118.         private long H(long x, long y, long z) {   
  119.                 return x ^ y ^ z;   
  120.         }   
  121.    
  122.         private long I(long x, long y, long z) {   
  123.                 return y ^ (x | (~z));   
  124.         }   
  125.            
  126.        /*   
  127.           FF,GG,HH和II将调用F,G,H,I进行近一步变换  
  128.           FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.  
  129.           Rotation is separate from addition to prevent recomputation.  
  130.        */     
  131.    
  132.         private long FF(long a, long b, long c, long d, long x, long s,   
  133.                 long ac) {   
  134.                 a += F (b, c, d) + x + ac;   
  135.                 a = ((int) a << s) | ((int) a >>> (32 - s));   
  136.                 a += b;   
  137.                 return a;   
  138.         }   
  139.    
  140.         private long GG(long a, long b, long c, long d, long x, long s,   
  141.                 long ac) {   
  142.                 a += G (b, c, d) + x + ac;   
  143.                 a = ((int) a << s) | ((int) a >>> (32 - s));   
  144.                 a += b;   
  145.                 return a;   
  146.         }   
  147.         private long HH(long a, long b, long c, long d, long x, long s,   
  148.                 long ac) {   
  149.                 a += H (b, c, d) + x + ac;   
  150.                 a = ((int) a << s) | ((int) a >>> (32 - s));   
  151.                 a += b;   
  152.                 return a;   
  153.         }   
  154.         private long II(long a, long b, long c, long d, long x, long s,   
  155.                 long ac) {   
  156.                 a += I (b, c, d) + x + ac;   
  157.                 a = ((int) a << s) | ((int) a >>> (32 - s));   
  158.                 a += b;   
  159.                 return a;   
  160.         }   
  161.         /*  
  162.          md5Update是MD5的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个  
  163.          函数由getMD5ofStr调用,调用之前需要调用md5init,因此把它设计成private的  
  164.         */   
  165.         private void md5Update(byte[] inbuf, int inputLen) {   
  166.    
  167.                 int i, index, partLen;   
  168.                 byte[] block = new byte[64];   
  169.                 index = (int)(count[0] >>> 3) & 0x3F;   
  170.                 // /* Update number of bits */    
  171.    
  172. if ((count[0] += (inputLen << 3)) < (inputLen << 3))   
  173.                         count[1]++;   
  174.                 count[1] += (inputLen >>> 29);   
  175.    
  176.                 partLen = 64 - index;   
  177.    
  178.                 // Transform as many times as possible.    
  179.                 if (inputLen >= partLen) {   
  180.                         md5Memcpy(buffer, inbuf, index, 0, partLen);   
  181.                         md5Transform(buffer);   
  182.    
  183.                         for (i = partLen; i + 63 < inputLen; i += 64) {   
  184.    
  185.                                 md5Memcpy(block, inbuf, 0, i, 64);   
  186.                                 md5Transform (block);   
  187.                         }   
  188.                         index = 0;   
  189.    
  190.                 } else   
  191.    
  192.                         i = 0;   
  193.    
  194.                 ///* Buffer remaining input */    
  195.                 md5Memcpy(buffer, inbuf, index, i, inputLen - i);   
  196.    
  197.         }   
  198.            
  199.         /*  
  200.           md5Final整理和填写输出结果  
  201.         */   
  202.         private void md5Final () {   
  203.                 byte[] bits = new byte[8];   
  204.                 int index, padLen;   
  205.    
  206.                 ///* Save number of bits */    
  207.                 Encode (bits, count, 8);   
  208.    
  209.                 ///* Pad out to 56 mod 64.    
  210.                 index = (int)(count[0] >>> 3) & 0x3f;   
  211.                 padLen = (index < 56) ? (56 - index) : (120 - index);   
  212.                 md5Update (PADDING, padLen);   
  213.    
  214.                 ///* Append length (before padding) */    
  215.                 md5Update(bits, 8);   
  216.    
  217.                 ///* Store state in digest */    
  218.                 Encode (digest, state, 16);   
  219.    
  220.         }   
  221.             
  222.         /* md5Memcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的  
  223.       字节拷贝到output的outpos位置开始   
  224.         */   
  225.    
  226.         private void md5Memcpy (byte[] output, byte[] input,   
  227.                 int outpos, int inpos, int len)   
  228.         {   
  229.                 int i;   
  230.    
  231.                 for (i = 0; i < len; i++)   
  232.                         output[outpos + i] = input[inpos + i];   
  233.         }   
  234.            
  235.         /*  
  236.            md5Transform是MD5核心变换程序,有md5Update调用,block是分块的原始字节  
  237.         */   
  238.         private void md5Transform (byte block[]) {   
  239.                 long a = state[0], b = state[1], c = state[2], d = state[3];   
  240.                 long[] x = new long[16];   
  241.    
  242.                 Decode (x, block, 64);   
  243.    
  244.                 /* Round 1 */   
  245.                 a = FF (a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */   
  246.                 d = FF (d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */   
  247.                 c = FF (c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */   
  248.                 b = FF (b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */   
  249.                 a = FF (a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */   
  250.                 d = FF (d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */   
  251.                 c = FF (c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */   
  252.                 b = FF (b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */   
  253.                 a = FF (a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */   
  254.                 d = FF (d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */   
  255.                 c = FF (c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */   
  256.                 b = FF (b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */   
  257.                 a = FF (a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */   
  258.                 d = FF (d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */   
  259.                 c = FF (c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */   
  260.                 b = FF (b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */   
  261.    
  262.                 /* Round 2 */   
  263.                 a = GG (a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */   
  264.                 d = GG (d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */   
  265.                 c = GG (c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */   
  266.                 b = GG (b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */   
  267.                 a = GG (a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */   
  268.                 d = GG (d, a, b, c, x[10], S22, 0x2441453L); /* 22 */   
  269.                 c = GG (c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */   
  270.                 b = GG (b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */   
  271.                 a = GG (a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */   
  272.                 d = GG (d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */   
  273.                 c = GG (c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */   
  274.                 b = GG (b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */   
  275.                 a = GG (a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */   
  276.                 d = GG (d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */   
  277.                 c = GG (c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */   
  278.                 b = GG (b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */   
  279.    
  280.                 /* Round 3 */   
  281.                 a = HH (a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */   
  282.                 d = HH (d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */   
  283.                 c = HH (c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */   
  284.                 b = HH (b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */   
  285.                 a = HH (a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */   
  286.                 d = HH (d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */   
  287.                 c = HH (c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */   
  288.                 b = HH (b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */   
  289.                 a = HH (a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */   
  290.                 d = HH (d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */   
  291.                 c = HH (c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */   
  292.                 b = HH (b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */   
  293.                 a = HH (a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */   
  294.                 d = HH (d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */   
  295.                 c = HH (c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */   
  296.                 b = HH (b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */   
  297.    
  298.                 /* Round 4 */   
  299.                 a = II (a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */   
  300.                 d = II (d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */   
  301.                 c = II (c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */   
  302.                 b = II (b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */   
  303.                 a = II (a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */   
  304.                 d = II (d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */   
  305.                 c = II (c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */   
  306.                 b = II (b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */   
  307.                 a = II (a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */   
  308.                 d = II (d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */   
  309.                 c = II (c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */   
  310.                 b = II (b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */   
  311.                 a = II (a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */   
  312.                 d = II (d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */   
  313.                 c = II (c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */   
  314.                 b = II (b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */   
  315.    
  316.                 state[0] += a;   
  317.                 state[1] += b;   
  318.                 state[2] += c;   
  319.                 state[3] += d;   
  320.    
  321.         }   
  322.            
  323.         /*Encode把long数组按顺序拆成byte数组,因为java的long类型是64bit的,  
  324.           只拆低32bit,以适应原始C实现的用途  
  325.         */   
  326.         private void Encode (byte[] output, long[] input, int len) {   
  327.                 int i, j;   
  328.    
  329.                 for (i = 0, j = 0; j < len; i++, j += 4) {   
  330.                         output[j] = (byte)(input[i] & 0xffL);   
  331.                         output[j + 1] = (byte)((input[i] >>> 8) & 0xffL);   
  332.                         output[j + 2] = (byte)((input[i] >>> 16) & 0xffL);   
  333.                         output[j + 3] = (byte)((input[i] >>> 24) & 0xffL);   
  334.                 }   
  335.         }   
  336.    
  337.         /*Decode把byte数组按顺序合成成long数组,因为java的long类型是64bit的,  
  338.           只合成低32bit,高32bit清零,以适应原始C实现的用途  
  339.         */   
  340.         private void Decode (long[] output, byte[] input, int len) {   
  341.                 int i, j;   
  342.    
  343.    
  344.                 for (i = 0, j = 0; j < len; i++, j += 4)   
  345.                         output[i] = b2iu(input[j]) |   
  346.                                 (b2iu(input[j + 1]) << 8) |   
  347.                                 (b2iu(input[j + 2]) << 16) |   
  348.                                 (b2iu(input[j + 3]) << 24);   
  349.    
  350.                 return;   
  351.         }   
  352.           
  353.         /*  
  354.           b2iu是我写的一个把byte按照不考虑正负号的原则的"升位"程序,因为java没有unsigned运算  
  355.         */   
  356.         public static long b2iu(byte b) {   
  357.                 return b < 0 ? b & 0x7F + 128 : b;   
  358.         }   
  359.            
  360.  /*byteHEX(),用来把一个byte类型的数转换成十六进制的ASCII表示,  
  361.   因为java中的byte的toString无法实现这一点,我们又没有C语言中的  
  362.    sprintf(outbuf,"%02X",ib)  
  363.  */   
  364.         public static String byteHEX(byte ib) {   
  365.                 char[] Digit = { '0','1','2','3','4','5','6','7','8','9',   
  366.                 'a','b','c','d','e','f' };   
  367.                 char [] ob = new char[2];   
  368.                 ob[0] = Digit[(ib >>> 4) & 0X0F];   
  369.                 ob[1] = Digit[ib & 0X0F];   
  370.                 String s = new String(ob);   
  371.                 return s;   
  372.         }   
  373.    
  374.         public static void main(String args[]) {   
  375.    
  376.    
  377.                 MD5 m = new MD5();   
  378.                 if (Array.getLength(args) == 0) {   //如果没有参数,执行标准的Test Suite    
  379.                    
  380.                  System.out.println("MD5 Test suite:");   
  381.                  System.out.println("MD5(/"10/"):"+m.getMD5ofStr("355694005457784"));   
  382.                  System.out.println("MD5(/"11/"):"+m.getMD5ofStr("800922"));   
  383.                  System.out.println("MB5(/"12/"):"+m.getMD5ofStr("781214"));   
  384.                  System.out.println("MD5(/"13/"):"+m.getMD5ofStr("13"));   
  385.                  System.out.println("MD5(/"message digest/"):"+m.getMD5ofStr("message digest"));   
  386.                  System.out.println("MD5(/"abcdefghijklmnopqrstuvwxyz/"):"+   
  387.                         m.getMD5ofStr("abcdefghijklmnopqrstuvwxyz"));   
  388.                  System.out.println("MD5(/"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/"):"+   
  389.                       m.getMD5ofStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"));   
  390.                 }   
  391.                 else    
  392.                        System.out.println("MD5(" + args[0] + ")=" + m.getMD5ofStr(args[0]));   
  393.                    
  394.             
  395.         }   
  396.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值