<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 斌哥 閱讀(1992) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    導航

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

    統計

    常用鏈接

    留言簿(1)

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲一级二级三级不卡| 风间由美在线亚洲一区| 在线观看免费a∨网站| 亚洲影视自拍揄拍愉拍| 91成人免费在线视频| 色五月五月丁香亚洲综合网| 亚洲真人无码永久在线| 久久久久久久91精品免费观看| 亚洲va无码va在线va天堂| 在线视频观看免费视频18| 一级毛片免费不卡| 亚洲视频中文字幕| 免费观看午夜在线欧差毛片| a在线免费观看视频| 国产成人精品日本亚洲网址| 亚洲精品综合久久| 搡女人真爽免费视频大全| 久久精品无码免费不卡| wwwxxx亚洲| 亚洲精品国产品国语在线| 毛片a级毛片免费播放100| 国产中文字幕在线免费观看| 在线观看亚洲AV每日更新无码| 亚洲天堂在线视频| 成年女人免费视频播放77777| 一级人做人a爰免费视频| 亚洲六月丁香婷婷综合| 亚洲精品无码久久久久去q| 国产成人精品高清免费| 久久久精品免费视频| 男女男精品网站免费观看 | 亚洲视频在线观看| 一区国严二区亚洲三区| 麻豆一区二区免费播放网站| 可以免费观看的毛片| 免费观看无遮挡www的视频| 亚洲综合色一区二区三区| 亚洲s色大片在线观看| 免费在线观看黄网站| 成年女人看片免费视频播放器| 中国一级特黄的片子免费 |