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

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

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

    空間站

    北極心空

      BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
      15 Posts :: 393 Stories :: 160 Comments :: 0 Trackbacks

    一般常用的有:

    MD5、SHA算法:代碼如下

    Java代碼 復(fù)制代碼
    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.      * 字節(jié)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. }  

     

    調(diào)用:

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

     

    加密前常常會(huì)轉(zhuǎn)換成十六進(jìn)制:

    字符串轉(zhuǎn)換成十六進(jìn)制

     

    Java代碼 復(fù)制代碼
    1. /**  
    2.      * byte數(shù)組轉(zhuǎn)16進(jìn)制字符串  
    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" };  

     

    在網(wǎng)絡(luò)傳輸過(guò)程中需要用到加密解密的

    Encrypt算法:

     

     

    Java代碼 復(fù)制代碼
    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.      * 調(diào)用實(shí)例  
    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 蘆葦 閱讀(345) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA
    主站蜘蛛池模板: 免费观看无遮挡www的视频| jizz免费观看| 免费下载成人电影| 亚洲AV日韩精品久久久久| 一级做a爰片久久毛片免费陪 | 深夜福利在线视频免费| 全部免费毛片在线| 亚洲情侣偷拍精品| 亚洲人成网站在线在线观看| 免费A级毛片无码免费视| 亚洲AV午夜成人片| av永久免费网站在线观看 | 伊人久久亚洲综合影院| 亚洲国产精品无码专区影院| 色欲色香天天天综合网站免费| 久久夜色精品国产噜噜噜亚洲AV | 亚洲精品久久无码av片俺去也| 啦啦啦手机完整免费高清观看 | 国产亚洲福利在线视频| 国产h视频在线观看免费| 成人区精品一区二区不卡亚洲| 免费观看的毛片手机视频| 免费毛片毛片网址| 国产亚洲色婷婷久久99精品| 性色午夜视频免费男人的天堂| 亚洲经典在线中文字幕| 成年人在线免费观看| 一级成人生活片免费看| 亚洲首页在线观看| 小小影视日本动漫观看免费| 国产日韩在线视频免费播放| 亚洲一区免费观看| 国产免费人视频在线观看免费 | 亚洲国产小视频精品久久久三级| 中文在线观看国语高清免费| 亚洲综合久久综合激情久久| 成人毛片免费观看视频大全| 久久毛片免费看一区二区三区| 亚洲国产香蕉碰碰人人| 暖暖日本免费在线视频 | 国产精品成人啪精品视频免费|