MD5 在开发中进行加密和解密的工具类

package cn.hnedu.encrypt;


public class MD5 {

  
/**
   * A class that implements the MD5 secure hash function.
   
*/



  
/**
   * Returns the MD5 hash digest transformed into a hex
   * representation as <code>String</code>.
   *
   * 
@return the MD5 hash of the input as <code>String</code>.
   
*/

  
public static String encoder(String str) {
    
return MD5.asHex(MD5.digest(str.getBytes()));
  }
 //hash

  
/**
   * Returns the MD5 hash of the input data.
   * <p>
   * Following the the MD5 standard specification, the result
   * is returned least significant byte first, however,
   * many applications use the most significant byte first (more conventional).
   *
   * 
@param msg the <code>byte[]</code> to be digested.
   *
   * 
@return the MD5 hash of the data as <code>String</code>.
   
*/

  
public static byte[] digest(byte[] msg) {
    
int padsize = (120 - (msg.length % 64)) % 64;
    
int i;
    
long l = msg.length * 8;

    
// the MD5 "registers"
    int a = 0x10325476, aa,
        b 
= 0xefcdab89, bb,
        c 
= 0x98badcfe, cc,
        d 
= 0x10325476, dd;

    
byte[] src = new byte[msg.length + padsize + 8];
    
/* Append Padding Bits */
    
try {
      System.arraycopy(msg, 
0, src, 0, msg.length);
    }


    
/* make compile happy even though this should never happen */
    
catch (ArrayIndexOutOfBoundsException e) {}
    
catch (ArrayStoreException e) {}

    src[msg.length] 
= (byte0x80;

    
for (i = msg.length + 1; i < msg.length + padsize; i++) src[i] = 0;

    
/* Append Length */
    
for (i = src.length - 8; i < src.length; i++{
      src[i] 
= (byte) l;
      l 
>>>= 8;
    }

    
int[] x = new int[16];
    
/* Process Message in 16-word Blocks */
    
for (i = 0; i < src.length; i += 64{
      
/* Construct block */
      
for (int j = 0; j < 16; j++{
        x[j] 
= 0;
        
for (int k = 3; k >= 0; k--{
          x[j] 
<<= 8;
          x[j] 
+= src[i + j * 4 + k] & 0xff;
          
// stupid sign-extended implicit conversion!!!
        }

      }

      aa 
= a;
      bb 
= b;
      cc 
= c;
      dd 
= d;
      a 
= round1(a, b, c, d, 071, x);
      d 
= round1(d, a, b, c, 1122, x);
      c 
= round1(c, d, a, b, 2173, x);
      b 
= round1(b, c, d, a, 3224, x);

      a 
= round1(a, b, c, d, 475, x);
      d 
= round1(d, a, b, c, 5126, x);
      c 
= round1(c, d, a, b, 6177, x);
      b 
= round1(b, c, d, a, 7228, x);

      a 
= round1(a, b, c, d, 879, x);
      d 
= round1(d, a, b, c, 91210, x);
      c 
= round1(c, d, a, b, 101711, x);
      b 
= round1(b, c, d, a, 112212, x);

      a 
= round1(a, b, c, d, 12713, x);
      d 
= round1(d, a, b, c, 131214, x);
      c 
= round1(c, d, a, b, 141715, x);
      b 
= round1(b, c, d, a, 152216, x);

      a 
= round2(a, b, c, d, 1517, x);
      d 
= round2(d, a, b, c, 6918, x);
      c 
= round2(c, d, a, b, 111419, x);
      b 
= round2(b, c, d, a, 02020, x);

      a 
= round2(a, b, c, d, 5521, x);
      d 
= round2(d, a, b, c, 10922, x);
      c 
= round2(c, d, a, b, 151423, x);
      b 
= round2(b, c, d, a, 42024, x);

      a 
= round2(a, b, c, d, 9525, x);
      d 
= round2(d, a, b, c, 14926, x);
      c 
= round2(c, d, a, b, 31427, x);
      b 
= round2(b, c, d, a, 82028, x);

      a 
= round2(a, b, c, d, 13529, x);
      d 
= round2(d, a, b, c, 2930, x);
      c 
= round2(c, d, a, b, 71431, x);
      b 
= round2(b, c, d, a, 122032, x);

      a 
= round3(a, b, c, d, 5433, x);
      d 
= round3(d, a, b, c, 81134, x);
      c 
= round3(c, d, a, b, 111635, x);
      b 
= round3(b, c, d, a, 142336, x);

      a 
= round3(a, b, c, d, 1437, x);
      d 
= round3(d, a, b, c, 41138, x);
      c 
= round3(c, d, a, b, 71639, x);
      b 
= round3(b, c, d, a, 102340, x);

      a 
= round3(a, b, c, d, 13441, x);
      d 
= round3(d, a, b, c, 01142, x);
      c 
= round3(c, d, a, b, 31643, x);
      b 
= round3(b, c, d, a, 62344, x);

      a 
= round3(a, b, c, d, 9445, x);
      d 
= round3(d, a, b, c, 121146, x);
      c 
= round3(c, d, a, b, 151647, x);
      b 
= round3(b, c, d, a, 22348, x);

      a 
= round4(a, b, c, d, 0649, x);
      d 
= round4(d, a, b, c, 71050, x);
      c 
= round4(c, d, a, b, 141551, x);
      b 
= round4(b, c, d, a, 52152, x);

      a 
= round4(a, b, c, d, 12653, x);
      d 
= round4(d, a, b, c, 31054, x);
      c 
= round4(c, d, a, b, 101555, x);
      b 
= round4(b, c, d, a, 12156, x);

      a 
= round4(a, b, c, d, 8657, x);
      d 
= round4(d, a, b, c, 151058, x);
      c 
= round4(c, d, a, b, 61559, x);
      b 
= round4(b, c, d, a, 132160, x);

      a 
= round4(a, b, c, d, 4661, x);
      d 
= round4(d, a, b, c, 111062, x);
      c 
= round4(c, d, a, b, 21563, x);
      b 
= round4(b, c, d, a, 92164, x);

      a 
+= aa;
      b 
+= bb;
      c 
+= cc;
      d 
+= dd;
    }

    
byte[] ret = new byte[16];

    
for (i = 0; i < 4; i++{
      ret[i] 
= (byte) a;
      a 
>>>= 8;
    }

    
for (; i < 8; i++{
      ret[i] 
= (byte) b;
      b 
>>>= 8;
    }

    
for (; i < 12; i++{
      ret[i] 
= (byte) c;
      c 
>>>= 8;
    }

    
for (; i < 16; i++{
      ret[i] 
= (byte) d;
      d 
>>>= 8;
    }

    
return ret;
  }
 //digest

  
/** MD5 Transformation routines *********************************************/

  
private static int rot(int x, int s) {
    
return x << s | x >>> (32 - s);
  }


  
private static int F(int x, int y, int z) {
    
return (x & y) | (~& z);
  }


  
private static int G(int x, int y, int z) {
    
return (x & z) | (y & ~z);
  }


  
private static int H(int x, int y, int z) {
    
return x ^ y ^ z;
  }


  
private static int I(int x, int y, int z) {
    
return y ^ (x | ~z);
  }


  
private static int round1(int a, int b, int c, int d, int k, int s, int i,
                            
int[] x) {
    
return b + rot( (a + F(b, c, d) + x[k] + T[i - 1]), s);
  }
 //round1

  
private static int round2(int a, int b, int c, int d, int k, int s, int i,
                            
int[] x) {
    
return b + rot( (a + G(b, c, d) + x[k] + T[i - 1]), s);
  }
 //round2

  
private static int round3(int a, int b, int c, int d, int k, int s, int i,
                            
int[] x) {
    
return b + rot( (a + H(b, c, d) + x[k] + T[i - 1]), s);
  }
 //round3

  
private static int round4(int a, int b, int c, int d, int k, int s, int i,
                            
int[] x) {
    
return b + rot( (a + I(b, c, d) + x[k] + T[i - 1]), s);
  }
 //round4

  
private static int T[] = {
      
0xd76aa4780xe8c7b7560x242070db0xc1bdceee0xf57c0faf,
      
0x4787c62a0xa83046130xfd4695010x698098d80x8b44f7af0xffff5bb1,
      
0x895cd7be0x6b9011220xfd9871930xa679438e0x49b408210xf61e2562,
      
0xc040b3400x265e5a510xe9b6c7aa0xd62f105d0x024414530xd8a1e681,
      
0xe7d3fbc80x21e1cde60xc33707d60xf4d50d870x455a14ed0xa9e3e905,
      
0xfcefa3f80x676f02d90x8d2a4c8a0xfffa39420x8771f6810x6d9d6122,
      
0xfde5380c0xa4beea440x4bdecfa90xf6bb4b600xbebfbc700x289b7ec6,
      
0xeaa127fa0xd4ef30850x04881d050xd9d4d0390xe6db99e50x1fa27cf8,
      
0xc4ac56650xf42922440x432aff970xab9423a70xfc93a0390x655b59c3,
      
0x8f0ccc920xffeff47d0x85845dd10x6fa87e4f0xfe2ce6e00xa3014314,
      
0x4e0811a10xf7537e820xbd3af2350x2ad7d2bb0xeb86d391}
;

  
/** END MD5 Transformation routines *****************************************/

  
/**
   * Returns a <code>String</code> containing unsigned hexadecimal
   * numbers as digits.
   * Contains two hex digit characters for each byte from the passed in
   * <code>byte[]</code>.
   *
   * 
@param data    the array of bytes to be converted into a hex-string.
   * 
@return    the generated hexadecimal representation as <code>String</code>.
   
*/

  
public static String asHex(byte[] data) {
    
//double size, two bytes (hex range) for one byte
    StringBuffer buf = new StringBuffer(data.length * 2);
    
for (int i = 0; i < data.length; i++{
      
//don't forget the second hex digit
      if ( ( (int) data[i] & 0xff< 0x10{
        buf.append(
"0");
      }

      buf.append(Long.toString( (
int) data[i] & 0xff16));
    }

    
return buf.toString();
  }
 //asHex



  
public static void main(String[] args) {
    
try {
      System.out.println(MD5.encoder(
"123456"));
    }

    
catch (Exception ex) {
      ex.printStackTrace();
    }

  }
 //main

}


posted on 2011-06-01 12:29 斌哥 阅读(1976) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2011年6月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

统计

常用链接

留言簿(1)

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜