MD5加密

 

      MD5的全称是Message-Digest Algorithm 5(信息-摘要算法),作用是让大容量信息在用数字签名软件签署私人密匙前被"压缩"成一种保密的格式(就是把一个任意长度的字节串变换成一定长的大整数)。不管是MD2、MD4还是MD5,它们都需要获得一个随机长度的信息并产生一个128位的信息摘要。虽然这些算法的结构或多或少有些相似,但MD2的设计与MD4和MD5完全不同,那是因为MD2是为8位机器做过设计优化的,而MD4和MD5却是面向32位的电脑。

 

下面这个类是md5算法的实现过程:

 

import java.lang.reflect.Array;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Encrypter {
 
 //下面是个4*4的矩阵
 static final int S11 = 7;
 static final int S12 = 12;
 static final int S13 = 17;
 static final int S14 = 22;
 static final int S21 = 5;
 static final int S22 = 9;
 static final int S23 = 14;
 static final int S24 = 20;
 static final int S31 = 4;
 static final int S32 = 11;
 static final int S33 = 16;
 static final int S34 = 23;
 static final int S41 = 6;
 static final int S42 = 10;
 static final int S43 = 15;
 static final int S44 = 21;
 static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0 };

 /**
  * 下面的三个成员是MD5计算过程中用到的3个核心数据
  */
 private long[] state = new long[4];
 private long[] count = new long[2];
 private byte[] buffer = new byte[64];
 
 //最新一次计算结果的16进制ASCII表示
 public String digestHexStr;
 
 //最新一次计算结果的2进制内部表示,表示128bit的MD5值
 private byte[] digest = new byte[16];

 private static MD5Encrypter md5 = new MD5Encrypter();
 
 //加密函数,str为需加密的字符串,返回加密过的字符串
 public String encrypt(String str){
  if(str==null){
   return null;
  }
  md5Init();
  md5Update(str.getBytes(), str.length());
  md5Final();
  this.digestHexStr = "";
  for (int i = 0; i < 16; i++) {
   this.digestHexStr += byteHEX(this.digest[i]);
  }
  return digestHexStr;
 }

 public String getMD5ofStr(String inbuf) {
  md5Init();
  md5Update(inbuf.getBytes(), inbuf.length());
  md5Final();
  this.digestHexStr = "";
  for (int i = 0; i < 8; ++i) {
   this.digestHexStr += byteHEX(this.digest[i]);
  }
  return this.digestHexStr.substring(0, 11);
 }

 public String getMD5to16(String param) throws NoSuchAlgorithmException {
  return jdkmd5(param).substring(8, 24);
 }

 public String getMD5to32(String param) throws NoSuchAlgorithmException {
  return jdkmd5(param);
 }

 private String jdkmd5(String param) throws NoSuchAlgorithmException {
  try {
   MessageDigest md5 = MessageDigest.getInstance("MD5");
   md5.update(param.getBytes());
   byte[] b = md5.digest();

   StringBuffer buf = new StringBuffer("");
   for (int offset = 0; offset < b.length; ++offset) {
    int i = b[offset];
    if (i < 0)
     i += 256;
    if (i < 16)
     buf.append("0");
    buf.append(Integer.toHexString(i));
   }
   return buf.toString();
  } catch (NoSuchAlgorithmException nsae) {
   throw nsae;
  }
 }

 private MD5Encrypter() {
  md5Init();
 }

 public static MD5Encrypter getInstance() {
  return md5;
 }

 private void md5Init() {
  this.count[0] = 0L;
  this.count[1] = 0L;

  this.state[0] = 1732584193L;
  this.state[1] = 4023233417L;
  this.state[2] = 2562383102L;
  this.state[3] = 271733878L;
 }
 
 
 /**
  * F,G,H,I 是四个基本的MD5函数
  * @param x
  * @param y
  * @param z
  * @return
  */
 private long F(long x, long y, long z) {
  return (x & y | (x ^ 0xFFFFFFFF) & z);
 }

 private long G(long x, long y, long z) {
  return (x & z | y & (z ^ 0xFFFFFFFF));
 }

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

 private long I(long x, long y, long z) {
  return (y ^ (x | z ^ 0xFFFFFFFF));
 }
 
 
 
 /**
  * FF,GG,HH,II将调用F,G,H,I进行进一步变换
  */
 private long FF(long a, long b, long c, long d, long x, long s, long ac) {
  a += F(b, c, d) + x + ac;
  a = (int) a << (int) s | (int) a >>> (int) (32L - s);
  a += b;
  return a;
 }

 private long GG(long a, long b, long c, long d, long x, long s, long ac) {
  a += G(b, c, d) + x + ac;
  a = (int) a << (int) s | (int) a >>> (int) (32L - s);
  a += b;
  return a;
 }

 private long HH(long a, long b, long c, long d, long x, long s, long ac) {
  a += H(b, c, d) + x + ac;
  a = (int) a << (int) s | (int) a >>> (int) (32L - s);
  a += b;
  return a;
 }

 private long II(long a, long b, long c, long d, long x, long s, long ac) {
  a += I(b, c, d) + x + ac;
  a = (int) a << (int) s | (int) a >>> (int) (32L - s);
  a += b;
  return a;
 }

 //MD5住计算过程
 private void md5Update(byte[] inbuf, int inputLen) {
  byte[] block = new byte[64];
  int index = (int) (this.count[0] >>> 3) & 0x3F;

  if ((this.count[0] += (inputLen << 3)) < (inputLen << 3))
   this.count[1] += 1L;
  this.count[1] += (inputLen >>> 29);

  int partLen = 64 - index;
  int i;
  if (inputLen >= partLen) {
   md5Memcpy(this.buffer, inbuf, index, 0, partLen);
   md5Transform(this.buffer);

   for (i = partLen; i + 63 < inputLen; i += 64) {
    md5Memcpy(block, inbuf, 0, i, 64);
    md5Transform(block);
   }
   index = 0;
  } else {
   i = 0;
  }

  md5Memcpy(this.buffer, inbuf, index, i, inputLen - i);
 }

 private void md5Final() {
  byte[] bits = new byte[8];

  Encode(bits, this.count, 8);

  int index = (int) (this.count[0] >>> 3) & 0x3F;
  int padLen = (index < 56) ? 56 - index : 120 - index;
  md5Update(PADDING, padLen);

  md5Update(bits, 8);

  Encode(this.digest, this.state, 16);
 }

 private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos,
   int len) {
  for (int i = 0; i < len; ++i)
   output[(outpos + i)] = input[(inpos + i)];
 }

 private void md5Transform(byte[] block) {
  long a = this.state[0];
  long b = this.state[1];
  long c = this.state[2];
  long d = this.state[3];
  long[] x = new long[16];

  Decode(x, block, 64);

  a = FF(a, b, c, d, x[0], 7L, 3614090360L);
  d = FF(d, a, b, c, x[1], 12L, 3905402710L);
  c = FF(c, d, a, b, x[2], 17L, 606105819L);
  b = FF(b, c, d, a, x[3], 22L, 3250441966L);
  a = FF(a, b, c, d, x[4], 7L, 4118548399L);
  d = FF(d, a, b, c, x[5], 12L, 1200080426L);
  c = FF(c, d, a, b, x[6], 17L, 2821735955L);
  b = FF(b, c, d, a, x[7], 22L, 4249261313L);
  a = FF(a, b, c, d, x[8], 7L, 1770035416L);
  d = FF(d, a, b, c, x[9], 12L, 2336552879L);
  c = FF(c, d, a, b, x[10], 17L, 4294925233L);
  b = FF(b, c, d, a, x[11], 22L, 2304563134L);
  a = FF(a, b, c, d, x[12], 7L, 1804603682L);
  d = FF(d, a, b, c, x[13], 12L, 4254626195L);
  c = FF(c, d, a, b, x[14], 17L, 2792965006L);
  b = FF(b, c, d, a, x[15], 22L, 1236535329L);

  a = GG(a, b, c, d, x[1], 5L, 4129170786L);
  d = GG(d, a, b, c, x[6], 9L, 3225465664L);
  c = GG(c, d, a, b, x[11], 14L, 643717713L);
  b = GG(b, c, d, a, x[0], 20L, 3921069994L);
  a = GG(a, b, c, d, x[5], 5L, 3593408605L);
  d = GG(d, a, b, c, x[10], 9L, 38016083L);
  c = GG(c, d, a, b, x[15], 14L, 3634488961L);
  b = GG(b, c, d, a, x[4], 20L, 3889429448L);
  a = GG(a, b, c, d, x[9], 5L, 568446438L);
  d = GG(d, a, b, c, x[14], 9L, 3275163606L);
  c = GG(c, d, a, b, x[3], 14L, 4107603335L);
  b = GG(b, c, d, a, x[8], 20L, 1163531501L);
  a = GG(a, b, c, d, x[13], 5L, 2850285829L);
  d = GG(d, a, b, c, x[2], 9L, 4243563512L);
  c = GG(c, d, a, b, x[7], 14L, 1735328473L);
  b = GG(b, c, d, a, x[12], 20L, 2368359562L);

  a = HH(a, b, c, d, x[5], 4L, 4294588738L);
  d = HH(d, a, b, c, x[8], 11L, 2272392833L);
  c = HH(c, d, a, b, x[11], 16L, 1839030562L);
  b = HH(b, c, d, a, x[14], 23L, 4259657740L);
  a = HH(a, b, c, d, x[1], 4L, 2763975236L);
  d = HH(d, a, b, c, x[4], 11L, 1272893353L);
  c = HH(c, d, a, b, x[7], 16L, 4139469664L);
  b = HH(b, c, d, a, x[10], 23L, 3200236656L);
  a = HH(a, b, c, d, x[13], 4L, 681279174L);
  d = HH(d, a, b, c, x[0], 11L, 3936430074L);
  c = HH(c, d, a, b, x[3], 16L, 3572445317L);
  b = HH(b, c, d, a, x[6], 23L, 76029189L);
  a = HH(a, b, c, d, x[9], 4L, 3654602809L);
  d = HH(d, a, b, c, x[12], 11L, 3873151461L);
  c = HH(c, d, a, b, x[15], 16L, 530742520L);
  b = HH(b, c, d, a, x[2], 23L, 3299628645L);

  a = II(a, b, c, d, x[0], 6L, 4096336452L);
  d = II(d, a, b, c, x[7], 10L, 1126891415L);
  c = II(c, d, a, b, x[14], 15L, 2878612391L);
  b = II(b, c, d, a, x[5], 21L, 4237533241L);
  a = II(a, b, c, d, x[12], 6L, 1700485571L);
  d = II(d, a, b, c, x[3], 10L, 2399980690L);
  c = II(c, d, a, b, x[10], 15L, 4293915773L);
  b = II(b, c, d, a, x[1], 21L, 2240044497L);
  a = II(a, b, c, d, x[8], 6L, 1873313359L);
  d = II(d, a, b, c, x[15], 10L, 4264355552L);
  c = II(c, d, a, b, x[6], 15L, 2734768916L);
  b = II(b, c, d, a, x[13], 21L, 1309151649L);
  a = II(a, b, c, d, x[4], 6L, 4149444226L);
  d = II(d, a, b, c, x[11], 10L, 3174756917L);
  c = II(c, d, a, b, x[2], 15L, 718787259L);
  b = II(b, c, d, a, x[9], 21L, 3951481745L);

  this.state[0] += a;
  this.state[1] += b;
  this.state[2] += c;
  this.state[3] += d;
 }

 private void Encode(byte[] output, long[] input, int len) {
  int i = 0;
  for (int j = 0; j < len; j += 4) {
   output[j] = (byte) (int) (input[i] & 0xFF);
   output[(j + 1)] = (byte) (int) (input[i] >>> 8 & 0xFF);
   output[(j + 2)] = (byte) (int) (input[i] >>> 16 & 0xFF);
   output[(j + 3)] = (byte) (int) (input[i] >>> 24 & 0xFF);

   ++i;
  }
 }

 private void Decode(long[] output, byte[] input, int len) {
  int i = 0;
  for (int j = 0; j < len; j += 4) {
   output[i] = (b2iu(input[j]) | b2iu(input[(j + 1)]) << 8
     | b2iu(input[(j + 2)]) << 16 | b2iu(input[(j + 3)]) << 24);

   ++i;
  }
 }

 public static long b2iu(byte b) {
  return b;
 }

 public static String byteHEX(byte ib) {
  char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
    'B', 'C', 'D', 'E', 'F' };
  char[] ob = new char[2];
  ob[0] = Digit[(ib >>> 4 & 0xF)];
  ob[1] = Digit[(ib & 0xF)];
  String s = new String(ob);
  return s;
 }

 public static void main(String[] args) {
  MD5Encrypter m = new MD5Encrypter();
  if (Array.getLength(args) == 0) {
   System.out.println("MD5 Test suite:");
   System.out.println("MD5(\"\"):" + m.getMD5ofStr(""));
   System.out.println("MD5(\"a\"):" + m.getMD5ofStr("a"));
   System.out.println("MD5(\"abc\"):" + m.getMD5ofStr("abc"));
   System.out.println("MD5(\"message digest\"):"
     + m.getMD5ofStr("message digest"));
   System.out.println("MD5(\"abcdefghijklmnopqrstuvwxyz\"):"
     + m.getMD5ofStr("abcdefghijklmnopqrstuvwxyz"));
   System.out
     .println("MD5(\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\"):"
       + m
         .getMD5ofStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"));
  } else {
   System.out
     .println("MD5(" + args[0] + ")=" + m.getMD5ofStr(args[0]));
  }
  try {
   System.out.println(m.getMD5to32("000000"));
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值