空間站

          北極心空

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            15 Posts :: 393 Stories :: 160 Comments :: 0 Trackbacks

          一般常用的有:

          MD5、SHA算法:代碼如下

          Java代碼 復制代碼
          1. /*  
          2.  * Copyright (c) 2008  
          3.  * All rights reserved.  
          4.  */  
          5. package cn.com.jody.wsclient;   
          6.   
          7. /**  
          8.  * <p>  
          9.  * 功能描述 MD5\SHA加密類  
          10.  * </p>  
          11.  * <br>  
          12.  * file name:EncryptCount .java<br>  
          13.  * @author: JODY  
          14.  * @Date: Jun 24, 2008  
          15.  * @Time: 12:11:02 PM  
          16.  */  
          17. import java.io.IOException;   
          18. import java.io.InputStream;   
          19. import java.security.MessageDigest;   
          20.   
          21. public class EncryptCount {   
          22.        
          23.     /**  
          24.      * MD5  
          25.      * @param fis  
          26.      * @return  
          27.      * String  
          28.      */  
          29.     public static String getMD5(InputStream fis) {   
          30.   
          31.         try {   
          32.             MessageDigest md = MessageDigest.getInstance("MD5");   
          33.             byte[] buffer = new byte[2048];   
          34.             int length = -1;   
          35.             while ((length = fis.read(buffer)) != -1) {   
          36.                 md.update(buffer, 0, length);   
          37.             }   
          38.             return bytesToString(md.digest());   
          39.         } catch (Exception ex) {   
          40.             ex.printStackTrace();   
          41.             return null;   
          42.         } finally {   
          43.             try {   
          44.                 fis.close();   
          45.             } catch (IOException ex) {   
          46.                 ex.printStackTrace();   
          47.             }   
          48.         }   
          49.     }   
          50.     /**  
          51.      * SHA  
          52.      * @param fis  
          53.      * @return  
          54.      * String  
          55.      */  
          56.     public static String getSHA(InputStream fis) {   
          57.         try {   
          58.             MessageDigest md = MessageDigest.getInstance("SHA");   
          59.             byte[] buffer = new byte[2048];   
          60.             int length = -1;   
          61.             while ((length = fis.read(buffer)) != -1) {   
          62.                 md.update(buffer, 0, length);   
          63.             }   
          64.             return bytesToString(md.digest());   
          65.         } catch (Exception ex) {   
          66.             ex.printStackTrace();   
          67.             return null;   
          68.         } finally {   
          69.             try {   
          70.                 fis.close();   
          71.             } catch (IOException ex) {   
          72.                 ex.printStackTrace();   
          73.             }   
          74.         }   
          75.     }   
          76.        
          77.     /**  
          78.      * 字節2字符串  
          79.      * @param data  
          80.      * @return  
          81.      * String  
          82.      */  
          83.     private static String bytesToString(byte[] data) {   
          84.         char hexDigits[] = { '0''1''2''3''4''5''6''7''8''9''a''b''c''d''e''f' };   
          85.         char[] temp = new char[data.length * 2];   
          86.         for (int i = 0; i < data.length; i++) {   
          87.             byte b = data[i];   
          88.             temp[i * 2] = hexDigits[b >>> 4 & 0x0f];   
          89.             temp[i * 2 + 1] = hexDigits[b & 0x0f];   
          90.         }   
          91.         return new String(temp);   
          92.     }   
          93. }  

           

          調用:

          Java代碼 復制代碼
          1. public String encryptMD5(String password) {   
          2.         InputStream is = new ByteArrayInputStream(password.getBytes());   
          3.         String res = EncryptCount.getMD5(is);   
          4.         return res;   
          5.     }  

           

          加密前常常會轉換成十六進制:

          字符串轉換成十六進制

           

          Java代碼 復制代碼
          1. /**  
          2.      * byte數組轉16進制字符串  
          3.      * @param b  
          4.      * @return  
          5.      */  
          6.     static public String byteArrayToHexString(byte b[]) {   
          7.         String result = "";   
          8.         for (int i = 0; i < b.length; i++)   
          9.             result = result + byteToHexString(b[i]);   
          10.         return result;   
          11.     }   
          12.   
          13.     static public String byteToHexString(byte b) {   
          14.         int n = b;   
          15.         if (n < 0)   
          16.             n = 256 + n;   
          17.         int d1 = n / 16;   
          18.         int d2 = n % 16;   
          19.         return HexCode[d1] + HexCode[d2];   
          20.     }   
          21.   
          22.     static public String HexCode[] = { "0""1""2""3""4""5""6""7""8",   
          23.             "9""A""B""C""D""E""F" };  

           

          在網絡傳輸過程中需要用到加密解密的

          Encrypt算法:

           

           

          Java代碼 復制代碼
          1. /*  
          2.  * Copyright (c) 2008   
          3.  * All rights reserved.  
          4.  */  
          5. package cn.com.jody.wsclient;   
          6.   
          7.   
          8. /**  
          9.  * <p>  
          10.  * 功能描述 加密解密類  
          11.  * </p>  
          12.  * <br>  
          13.  * file name:Encrypt.java<br>  
          14.  * @author: JODY  
          15.  * @Date: Jun 20, 2008  
          16.  * @Time: 12:11:02 PM  
          17.  */  
          18. public class Encrypt {   
          19.     private static int MIN_ASC = 32;   
          20.   
          21.     private static int MAX_ASC = 126;   
          22.   
          23.     private static int NUM_ASC = MAX_ASC - MIN_ASC + 1;   
          24.   
          25.     private static long MYPRIMENUMBER = 100537;   
          26.   
          27.     private static long MYPRIMENUMBER2 = 100609;   
          28.   
          29.     private static String KEYWORD = "12345678"//密匙   
          30.   
          31.        
          32.     private static String decoder(String from_text) {   
          33.         long key;   
          34.         double offset;   
          35.         int str_len;   
          36.         int ch;   
          37.         char[] word = from_text.toCharArray();   
          38.         String to_text = "";   
          39.         key = NumericPassword(KEYWORD);   
          40.         str_len = from_text.length() - 1;   
          41.         for (int i = 0; i < str_len; i++) {   
          42.             word[i] = from_text.charAt(i);   
          43.             ch = word[i];   
          44.             if (ch >= MIN_ASC && ch <= MAX_ASC) {   
          45.                 i = i + 1;   
          46.                 ch = ch - MIN_ASC;   
          47.                 offset = (NUM_ASC + 1) * ((double) (((key * i) % MYPRIMENUMBER)) / (double) (MYPRIMENUMBER));   
          48.                 ch = ((ch - (int) (offset)) % NUM_ASC);   
          49.                 if (ch < 0)   
          50.                     ch = ch + NUM_ASC;   
          51.                 ch = ch + MIN_ASC;   
          52.                 i = i - 1;   
          53.                 to_text += (char) (ch);   
          54.             }   
          55.         }   
          56.         return to_text;   
          57.     }   
          58.   
          59.     private static String encoder(String from_text) {   
          60.         long key;   
          61.         double offset;   
          62.         int str_len;   
          63.         int ch;   
          64.         char[] word = from_text.toCharArray();   
          65.         String to_text = "";   
          66.         key = NumericPassword(KEYWORD);   
          67.         str_len = from_text.length() - 1;   
          68.         for (int i = 0; i <= str_len; i++) {   
          69.             word[i] = from_text.charAt(i);   
          70.             ch = word[i];   
          71.             if (ch >= MIN_ASC && ch <= MAX_ASC) {   
          72.                 i = i + 1;   
          73.                 ch = ch - MIN_ASC;   
          74.                 offset = (NUM_ASC + 1) * ((double) (((key * i) % MYPRIMENUMBER)) / (double) (MYPRIMENUMBER));   
          75.                 ch = ((ch + (int) (offset)) % NUM_ASC);   
          76.                 ch = ch + MIN_ASC;   
          77.                 i = i - 1;   
          78.                 to_text = to_text + (char) (ch);   
          79.             }   
          80.         }   
          81.         return to_text + "a";   
          82.     }   
          83.   
          84.     private static long NumericPassword(String password) {   
          85.         long value, ch, shift1, shift2;   
          86.         int str_len;   
          87.   
          88.         shift1 = 0;   
          89.         shift2 = 0;   
          90.         value = 0;   
          91.         str_len = password.length();   
          92.         for (int i = 0; i < str_len; i++) {   
          93.             ch = password.charAt(i);   
          94.             value = value ^ (ch * MyIndex(shift1));   
          95.             value = value ^ (ch * MyIndex(shift2));   
          96.             shift1 = (shift1 + 7) % 19;   
          97.             shift2 = (shift2 + 13) % 23;   
          98.         }   
          99.         value = (value ^ MYPRIMENUMBER2) % MYPRIMENUMBER;   
          100.         return value;   
          101.     }   
          102.   
          103.     private static long MyIndex(long shadow) {   
          104.         long i, j;   
          105.         j = 1;   
          106.         for (i = 1; i <= shadow; i++)   
          107.             j = j * 2;   
          108.         return j;   
          109.     }   
          110.        
          111.        
          112.     private static String base64Encoder(String str){           
          113.         String returnstr = (new sun.misc.BASE64Encoder()).encode(str.getBytes());   
          114.         //System.out.println(returnstr);   
          115.         return returnstr;   
          116.            
          117.     }   
          118.        
          119.     private static String base64Decoder(String str) throws Exception{   
          120.         String returnstr = new String((new sun.misc.BASE64Decoder()).decodeBuffer(str));   
          121.         //System.out.println(returnstr);   
          122.         return returnstr;   
          123.     }   
          124.        
          125.     /**  
          126.      * 加密  
          127.      * @param from_text  
          128.      * @return  
          129.      * String  
          130.      */  
          131.     public static String Cipher(String password){   
          132.         password = Encrypt.encoder(password);   
          133.         password = Encrypt.base64Encoder(password);   
          134.         return password;   
          135.     }   
          136.     /**  
          137.      * 解密  
          138.      * @param from_text  
          139.      * @return  
          140.      * String  
          141.      */  
          142.     public static String Decipher(String password) throws Exception{   
          143.         password = Encrypt.base64Decoder(password);   
          144.         password = Encrypt.decoder(password);   
          145.         return password;   
          146.     }   
          147.     /**  
          148.      * 調用實例  
          149.      * @param args  
          150.      * void  
          151.      * @throws Exception   
          152.      */  
          153.     public static void main(String[] args) throws Exception {   
          154.         String password ="abcdefghijklmnobqrstuvwxyz1234567890";   
          155.                    
          156.         password = Encrypt.Cipher(password);   
          157.         System.out.println(password);   
          158.            
          159.         password = Encrypt.Decipher(password);   
          160.         System.out.println(password);   
          161.            
          162.     }   
          163. }  

           

          以上的幾種是比較常用的加密方法。

          posted on 2008-06-25 18:12 蘆葦 閱讀(348) 評論(0)  編輯  收藏 所屬分類: JAVA
          主站蜘蛛池模板: 安顺市| 张家界市| 石阡县| 浮山县| 丰原市| 洛宁县| 阜阳市| 舞阳县| 韩城市| 当雄县| 鄂托克旗| 兰溪市| 新野县| 衢州市| 赤城县| 余姚市| 大同市| 华池县| 西乡县| 新郑市| 九台市| 镇赉县| 锦屏县| 齐齐哈尔市| 罗江县| 怀仁县| 石泉县| 萍乡市| 当雄县| 海阳市| 嵩明县| 赞皇县| 沙田区| 丹江口市| 客服| 白城市| 和龙市| 诏安县| 东安县| 道孚县| 三台县|