java中的几种加密方法

来源:互联网 发布:直播间网站源码 编辑:程序博客网 时间:2024/06/15 10:16
Java代码  收藏代码
  1. package com.cxlh.mm;  
  2.   
  3. public class Base64 {  
  4.     final static String baseTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";  
  5.   
  6.     /** 
  7.      * Encode a byte array.  
  8.      *  
  9.      * @param bytes a byte array to be encoded.  
  10.      * @return encoded object as a String object.  
  11.      */  
  12.     public static String encode(byte[] bytes) {  
  13.   
  14.         StringBuffer tmp = new StringBuffer();  
  15.         int i = 0;  
  16.         byte pos;   
  17.   
  18.         for(i=0; i < (bytes.length - bytes.length%3); i+=3) {  
  19.   
  20.             pos = (byte) ((bytes[i] >> 2) & 63);   
  21.             tmp.append(baseTable.charAt(pos));   
  22.   
  23.             pos = (byte) (((bytes[i] & 3) << 4) + ((bytes[i+1] >> 4) & 15));   
  24.             tmp.append(baseTable.charAt( pos ));  
  25.                       
  26.             pos = (byte) (((bytes[i+1] & 15) << 2) + ((bytes[i+2]  >> 6) & 3));  
  27.             tmp.append(baseTable.charAt(pos));  
  28.           
  29.             pos = (byte) (((bytes[i+2]) & 63));  
  30.             tmp.append(baseTable.charAt(pos));  
  31.           
  32.             // Add a new line for each 76 chars.   
  33.             // 76*3/4 = 57  
  34.             if(((i+2)%56) == 0) {  
  35.                 tmp.append("\r\n");  
  36.             }  
  37.         }  
  38.   
  39.         if(bytes.length % 3 != 0) {  
  40.   
  41.             if(bytes.length % 3 == 2) {  
  42.   
  43.                 pos = (byte) ((bytes[i] >> 2) & 63);   
  44.                 tmp.append(baseTable.charAt(pos));   
  45.   
  46.                 pos = (byte) (((bytes[i] & 3) << 4) + ((bytes[i+1] >> 4) & 15));   
  47.                 tmp.append(baseTable.charAt( pos ));  
  48.                           
  49.                 pos = (byte) ((bytes[i+1] & 15) << 2);  
  50.                 tmp.append(baseTable.charAt(pos));  
  51.               
  52.                 tmp.append("=");  
  53.   
  54.             } else if(bytes.length % 3 == 1) {  
  55.                   
  56.                 pos = (byte) ((bytes[i] >> 2) & 63);   
  57.                 tmp.append(baseTable.charAt(pos));   
  58.   
  59.                 pos = (byte) ((bytes[i] & 3) << 4);   
  60.                 tmp.append(baseTable.charAt( pos ));  
  61.                           
  62.                 tmp.append("==");  
  63.             }  
  64.         }  
  65.         return tmp.toString();  
  66.   
  67.     }  
  68.   
  69.     /** 
  70.      * Encode a String object.  
  71.      *  
  72.      * @param src a String object to be encoded with Base64 schema.  
  73.      * @return encoded String object.  
  74.      */  
  75.     public static String encode(String src) {  
  76.           
  77.         return encode(src.getBytes());    
  78.     }  
  79.   
  80.     public static byte[] decode(String src) throws Exception {  
  81.   
  82.         byte[] bytes = null;  
  83.   
  84.         StringBuffer buf = new StringBuffer(src);  
  85.   
  86.         // First, Remove white spaces (\r\n, \t, " ");  
  87.         int i = 0;  
  88.         char c = ' ';  
  89.         char oc = ' ';  
  90.         while( i < buf.length()) {             
  91.             oc = c;   
  92.             c = buf.charAt(i);  
  93.             if( oc == '\r' && c == '\n') {  
  94.                 buf.deleteCharAt(i);  
  95.                 buf.deleteCharAt(i-1);  
  96.                 i -= 2;  
  97.             } else if( c == '\t') {  
  98.                 buf.deleteCharAt(i);  
  99.                 i --;  
  100.             } else if( c == ' ') {  
  101.                 i --;  
  102.             }  
  103.             i++;  
  104.         }  
  105.   
  106.         // The source should consists groups with length of 4 chars.   
  107.         if(buf.length() % 4 != 0) {  
  108.             throw new Exception("Base64 decoding invalid length");  
  109.         }  
  110.   
  111.         // pre-set byte array size.  
  112.         bytes = new byte[3 * (buf.length() / 4)];  
  113.         //int len = 3 * (buf.length() % 4);   
  114.         //System.out.println("Size of Bytes array: " + len);  
  115.         int index = 0;  
  116.           
  117.         // Now decode each group  
  118.         for(i = 0; i < buf.length(); i+=4) {  
  119.   
  120.             byte data = 0;  
  121.             int nGroup = 0;  
  122.   
  123.             for(int j = 0; j < 4; j++) {  
  124.   
  125.                 char theChar = buf.charAt(i + j);   
  126.   
  127.                 if(theChar == '=') {  
  128.                     data = 0;  
  129.                 } else {  
  130.                     data = getBaseTableIndex(theChar);   
  131.                 }  
  132.   
  133.                 if(data == -1) {  
  134.                     throw new Exception("Base64 decoding bad character");  
  135.                 }  
  136.   
  137.                 nGroup = 64*nGroup + data;  
  138.             }  
  139.   
  140.             bytes[index] = (byte) (255 & (nGroup >> 16));  
  141.             index ++;  
  142.   
  143.             bytes[index] = (byte) (255 & (nGroup >> 8));  
  144.             index ++;  
  145.   
  146.             bytes[index] = (byte) (255 & (nGroup));  
  147.             index ++;  
  148.         }  
  149.           
  150.         byte[] newBytes = new byte[index];  
  151.         for(i = 0; i < index; i++) {  
  152.             newBytes[i] = bytes[i];  
  153.         }  
  154.   
  155.         return newBytes;  
  156.     }  
  157.   
  158.     /** 
  159.      * Find index number in base table for a given character.  
  160.      *  
  161.      */  
  162.     protected static byte getBaseTableIndex(char c) {  
  163.           
  164.         byte index = -1;  
  165.   
  166.         for(byte i = 0; i < baseTable.length(); i ++) {  
  167.           
  168.             if(baseTable.charAt(i) == c) {  
  169.                 index = i;  
  170.                 break;  
  171.             }  
  172.         }  
  173.   
  174.         return index;  
  175.     }  
  176.       
  177.     public static void main(String[] args) {  
  178.         String encodedString = Base64.encode("Hello PHPRPC".getBytes());  
  179.         System.out.println(encodedString);  
  180.     }  
  181. }  

  MD5:

Java代码  收藏代码
  1. public class MD5 {  
  2.     /* 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的, 
  3.     这里把它们实现成为static final是表示了只读,切能在同一个进程空间内的多个 
  4.     Instance间共享*/  
  5.     static final int S11 = 7;  
  6.     static final int S12 = 12;  
  7.     static final int S13 = 17;  
  8.     static final int S14 = 22;  
  9.   
  10.     static final int S21 = 5;  
  11.     static final int S22 = 9;  
  12.     static final int S23 = 14;  
  13.     static final int S24 = 20;  
  14.   
  15.     static final int S31 = 4;  
  16.     static final int S32 = 11;  
  17.     static final int S33 = 16;  
  18.     static final int S34 = 23;  
  19.   
  20.     static final int S41 = 6;  
  21.     static final int S42 = 10;  
  22.     static final int S43 = 15;  
  23.     static final int S44 = 21;  
  24.   
  25.     static final byte[] PADDING =  
  26.         {  
  27.             -128,  
  28.             0,  
  29.             0,  
  30.             0,  
  31.             0,  
  32.             0,  
  33.             0,  
  34.             0,  
  35.             0,  
  36.             0,  
  37.             0,  
  38.             0,  
  39.             0,  
  40.             0,  
  41.             0,  
  42.             0,  
  43.             0,  
  44.             0,  
  45.             0,  
  46.             0,  
  47.             0,  
  48.             0,  
  49.             0,  
  50.             0,  
  51.             0,  
  52.             0,  
  53.             0,  
  54.             0,  
  55.             0,  
  56.             0,  
  57.             0,  
  58.             0,  
  59.             0,  
  60.             0,  
  61.             0,  
  62.             0,  
  63.             0,  
  64.             0,  
  65.             0,  
  66.             0,  
  67.             0,  
  68.             0,  
  69.             0,  
  70.             0,  
  71.             0,  
  72.             0,  
  73.             0,  
  74.             0,  
  75.             0,  
  76.             0,  
  77.             0,  
  78.             0,  
  79.             0,  
  80.             0,  
  81.             0,  
  82.             0,  
  83.             0,  
  84.             0,  
  85.             0,  
  86.             0,  
  87.             0,  
  88.             0,  
  89.             0,  
  90.             0 };  
  91.     /* 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中 
  92.        被定义到MD5_CTX结构中 
  93.      
  94.      */  
  95.     private long[] state = new long[4]; // state (ABCD)  
  96.     private long[] count = new long[2];  
  97.     // number of bits, modulo 2^64 (lsb first)  
  98.     private byte[] buffer = new byte[64]; // input buffer  
  99.   
  100.     /* digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的 
  101.           16进制ASCII表示. 
  102.     */  
  103.     public String digestHexStr;  
  104.   
  105.     /* digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值. 
  106.     */  
  107.     private byte[] digest = new byte[16];  
  108.       
  109.     /** 
  110.      * 得到MD5加密后的字符串 
  111.      * @param inbuf 
  112.      * @return 
  113.      */  
  114.     public String getStrToMD5(String inbuf) {  
  115.         md5Init();  
  116.         md5Update(inbuf.getBytes(), inbuf.length());  
  117.         md5Final();  
  118.         digestHexStr = "";  
  119.         for (int i = 0; i < 16; i++) {  
  120.             digestHexStr += byteHEX(digest[i]);  
  121.         }  
  122.         return digestHexStr;  
  123.   
  124.     }  
  125.   
  126.     /* 
  127.       getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串 
  128.       返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的. 
  129.     */  
  130.     public String getMD5ofStr(String inbuf) {  
  131.         md5Init();  
  132.         md5Update(inbuf.getBytes(), inbuf.length());  
  133.         md5Final();  
  134.         digestHexStr = "";  
  135.         for (int i = 0; i < 16; i++) {  
  136.             digestHexStr += byteHEX(digest[i]);  
  137.         }  
  138.         digestHexStr=digestHexStr.substring(8,24);  
  139.         return digestHexStr;  
  140.   
  141.     }  
  142.     // 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数  
  143.     public MD5() {  
  144.         md5Init();  
  145.   
  146.         return;  
  147.     }  
  148.   
  149.     /* md5Init是一个初始化函数,初始化核心变量,装入标准的幻数 */  
  150.     private void md5Init() {  
  151.         count[0] = 0L;  
  152.         count[1] = 0L;  
  153.         ///* Load magic initialization constants.  
  154.   
  155.         state[0] = 0x67452301L;  
  156.         state[1] = 0xefcdab89L;  
  157.         state[2] = 0x98badcfeL;  
  158.         state[3] = 0x10325476L;  
  159.   
  160.         return;  
  161.     }  
  162.     /* F, G, H ,I 是4个基本的MD5函数,在原始的MD5的C实现中,由于它们是  
  163.     简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们  
  164.            实现成了private方法,名字保持了原来C中的。 */  
  165.   
  166.     private long F(long x, long y, long z) {  
  167.         return (x & y) | ((~x) & z);  
  168.   
  169.     }  
  170.     private long G(long x, long y, long z) {  
  171.         return (x & z) | (y & (~z));  
  172.   
  173.     }  
  174.     private long H(long x, long y, long z) {  
  175.         return x ^ y ^ z;  
  176.     }  
  177.   
  178.     private long I(long x, long y, long z) {  
  179.         return y ^ (x | (~z));  
  180.     }  
  181.   
  182.     /*  
  183.           FF,GG,HH和II将调用F,G,H,I进行近一步变换 
  184.           FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. 
  185.           Rotation is separate from addition to prevent recomputation. 
  186.     */  
  187.   
  188.     private long FF(long a, long b, long c, long d, long x, long s, long ac) {  
  189.         a += F(b, c, d) + x + ac;  
  190.         a = ((int) a << s) | ((int) a >>> (32 - s));  
  191.         a += b;  
  192.         return a;  
  193.     }  
  194.   
  195.     private long GG(long a, long b, long c, long d, long x, long s, long ac) {  
  196.         a += G(b, c, d) + x + ac;  
  197.         a = ((int) a << s) | ((int) a >>> (32 - s));  
  198.         a += b;  
  199.         return a;  
  200.     }  
  201.     private long HH(long a, long b, long c, long d, long x, long s, long ac) {  
  202.         a += H(b, c, d) + x + ac;  
  203.         a = ((int) a << s) | ((int) a >>> (32 - s));  
  204.         a += b;  
  205.         return a;  
  206.     }  
  207.     private long II(long a, long b, long c, long d, long x, long s, long ac) {  
  208.         a += I(b, c, d) + x + ac;  
  209.         a = ((int) a << s) | ((int) a >>> (32 - s));  
  210.         a += b;  
  211.         return a;  
  212.     }  
  213.     /* 
  214.      md5Update是MD5的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个 
  215.      函数由getMD5ofStr调用,调用之前需要调用md5init,因此把它设计成private的 
  216.     */  
  217.     private void md5Update(byte[] inbuf, int inputLen) {  
  218.   
  219.         int i, index, partLen;  
  220.         byte[] block = new byte[64];  
  221.         index = (int) (count[0] >>> 3) & 0x3F;  
  222.         // /* Update number of bits */  
  223.         if ((count[0] += (inputLen << 3)) < (inputLen << 3))  
  224.             count[1]++;  
  225.         count[1] += (inputLen >>> 29);  
  226.   
  227.         partLen = 64 - index;  
  228.   
  229.         // Transform as many times as possible.  
  230.         if (inputLen >= partLen) {  
  231.             md5Memcpy(buffer, inbuf, index, 0, partLen);  
  232.             md5Transform(buffer);  
  233.   
  234.             for (i = partLen; i + 63 < inputLen; i += 64) {  
  235.   
  236.                 md5Memcpy(block, inbuf, 0, i, 64);  
  237.                 md5Transform(block);  
  238.             }  
  239.             index = 0;  
  240.   
  241.         } else  
  242.             i = 0;  
  243.   
  244.         ///* Buffer remaining input */  
  245.         md5Memcpy(buffer, inbuf, index, i, inputLen - i);  
  246.   
  247.     }  
  248.   
  249.     /* 
  250.       md5Final整理和填写输出结果 
  251.     */  
  252.     private void md5Final() {  
  253.         byte[] bits = new byte[8];  
  254.         int index, padLen;  
  255.   
  256.         ///* Save number of bits */  
  257.         Encode(bits, count, 8);  
  258.   
  259.         ///* Pad out to 56 mod 64.  
  260.         index = (int) (count[0] >>> 3) & 0x3f;  
  261.         padLen = (index < 56) ? (56 - index) : (120 - index);  
  262.         md5Update(PADDING, padLen);  
  263.   
  264.         ///* Append length (before padding) */  
  265.         md5Update(bits, 8);  
  266.   
  267.         ///* Store state in digest */  
  268.         Encode(digest, state, 16);  
  269.   
  270.     }  
  271.   
  272.     /* md5Memcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的 
  273.           字节拷贝到output的outpos位置开始  
  274.     */  
  275.   
  276.     private void md5Memcpy(  
  277.         byte[] output,  
  278.         byte[] input,  
  279.         int outpos,  
  280.         int inpos,  
  281.         int len) {  
  282.         int i;  
  283.   
  284.         for (i = 0; i < len; i++)  
  285.             output[outpos + i] = input[inpos + i];  
  286.     }  
  287.   
  288.     /* 
  289.        md5Transform是MD5核心变换程序,有md5Update调用,block是分块的原始字节 
  290.     */  
  291.     private void md5Transform(byte block[]) {  
  292.         long a = state[0], b = state[1], c = state[2], d = state[3];  
  293.         long[] x = new long[16];  
  294.   
  295.         Decode(x, block, 64);  
  296.   
  297.         /* Round 1 */  
  298.         a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */  
  299.         d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */  
  300.         c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */  
  301.         b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */  
  302.         a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */  
  303.         d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */  
  304.         c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */  
  305.         b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */  
  306.         a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */  
  307.         d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */  
  308.         c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */  
  309.         b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */  
  310.         a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */  
  311.         d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */  
  312.         c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */  
  313.         b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */  
  314.   
  315.         /* Round 2 */  
  316.         a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */  
  317.         d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */  
  318.         c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */  
  319.         b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */  
  320.         a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */  
  321.         d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */  
  322.         c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */  
  323.         b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */  
  324.         a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */  
  325.         d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */  
  326.         c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */  
  327.         b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */  
  328.         a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */  
  329.         d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */  
  330.         c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */  
  331.         b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */  
  332.   
  333.         /* Round 3 */  
  334.         a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */  
  335.         d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */  
  336.         c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */  
  337.         b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */  
  338.         a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */  
  339.         d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */  
  340.         c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */  
  341.         b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */  
  342.         a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */  
  343.         d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */  
  344.         c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */  
  345.         b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */  
  346.         a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */  
  347.         d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */  
  348.         c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */  
  349.         b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */  
  350.   
  351.         /* Round 4 */  
  352.         a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */  
  353.         d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */  
  354.         c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */  
  355.         b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */  
  356.         a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */  
  357.         d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */  
  358.         c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */  
  359.         b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */  
  360.         a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */  
  361.         d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */  
  362.         c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */  
  363.         b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */  
  364.         a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */  
  365.         d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */  
  366.         c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */  
  367.         b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */  
  368.   
  369.         state[0] += a;  
  370.         state[1] += b;  
  371.         state[2] += c;  
  372.         state[3] += d;  
  373.   
  374.     }  
  375.   
  376.     /*Encode把long数组按顺序拆成byte数组,因为java的long类型是64bit的, 
  377.       只拆低32bit,以适应原始C实现的用途 
  378.     */  
  379.     private void Encode(byte[] output, long[] input, int len) {  
  380.         int i, j;  
  381.   
  382.         for (i = 0, j = 0; j < len; i++, j += 4) {  
  383.             output[j] = (byte) (input[i] & 0xffL);  
  384.             output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);  
  385.             output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);  
  386.             output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);  
  387.         }  
  388.     }  
  389.   
  390.     /*Decode把byte数组按顺序合成成long数组,因为java的long类型是64bit的, 
  391.       只合成低32bit,高32bit清零,以适应原始C实现的用途 
  392.     */  
  393.     private void Decode(long[] output, byte[] input, int len) {  
  394.         int i, j;  
  395.   
  396.         for (i = 0, j = 0; j < len; i++, j += 4)  
  397.             output[i] =  
  398.                 b2iu(input[j])  
  399.                     | (b2iu(input[j + 1]) << 8)  
  400.                     | (b2iu(input[j + 2]) << 16)  
  401.                     | (b2iu(input[j + 3]) << 24);  
  402.   
  403.         return;  
  404.     }  
  405.   
  406.     /* 
  407.       b2iu是我写的一个把byte按照不考虑正负号的原则的"升位"程序,因为java没有unsigned运算 
  408.     */  
  409.     public static long b2iu(byte b) {  
  410.         return b < 0 ? b & 0x7F + 128 : b;  
  411.     }  
  412.   
  413.     /*byteHEX(),用来把一个byte类型的数转换成十六进制的ASCII表示, 
  414.          因为java中的byte的toString无法实现这一点,我们又没有C语言中的 
  415.       sprintf(outbuf,"%02X",ib) 
  416.     */  
  417.     public static String byteHEX(byte ib) {  
  418.         char[] Digit =  
  419.             {  
  420.                 '0',  
  421.                 '1',  
  422.                 '2',  
  423.                 '3',  
  424.                 '4',  
  425.                 '5',  
  426.                 '6',  
  427.                 '7',  
  428.                 '8',  
  429.                 '9',  
  430.                 'a',  
  431.                 'b',  
  432.                 'c',  
  433.                 'd',  
  434.                 'e',  
  435.                 'f' };  
  436.         char[] ob = new char[2];  
  437.         ob[0] = Digit[(ib >>> 4) & 0X0F];  
  438.         ob[1] = Digit[ib & 0X0F];  
  439.         String s = new String(ob);  
  440.         return s;  
  441.     }  
  442.   
  443.     public static void main(String args[]) {  
  444.   
  445.         MD5 m = new MD5();  
  446.         String abc = m.getMD5ofStr("123456");  
  447.         System.out.println(abc);  
  448.     }  
  449.   
  450. }  

  复杂加密:

Java代码  收藏代码
  1. public static String Encrypt(String str) {  
  2.     Security.addProvider(new com.sun.crypto.provider.SunJCE());  
  3.     sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();  
  4.     String strOut = "";  
  5.     try {  
  6.         // DES算法要求有一个可信任的随机数源  
  7.         SecureRandom sr = new SecureRandom();  
  8.   
  9.         // 从原始密匙数据创建DESKeySpec对象  
  10.         DESKeySpec dks = new DESKeySpec(KEY_STRING.getBytes());  
  11.   
  12.         // 创建一个密匙工厂,然后用它把DESKeySpec转换成  
  13.         // 一个SecretKey对象  
  14.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
  15.         SecretKey key = keyFactory.generateSecret(dks);  
  16.   
  17.         // Cipher对象实际完成加密操作  
  18.         Cipher cipher = Cipher.getInstance("DES");  
  19.   
  20.         // 用密匙初始化Cipher对象  
  21.         cipher.init(Cipher.ENCRYPT_MODE, key, sr);  
  22.   
  23.         // 现在,获取数据并加密  
  24.         byte data[] = str.getBytes(); /* 用某种方法获取数据 */  
  25.   
  26.         // 正式执行加密操作  
  27.         byte encryptedData[] = cipher.doFinal(data);  
  28.   
  29.         strOut = encoder.encode(encryptedData);  
  30.     } catch (Exception e) {  
  31.         e.printStackTrace();  
  32.     }  
  33.     return strOut;  
  34. }  
  35.   
  36. public static String Decrypt(String s) {  
  37.     Security.addProvider(new com.sun.crypto.provider.SunJCE());  
  38.     String strOut = "";  
  39.     sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();  
  40.     try {  
  41.         // DES算法要求有一个可信任的随机数源  
  42.         SecureRandom sr = new SecureRandom();  
  43.   
  44.         // 从原始密匙数据创建一个DESKeySpec对象  
  45.         DESKeySpec dks = new DESKeySpec(KEY_STRING.getBytes());  
  46.   
  47.         // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成  
  48.         // 一个SecretKey对象  
  49.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
  50.           
  51.         SecretKey key = keyFactory.generateSecret(dks);  
  52.   
  53.         // Cipher对象实际完成解密操作  
  54.         Cipher cipher = Cipher.getInstance("DES");  
  55.   
  56.         // 用密匙初始化Cipher对象  
  57.         cipher.init(Cipher.DECRYPT_MODE, key, sr);  
  58.   
  59.         // 现在,获取数据并解密  
  60.         byte encryptedData[] = decoder.decodeBuffer(s); /* 获得经过加密的数据 */  
  61.   
  62.         // 正式执行解密操作  
  63.         byte decryptedData[] = cipher.doFinal(encryptedData);  
  64.   
  65.         strOut = new String(decryptedData);  
  66.     } catch (Exception e) {  
  67.         e.printStackTrace();  
  68.     }  
  69.     return strOut;  
  70. }  
  71.   
  72. public static void main(String[] args){  
  73.     String a=Encrypt("aaa");  
  74.       
  75.     System.out.print(a);  
  76.       
  77.     String b=Decrypt("5991bf21cbe98232".toUpperCase());  
  78.       
  79.     System.out.print(b);  
  80. }  
 


0 0
原创粉丝点击