`
温柔一刀
  • 浏览: 857498 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

加密算法的java实现源码

    博客分类:
  • java
阅读更多
java 代码
  1. package com.ctgusec.bean;   
  2.   
  3. /** */  
  4. /**  
  5.  *   
  6.  * @author zhupan  
  7.  * @version 1.0  
  8.  */  
  9. public class MD5 {   
  10.     /**//*  
  11.          * 下面这些S11-S44实际上是一个4*4的矩阵, 这里把它们实现成为static final是表示了只读,切能在同一个进程空间内的多个  
  12.          * Instance间共享  
  13.          */  
  14.     static final int S11 = 7;   
  15.   
  16.     static final int S12 = 12;   
  17.   
  18.     static final int S13 = 17;   
  19.   
  20.     static final int S14 = 22;   
  21.   
  22.     static final int S21 = 5;   
  23.   
  24.     static final int S22 = 9;   
  25.   
  26.     static final int S23 = 14;   
  27.   
  28.     static final int S24 = 20;   
  29.   
  30.     static final int S31 = 4;   
  31.   
  32.     static final int S32 = 11;   
  33.   
  34.     static final int S33 = 16;   
  35.   
  36.     static final int S34 = 23;   
  37.   
  38.     static final int S41 = 6;   
  39.   
  40.     static final int S42 = 10;   
  41.   
  42.     static final int S43 = 15;   
  43.   
  44.     static final int S44 = 21;   
  45.   
  46.     static final byte[] PADDING = { -128000000000000,   
  47.             0000000000000000000000,   
  48.             0000000000000000000000,   
  49.             0000000 };   
  50.   
  51.     /**//*  
  52.          * 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中 被定义到MD5_CTX结构中  
  53.          *   
  54.          */  
  55.     private long[] state = new long[4]; // state (ABCD)   
  56.   
  57.     private long[] count = new long[2]; // number of bits, modulo 2^64 (lsb   
  58.   
  59.     // first)   
  60.   
  61.     private byte[] buffer = new byte[64]; // input buffer   
  62.   
  63.     /**//*  
  64.          * digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的 16进制ASCII表示.  
  65.          */  
  66.     public String digestHexStr;   
  67.   
  68.     /**//*  
  69.          * digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值.  
  70.          */  
  71.     private byte[] digest = new byte[16];   
  72.   
  73.     /**//*  
  74.          * getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串  
  75.          * 返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的.  
  76.          */  
  77.     public String getMD5ofStr(String inbuf) {   
  78.         md5Init();   
  79.         md5Update(inbuf.getBytes(), inbuf.length());   
  80.         md5Final();   
  81.         digestHexStr = "";   
  82.         for (int i = 0; i < 16; i++) {   
  83.             digestHexStr += byteHEX(digest[i]);   
  84.         }   
  85.         return digestHexStr;   
  86.   
  87.     }   
  88.   
  89.     // 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数   
  90.     public MD5() {   
  91.         md5Init();   
  92.   
  93.         return;   
  94.     }   
  95.   
  96.     /**//* md5Init是一个初始化函数,初始化核心变量,装入标准的幻数 */  
  97.     private void md5Init() {   
  98.         count[0] = 0L;   
  99.         count[1] = 0L;   
  100.         // /* Load magic initialization constants.   
  101.   
  102.         state[0] = 0x67452301L;   
  103.         state[1] = 0xefcdab89L;   
  104.         state[2] = 0x98badcfeL;   
  105.         state[3] = 0x10325476L;   
  106.   
  107.         return;   
  108.     }   
  109.   
  110.     /**//*   
  111.          * F, G, H ,I 是4个基本的MD5函数,在原始的MD5的C实现中,由于它们是   
  112.          * 简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们 实现成了private方法,名字保持了原来C中的。   
  113.          */   
  114.   
  115.     private long F(long x, long y, long z) {   
  116.         return (x & y) | ((~x) & z);   
  117.   
  118.     }   
  119.   
  120.     private long G(long x, long y, long z) {   
  121.         return (x & z) | (y & (~z));   
  122.   
  123.     }   
  124.   
  125.     private long H(long x, long y, long z) {   
  126.         return x ^ y ^ z;   
  127.     }   
  128.   
  129.     private long I(long x, long y, long z) {   
  130.         return y ^ (x | (~z));   
  131.     }   
  132.   
  133.     /**//*  
  134.          * FF,GG,HH和II将调用F,G,H,I进行近一步变换 FF, GG, HH, and II transformations for  
  135.          * rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent  
  136.          * recomputation.  
  137.          */  
  138.   
  139.     private long FF(long a, long b, long c, long d, long x, long s, long ac) {   
  140.         a += F(b, c, d) + x + ac;   
  141.         a = ((int) a << s) | ((int) a >>> (32 - s));   
  142.         a += b;   
  143.         return a;   
  144.     }   
  145.   
  146.     private long GG(long a, long b, long c, long d, long x, long s, long ac) {   
  147.         a += G(b, c, d) + x + ac;   
  148.         a = ((int) a << s) | ((int) a >>> (32 - s));   
  149.         a += b;   
  150.         return a;   
  151.     }   
  152.   
  153.     private long HH(long a, long b, long c, long d, long x, long s, long ac) {   
  154.         a += H(b, c, d) + x + ac;   
  155.         a = ((int) a << s) | ((int) a >>> (32 - s));   
  156.         a += b;   
  157.         return a;   
  158.     }   
  159.   
  160.     private long II(long a, long b, long c, long d, long x, long s, long ac) {   
  161.         a += I(b, c, d) + x + ac;   
  162.         a = ((int) a << s) | ((int) a >>> (32 - s));   
  163.         a += b;   
  164.         return a;   
  165.     }   
  166.   
  167.     /**//*  
  168.          * md5Update是MD5的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个  
  169.          * 函数由getMD5ofStr调用,调用之前需要调用md5init,因此把它设计成private的  
  170.          */  
  171.     private void md5Update(byte[] inbuf, int inputLen) {   
  172.   
  173.         int i, index, partLen;   
  174.         byte[] block = new byte[64];   
  175.         index = (int) (count[0] >>> 3) & 0x3F;   
  176.         // /* Update number of bits */   
  177.         if ((count[0] += (inputLen << 3)) < (inputLen << 3))   
  178.             count[1]++;   
  179.         count[1] += (inputLen >>> 29);   
  180.   
  181.         partLen = 64 - index;   
  182.   
  183.         // Transform as many times as possible.   
  184.         if (inputLen >= partLen) {   
  185.             md5Memcpy(buffer, inbuf, index, 0, partLen);   
  186.             md5Transform(buffer);   
  187.   
  188.             for (i = partLen; i + 63 < inputLen; i += 64) {   
  189.   
  190.                 md5Memcpy(block, inbuf, 0, i, 64);   
  191.                 md5Transform(block);   
  192.             }   
  193.             index = 0;   
  194.   
  195.         } else  
  196.   
  197.             i = 0;   
  198.   
  199.         // /* Buffer remaining input */   
  200.         md5Memcpy(buffer, inbuf, index, i, inputLen - i);   
  201.   
  202.     }   
  203.   
  204.     private  String MD5Front(String str)   {   
  205.   str  =  str.replace( ' : ' ,  ' z ' ).replace( ' - ' ,  ' z ' );   
  206.    return  str;   
  207.  }   
  208.   
  209.     private  String MD5after(String str)   {   
  210.   str  =  str.replace( ' 1 ' ,  ' z ' ).replace( ' 2 ' ,  ' h ' ).replace( ' 3 ' ,  ' u ' )   
  211.     .replace( ' b ' ,  ' p ' ).replace( ' c ' ,  ' n ' );   
  212.    return  str;   
  213.  }   
  214.   
  215.     /**//*  
  216.          * md5Final整理和填写输出结果  
  217.          */  
  218.     private void md5Final() {   
  219.         byte[] bits = new byte[8];   
  220.         int index, padLen;   
  221.   
  222.         // /* Save number of bits */   
  223.         Encode(bits, count, 8);   
  224.   
  225.         // /* Pad out to 56 mod 64.   
  226.         index = (int) (count[0] >>> 3) & 0x3f;   
  227.         padLen = (index < 56) ? (56 - index) : (120 - index);   
  228.         md5Update(PADDING, padLen);   
  229.   
  230.         // /* Append length (before padding) */   
  231.         md5Update(bits, 8);   
  232.   
  233.         // /* Store state in digest */   
  234.         Encode(digest, state, 16);   
  235.   
  236.     }   
  237.   
  238.     /**//*  
  239.          * md5Memcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的  
  240.          * 字节拷贝到output的outpos位置开始  
  241.          */  
  242.   
  243.     private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos,   
  244.             int len) {   
  245.         int i;   
  246.   
  247.         for (i = 0; i < len; i++)   
  248.             output[outpos + i] = input[inpos + i];   
  249.     }   
  250.   
  251.     /**//*  
  252.          * md5Transform是MD5核心变换程序,有md5Update调用,block是分块的原始字节  
  253.          */  
  254.     private void md5Transform(byte block[]) {   
  255.         long a = state[0], b = state[1], c = state[2], d = state[3];   
  256.         long[] x = new long[16];   
  257.   
  258.         Decode(x, block, 64);   
  259.   
  260.         /**//* Round 1 */  
  261.         a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /**//* 1 */  
  262.         d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /**//* 2 */  
  263.         c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /**//* 3 */  
  264.         b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /**//* 4 */  
  265.         a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /**//* 5 */  
  266.         d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /**//* 6 */  
  267.         c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /**//* 7 */  
  268.         b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /**//* 8 */  
  269.         a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /**//* 9 */  
  270.         d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /**//* 10 */  
  271.         c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /**//* 11 */  
  272.         b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /**//* 12 */  
  273.         a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /**//* 13 */  
  274.         d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /**//* 14 */  
  275.         c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /**//* 15 */  
  276.         b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /**//* 16 */  
  277.   
  278.         /**//* Round 2 */  
  279.         a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /**//* 17 */  
  280.         d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /**//* 18 */  
  281.         c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /**//* 19 */  
  282.         b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /**//* 20 */  
  283.         a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /**//* 21 */  
  284.         d = GG(d, a, b, c, x[10], S22, 0x2441453L); /**//* 22 */  
  285.         c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /**//* 23 */  
  286.         b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /**//* 24 */  
  287.         a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /**//* 25 */  
  288.         d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /**//* 26 */  
  289.         c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /**//* 27 */  
  290.         b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /**//* 28 */  
  291.         a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /**//* 29 */  
  292.         d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /**//* 30 */  
  293.         c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /**//* 31 */  
  294.         b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /**//* 32 */  
  295.   
  296.         /**//* Round 3 */  
  297.         a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /**//* 33 */  
  298.         d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /**//* 34 */  
  299.         c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /**//* 35 */  
  300.         b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /**//* 36 */  
  301.         a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /**//* 37 */  
  302.         d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /**//* 38 */  
  303.         c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /**//* 39 */  
  304.         b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /**//* 40 */  
  305.         a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /**//* 41 */  
  306.         d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /**//* 42 */  
  307.         c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /**//* 43 */  
  308.         b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /**//* 44 */  
  309.         a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /**//* 45 */  
  310.         d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /**//* 46 */  
  311.         c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /**//* 47 */  
  312.         b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /**//* 48 */  
  313.   
  314.         /**//* Round 4 */  
  315.         a = II(a, b, c, d, x[0], S41, 0xf4292244L); /**//* 49 */  
  316.         d = II(d, a, b, c, x[7], S42, 0x432aff97L); /**//* 50 */  
  317.         c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /**//* 51 */  
  318.         b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /**//* 52 */  
  319.         a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /**//* 53 */  
  320.         d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /**//* 54 */  
  321.         c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /**//* 55 */  
  322.         b = II(b, c, d, a, x[1], S44, 0x85845dd
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics