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

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

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

    數(shù)據(jù)加載中……
    JAVA:DES可逆加密算法
      1 import java.security.*;
      2 import javax.crypto.*;
      3 
      4 public class DESPlus {
      5     private static String strDefaultKey = "national";
      6     private Cipher encryptCipher = null;
      7     private Cipher decryptCipher = null;
      8 
      9     /**
     10      * 將byte數(shù)組轉(zhuǎn)換為表示16進(jìn)制值的字符串, 如:byte[]{8,18}轉(zhuǎn)換為:0813, 和public static byte[]
     11      * hexStr2ByteArr(String strIn) 互為可逆的轉(zhuǎn)換過(guò)程
     12      * 
     13      * @param arrB
     14      *            需要轉(zhuǎn)換的byte數(shù)組
     15      * @return 轉(zhuǎn)換后的字符串
     16      * @throws Exception
     17      *             本方法不處理任何異常,所有異常全部拋出
     18      */
     19     public static String byteArr2HexStr(byte[] arrB) throws Exception {
     20         int iLen = arrB.length;
     21         // 每個(gè)byte用兩個(gè)字符才能表示,所以字符串的長(zhǎng)度是數(shù)組長(zhǎng)度的兩倍
     22         StringBuffer sb = new StringBuffer(iLen * 2);
     23         for (int i = 0; i < iLen; i++) {
     24             int intTmp = arrB[i];
     25             // 把負(fù)數(shù)轉(zhuǎn)換為正數(shù)
     26             while (intTmp < 0) {
     27                 intTmp = intTmp + 256;
     28             }
     29             // 小于0F的數(shù)需要在前面補(bǔ)0
     30             if (intTmp < 16) {
     31                 sb.append("0");
     32             }
     33             sb.append(Integer.toString(intTmp, 16));
     34         }
     35         return sb.toString();
     36     }
     37 
     38     /**
     39      * 將表示16進(jìn)制值的字符串轉(zhuǎn)換為byte數(shù)組, 和public static String byteArr2HexStr(byte[] arrB)
     40      * 互為可逆的轉(zhuǎn)換過(guò)程
     41      * 
     42      * @param strIn
     43      *            需要轉(zhuǎn)換的字符串
     44      * @return 轉(zhuǎn)換后的byte數(shù)組
     45      * @throws Exception
     46      *             本方法不處理任何異常,所有異常全部拋出
     47      * @author <a href="mailto:leo841001@163.com">LiGuoQing</a>
     48      */
     49     public static byte[] hexStr2ByteArr(String strIn) throws Exception {
     50         byte[] arrB = strIn.getBytes();
     51         int iLen = arrB.length;
     52 
     53         // 兩個(gè)字符表示一個(gè)字節(jié),所以字節(jié)數(shù)組長(zhǎng)度是字符串長(zhǎng)度除以2
     54         byte[] arrOut = new byte[iLen / 2];
     55         for (int i = 0; i < iLen; i = i + 2) {
     56             String strTmp = new String(arrB, i, 2);
     57             arrOut[i / 2= (byte) Integer.parseInt(strTmp, 16);
     58         }
     59         return arrOut;
     60     }
     61 
     62     /**
     63      * 默認(rèn)構(gòu)造方法,使用默認(rèn)密鑰
     64      * 
     65      * @throws Exception
     66      */
     67     public DESPlus() throws Exception {
     68         this(strDefaultKey);
     69     }
     70 
     71     /**
     72      * 指定密鑰構(gòu)造方法
     73      * 
     74      * @param strKey
     75      *            指定的密鑰
     76      * @throws Exception
     77      */
     78     public DESPlus(String strKey) throws Exception {
     79         Security.addProvider(new com.sun.crypto.provider.SunJCE());
     80         Key key = getKey(strKey.getBytes());
     81 
     82         encryptCipher = Cipher.getInstance("DES");
     83         encryptCipher.init(Cipher.ENCRYPT_MODE, key);
     84 
     85         decryptCipher = Cipher.getInstance("DES");
     86         decryptCipher.init(Cipher.DECRYPT_MODE, key);
     87     }
     88 
     89     /**
     90      * 加密字節(jié)數(shù)組
     91      * 
     92      * @param arrB
     93      *            需加密的字節(jié)數(shù)組
     94      * @return 加密后的字節(jié)數(shù)組
     95      * @throws Exception
     96      */
     97     public byte[] encrypt(byte[] arrB) throws Exception {
     98         return encryptCipher.doFinal(arrB);
     99     }
    100 
    101     /**
    102      * 加密字符串
    103      * 
    104      * @param strIn
    105      *            需加密的字符串
    106      * @return 加密后的字符串
    107      * @throws Exception
    108      */
    109     public String encrypt(String strIn) throws Exception {
    110         return byteArr2HexStr(encrypt(strIn.getBytes()));
    111     }
    112 
    113     /**
    114      * 解密字節(jié)數(shù)組
    115      * 
    116      * @param arrB
    117      *            需解密的字節(jié)數(shù)組
    118      * @return 解密后的字節(jié)數(shù)組
    119      * @throws Exception
    120      */
    121     public byte[] decrypt(byte[] arrB) throws Exception {
    122         return decryptCipher.doFinal(arrB);
    123     }
    124 
    125     /**
    126      * 解密字符串
    127      * 
    128      * @param strIn
    129      *            需解密的字符串
    130      * @return 解密后的字符串
    131      * @throws Exception
    132      */
    133     public String decrypt(String strIn) throws Exception {
    134         return new String(decrypt(hexStr2ByteArr(strIn)));
    135     }
    136 
    137     /**
    138      * 從指定字符串生成密鑰,密鑰所需的字節(jié)數(shù)組長(zhǎng)度為8位 不足8位時(shí)后面補(bǔ)0,超出8位只取前8位
    139      * 
    140      * @param arrBTmp
    141      *            構(gòu)成該字符串的字節(jié)數(shù)組
    142      * @return 生成的密鑰
    143      * @throws java.lang.Exception
    144      */
    145     private Key getKey(byte[] arrBTmp) throws Exception {
    146         // 創(chuàng)建一個(gè)空的8位字節(jié)數(shù)組(默認(rèn)值為0)
    147         byte[] arrB = new byte[8];
    148 
    149         // 將原始字節(jié)數(shù)組轉(zhuǎn)換為8位
    150         for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
    151             arrB[i] = arrBTmp[i];
    152         }
    153 
    154         // 生成密鑰
    155         Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
    156 
    157         return key;
    158     }
    159 }
    160 

    posted on 2009-05-24 11:18 YeeYang 閱讀(6709) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): 程序設(shè)計(jì)

    主站蜘蛛池模板: 亚洲男人天堂2017| 久久永久免费人妻精品下载 | 亚洲a∨无码精品色午夜| 亚洲成AV人在线播放无码| 婷婷亚洲天堂影院| 久久久久久国产精品免费免费 | 亚洲精品无码专区久久久| 成人爱做日本视频免费| 18未年禁止免费观看| 国产免费阿v精品视频网址| 曰批全过程免费视频免费看| 亚洲乱理伦片在线观看中字| 亚洲乱码无限2021芒果| 久久亚洲中文字幕精品有坂深雪| 黑人大战亚洲人精品一区 | 亚洲精品无码久久久久A片苍井空| 亚洲av日韩av激情亚洲| 亚洲色欲色欲www在线丝| 亚洲第一页日韩专区| 国产一级理论免费版| 午夜a级成人免费毛片| 久久不见久久见中文字幕免费| 亚洲成人在线免费观看| 8x成人永久免费视频| 久9热免费精品视频在线观看| 两个人看的www免费| 久久精品成人免费国产片小草| 日亚毛片免费乱码不卡一区| 国产亚洲蜜芽精品久久| 色噜噜噜噜亚洲第一| 久久水蜜桃亚洲AV无码精品| 精品亚洲成A人在线观看青青| 亚洲精华国产精华精华液| 亚洲狠狠婷婷综合久久蜜芽| 亚洲一久久久久久久久| 亚洲国产高清国产拍精品| 亚洲成a∨人片在无码2023| MM1313亚洲国产精品| 成人福利在线观看免费视频| 国产精品九九久久免费视频| baoyu122.永久免费视频|