加密前: abcd
加密后: 856f50bbabcd加密前: the quick brown fox jumps over the lazy dog
加密后: 6d9aa89fhe quick brown fox jumps over the lazy dog加密前:消息标题
加密后:277c28c0消息标题

解决方案 »

  1.   

    MD5.encod(str);
    加密前: abcd
    加密后: E2FC714C4727EE9395F324CD2E7F331F加密前: the quick brown fox jumps over the lazy dog
    加密后: 77ADD1D5F41223D5582FCA736A5CB335加密前:消息标题
    加密后:1CDFADDC2BA413127F2431B266BCFB03
      

  2.   

    引用 5 楼 nicholas_xht 的回复:
    谁知道 用 md5怎么实现 啊 ,知道的给 满分@
      

  3.   


    public class MD5
    {
      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 };  private long[] state = new long[4];
      private long[] count = new long[2];
      private byte[] buffer = new byte[64];
      public String digestHexStr;
      private byte[] digest = new byte[16];  public String encode(String inbuf)
      {
        md5Init();
        md5Update(inbuf.getBytes(), inbuf.length());
        md5Final();
        this.digestHexStr = "";
        for (int i = 0; i < 16; ++i)
        {
          this.digestHexStr += byteHEX(this.digest[i]);
        }
        return this.digestHexStr;
      }  public MD5()
      {
        md5Init();
      }  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;
      }  private long fFunction(long x, long y, long z)
      {
        return x & y | (x ^ 0xFFFFFFFF) & z;
      }  private long gFunction(long x, long y, long z)
      {
        return x & z | y & (z ^ 0xFFFFFFFF);
      }  private long hFunction(long x, long y, long z)
      {
        return x ^ y ^ z;
      }  private long iFunction(long x, long y, long z)
      {
        return y ^ (x | z ^ 0xFFFFFFFF);
      }  private long ffFunction(long a, long b, long c, long d, long x, long s, long ac)
      {
        a += fFunction(b, c, d) + x + ac;
        a = (int)a << (int)s | (int)a >>> (int)(32L - s);
        a += b;
        return a;
      }  private long ggFunction(long a, long b, long c, long d, long x, long s, long ac)
      {
        a += gFunction(b, c, d) + x + ac;
        a = (int)a << (int)s | (int)a >>> (int)(32L - s);
        a += b;
        return a;
      }  private long hhFunction(long a, long b, long c, long d, long x, long s, long ac)
      {
        a += hFunction(b, c, d) + x + ac;
        a = (int)a << (int)s | (int)a >>> (int)(32L - s);
        a += b;
        return a;
      }  private long iiFunction(long a, long b, long c, long d, long x, long s, long ac)
      {
        a += iFunction(b, c, d) + x + ac;
        a = (int)a << (int)s | (int)a >>> (int)(32L - s);
        a += b;
        return a;
      }  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 (int 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 = ffFunction(a, b, c, d, x[0], 7L, 3614090360L);
        d = ffFunction(d, a, b, c, x[1], 12L, 3905402710L);
        c = ffFunction(c, d, a, b, x[2], 17L, 606105819L);
        b = ffFunction(b, c, d, a, x[3], 22L, 3250441966L);
        a = ffFunction(a, b, c, d, x[4], 7L, 4118548399L);
        d = ffFunction(d, a, b, c, x[5], 12L, 1200080426L);
        c = ffFunction(c, d, a, b, x[6], 17L, 2821735955L);
        b = ffFunction(b, c, d, a, x[7], 22L, 4249261313L);
        a = ffFunction(a, b, c, d, x[8], 7L, 1770035416L);
        d = ffFunction(d, a, b, c, x[9], 12L, 2336552879L);
        c = ffFunction(c, d, a, b, x[10], 17L, 4294925233L);
        b = ffFunction(b, c, d, a, x[11], 22L, 2304563134L);
        a = ffFunction(a, b, c, d, x[12], 7L, 1804603682L);
        d = ffFunction(d, a, b, c, x[13], 12L, 4254626195L);
        c = ffFunction(c, d, a, b, x[14], 17L, 2792965006L);
        b = ffFunction(b, c, d, a, x[15], 22L, 1236535329L);    a = ggFunction(a, b, c, d, x[1], 5L, 4129170786L);
        d = ggFunction(d, a, b, c, x[6], 9L, 3225465664L);
        c = ggFunction(c, d, a, b, x[11], 14L, 643717713L);
        b = ggFunction(b, c, d, a, x[0], 20L, 3921069994L);
        a = ggFunction(a, b, c, d, x[5], 5L, 3593408605L);
        d = ggFunction(d, a, b, c, x[10], 9L, 38016083L);
        c = ggFunction(c, d, a, b, x[15], 14L, 3634488961L);
        b = ggFunction(b, c, d, a, x[4], 20L, 3889429448L);
        a = ggFunction(a, b, c, d, x[9], 5L, 568446438L);
        d = ggFunction(d, a, b, c, x[14], 9L, 3275163606L);
        c = ggFunction(c, d, a, b, x[3], 14L, 4107603335L);
        b = ggFunction(b, c, d, a, x[8], 20L, 1163531501L);
        a = ggFunction(a, b, c, d, x[13], 5L, 2850285829L);
        d = ggFunction(d, a, b, c, x[2], 9L, 4243563512L);
        c = ggFunction(c, d, a, b, x[7], 14L, 1735328473L);
        b = ggFunction(b, c, d, a, x[12], 20L, 2368359562L);    a = hhFunction(a, b, c, d, x[5], 4L, 4294588738L);
        d = hhFunction(d, a, b, c, x[8], 11L, 2272392833L);
        c = hhFunction(c, d, a, b, x[11], 16L, 1839030562L);
        b = hhFunction(b, c, d, a, x[14], 23L, 4259657740L);
        a = hhFunction(a, b, c, d, x[1], 4L, 2763975236L);
        d = hhFunction(d, a, b, c, x[4], 11L, 1272893353L);
        c = hhFunction(c, d, a, b, x[7], 16L, 4139469664L);
        b = hhFunction(b, c, d, a, x[10], 23L, 3200236656L);
        a = hhFunction(a, b, c, d, x[13], 4L, 681279174L);
        d = hhFunction(d, a, b, c, x[0], 11L, 3936430074L);
        c = hhFunction(c, d, a, b, x[3], 16L, 3572445317L);
        b = hhFunction(b, c, d, a, x[6], 23L, 76029189L);
        a = hhFunction(a, b, c, d, x[9], 4L, 3654602809L);    d = hhFunction(d, a, b, c, x[12], 11L, 3873151461L);
        c = hhFunction(c, d, a, b, x[15], 16L, 530742520L);
        b = hhFunction(b, c, d, a, x[2], 23L, 3299628645L);    a = iiFunction(a, b, c, d, x[0], 6L, 4096336452L);
        d = iiFunction(d, a, b, c, x[7], 10L, 1126891415L);
        c = iiFunction(c, d, a, b, x[14], 15L, 2878612391L);
        b = iiFunction(b, c, d, a, x[5], 21L, 4237533241L);
        a = iiFunction(a, b, c, d, x[12], 6L, 1700485571L);
        d = iiFunction(d, a, b, c, x[3], 10L, 2399980690L);
        c = iiFunction(c, d, a, b, x[10], 15L, 4293915773L);
        b = iiFunction(b, c, d, a, x[1], 21L, 2240044497L);
        a = iiFunction(a, b, c, d, x[8], 6L, 1873313359L);
        d = iiFunction(d, a, b, c, x[15], 10L, 4264355552L);
        c = iiFunction(c, d, a, b, x[6], 15L, 2734768916L);
        b = iiFunction(b, c, d, a, x[13], 21L, 1309151649L);
        a = iiFunction(a, b, c, d, x[4], 6L, 4149444226L);
        d = iiFunction(d, a, b, c, x[11], 10L, 3174756917L);
        c = iiFunction(c, d, a, b, x[2], 15L, 718787259L);
        b = iiFunction(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;
      }
    }
      

  4.   

    JDK自带的Md5加密算法难道不好用吗?强烈推荐楼主试一试
      

  5.   

    彻底无语。
    lz,你不会google一下java md5啊
      

  6.   

    http://topic.csdn.net/u/20100802/19/68F750AD-EDAD-4D36-95F5-2D6442C2CD85.html#r_67414922
    看我的回答!
      

  7.   

    谁能给我看看下面 用MD5 是怎么算出来的
    加密前: abcd
    加密后: 856f50bbabcd加密前: the quick brown fox jumps over the lazy dog
    加密后: 6d9aa89fhe quick brown fox jumps over the lazy dog加密前:消息标题
    加密后:277c28c0消息标题
      

  8.   

    我不是把md5加密的类发给你了吗
    你String md5Code = new MD5().encode(str);//str为你要加密的字符串
    那么md5Code就是你加密后的字符串
    你发的那个不属于md5加密。。
    貌似是在你的字符串前面加了一段随机出来的字符串在处理
    你可以考虑将你的字符串整个用md5类加密在处理
      

  9.   

    LZ应该先去了解一下什么是MD5
    你那个根本不可能是MD5加密出来的
    MD5加密过后得到的是一个128位的byte数组
      

  10.   

    /* MD5加密类 */
    import java.security.MessageDigest;public class MD5 {
    private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    private static String byteArrayToHexString(byte[] b) { 
    StringBuffer resultSb = new StringBuffer(); 
    for (int i = 0; i < b.length; i++) { 
    resultSb.append(byteToHexString(b[i])); 

    return resultSb.toString(); 
    }  private static String byteToHexString(byte b) { 
    int n = b; 
    if (n < 0) n = 256 + n; 
    int d1 = n / 16; 
    int d2 = n % 16; 
    return hexDigits[d1] + hexDigits[d2]; 
    }  public static String MD5Encode(String origin) { 
    String resultString = null; 
    try { 
    resultString=new String(origin); 
    MessageDigest md = MessageDigest.getInstance("MD5"); 
    resultString=byteArrayToHexString(md.digest(resultString.getBytes())); 

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

    return resultString; 
    }
    }