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

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

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

    小毅收藏之------JAVA加密解密工具類

    Posted on 2009-05-30 01:49 H2O 閱讀(541) 評論(0)  編輯  收藏 所屬分類: java
    在很多的時候,需要將重要信息加密,而以下類就是在java中如何加密和解密經常用到的代碼:
      1package cn.com.hkgt.apps.util; 
      2
      3
      4import java.security.*
      5import javax.crypto.Cipher; 
      6import javax.crypto.SecretKey; 
      7import javax.crypto.SecretKeyFactory; 
      8import javax.crypto.spec.DESKeySpec; 
      9
     10/** 
     11 * 字符串工具集合 
     12 */
     
     13public class StringUtils 
     14
     15    private static final String PASSWORD_CRYPT_KEY = "cindaportal"
     16private final static String DES = "DES"
     17
     18/** 
     19 * 加密 
     20 * @param src 數據源 
     21 * @param key 密鑰,長度必須是8的倍數 
     22 * @return   返回加密后的數據 
     23 * @throws Exception 
     24 */
     
     25public static byte[] encrypt(byte[] src, byte[] key)throws Exception 
     26                //DES算法要求有一個可信任的隨機數源 
     27                SecureRandom sr = new SecureRandom(); 
     28                // 從原始密匙數據創建DESKeySpec對象 
     29                DESKeySpec dks = new DESKeySpec(key); 
     30                // 創建一個密匙工廠,然后用它把DESKeySpec轉換成 
     31                // 一個SecretKey對象 
     32                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); 
     33                SecretKey securekey = keyFactory.generateSecret(dks); 
     34                // Cipher對象實際完成加密操作 
     35                Cipher cipher = Cipher.getInstance(DES); 
     36                // 用密匙初始化Cipher對象 
     37                cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); 
     38                // 現在,獲取數據并加密 
     39                // 正式執行加密操作 
     40                return cipher.doFinal(src); 
     41         }
     
     42
     43         /** 
     44          * 解密 
     45          * @param src 數據源 
     46          * @param key 密鑰,長度必須是8的倍數 
     47          * @return     返回解密后的原始數據 
     48          * @throws Exception 
     49          */
     
     50         public static byte[] decrypt(byte[] src, byte[] key)throws Exception 
     51                // DES算法要求有一個可信任的隨機數源 
     52                SecureRandom sr = new SecureRandom(); 
     53                // 從原始密匙數據創建一個DESKeySpec對象 
     54                DESKeySpec dks = new DESKeySpec(key); 
     55                // 創建一個密匙工廠,然后用它把DESKeySpec對象轉換成 
     56                // 一個SecretKey對象 
     57                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); 
     58                SecretKey securekey = keyFactory.generateSecret(dks); 
     59                // Cipher對象實際完成解密操作 
     60                Cipher cipher = Cipher.getInstance(DES); 
     61                // 用密匙初始化Cipher對象 
     62                cipher.init(Cipher.DECRYPT_MODE, securekey, sr); 
     63                // 現在,獲取數據并解密 
     64                // 正式執行解密操作 
     65                return cipher.doFinal(src); 
     66         }
     
     67    /** 
     68     * 密碼解密 
     69     * @param data 
     70     * @return 
     71     * @throws Exception 
     72     */
     
     73    public final static String decrypt(String data)
     74         try 
     75            return new String(decrypt(hex2byte(data.getBytes()),PASSWORD_CRYPT_KEY.getBytes())); 
     76        }
    catch(Exception e) 
     77        }
     
     78        return null
     79    }
     
     80    /** 
     81     * 密碼加密 
     82     * @param password 
     83     * @return 
     84     * @throws Exception 
     85     */
     
     86    public final static String encrypt(String password)
     87        try 
     88            return byte2hex(encrypt(password.getBytes(),PASSWORD_CRYPT_KEY.getBytes())); 
     89        }
    catch(Exception e) 
     90        }
     
     91        return null
     92    }
     
     93/** 
     94 * 二行制轉字符串 
     95 * @param b 
     96 * @return 
     97 */
     
     98    public static String byte2hex(byte[] b) 
     99                String hs = ""
    100                String stmp = ""
    101                for (int n = 0; n < b.length; n++
    102                        stmp = (java.lang.Integer.toHexString(b[n] & 0XFF)); 
    103                        if (stmp.length() == 1
    104                                hs = hs + "0" + stmp; 
    105                        else 
    106                                hs = hs + stmp; 
    107                }
     
    108                return hs.toUpperCase(); 
    109     }
     
    110
    111    public static byte[] hex2byte(byte[] b) 
    112       if((b.length%2)!=0
    113              throw new IllegalArgumentException("長度不是偶數"); 
    114                byte[] b2 = new byte[b.length/2]; 
    115                for (int n = 0; n < b.length; n+=2
    116                    String item = new String(b,n,2); 
    117                    b2[n/2= (byte)Integer.parseInt(item,16); 
    118                }
     
    119       return b2; 
    120    }
     
    121
    122    public static void main(String[] args) 
    123        String pwd = "測試dasdfaaaaaaa"
    124        System.out.println("測試數據="+pwd); 
    125        String data = encrypt(pwd); 
    126        System.out.println("加密后的數據data="+data); 
    127        pwd = decrypt(data); 
    128        System.out.println("解密后="+pwd); 
    129
    130    }
     
    131}
     
    132
    133
    134

    posts - 0, comments - 21, trackbacks - 0, articles - 101

    Copyright © H2O

    主站蜘蛛池模板: 亚洲视频在线观看免费视频| 国产大片51精品免费观看| 亚洲六月丁香婷婷综合| 午夜免费福利影院| 久久不见久久见免费影院www日本| 亚洲AV永久无码区成人网站| 中国xxxxx高清免费看视频| 亚洲精品无码成人片久久不卡| 亚洲伊人久久综合中文成人网| 99re6在线视频精品免费下载| 亚洲国产精品成人综合色在线| 亚洲精品无码鲁网中文电影| 国产卡二卡三卡四卡免费网址| 阿v免费在线观看| 亚洲国产美女视频| 久久精品国产亚洲7777| 久久精品无码一区二区三区免费| 在线播放免费人成视频网站| 亚洲免费在线视频观看| 亚洲午夜久久久影院| 成人午夜大片免费7777| 久久青草免费91线频观看站街| 国产成人精品日本亚洲语音| 亚洲网红精品大秀在线观看| 亚洲精品岛国片在线观看| 成人女人A级毛片免费软件| 久久国产乱子精品免费女| 国产精品亚洲一区二区在线观看| 亚洲福利视频网址| 国产亚洲精久久久久久无码| 国产免费av一区二区三区| 69xx免费观看视频| 男女午夜24式免费视频| 无码 免费 国产在线观看91| 亚洲性无码一区二区三区| 老色鬼久久亚洲AV综合| 亚洲人JIZZ日本人| 亚洲福利精品电影在线观看| 四虎影院在线免费播放| 全免费毛片在线播放| 91香蕉国产线观看免费全集|