




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、import java.security.Key;import java.security.Security;import java.util.Scanner;import javax.crypto.Cipher;public class Des /* 字符串默認鍵值 */ private static String strDefaultKey = "national" /* 加密工具 */ private Cipher encryptCipher = null; /* 解密工具 */ private Cipher decryptCipher = null; /* * 將b
2、yte數(shù)組轉(zhuǎn)換為表示16進制值的字符串, 如:byte8,18轉(zhuǎn)換為:0813, 和public static byte * hexStr2ByteArr(String strIn) 互為可逆的轉(zhuǎn)換過程 * * param arrB * 需要轉(zhuǎn)換的byte數(shù)組 * return 轉(zhuǎn)換后的字符串 * throws Exception * 本方法不處理任何異常,所有異常全部拋出 */ public static String byteArr2HexStr(byte arrB) throws Exception int iLen = arrB.length; / 每個byte用兩個字符才能表示,所以
3、字符串的長度是數(shù)組長度的兩倍 StringBuffer sb = new StringBuffer(iLen * 2); for (int i = 0; i < iLen; i+) int intTmp = arrBi; / 把負數(shù)轉(zhuǎn)換為正數(shù) while (intTmp < 0) intTmp = intTmp + 256; / 小于0F的數(shù)需要在前面補0 if (intTmp < 16) sb.append("0"); sb.append(Integer.toString(intTmp, 16); return sb.toString(); /* * 將
4、表示16進制值的字符串轉(zhuǎn)換為byte數(shù)組, 和public static String byteArr2HexStr(byte arrB) * 互為可逆的轉(zhuǎn)換過程 * * param strIn * 需要轉(zhuǎn)換的字符串 * return 轉(zhuǎn)換后的byte數(shù)組 * throws Exception * 本方法不處理任何異常,所有異常全部拋出 * author <a href="mailto:leo841001">LiGuoQing</a> */ public static byte hexStr2ByteArr(String strIn) throws
5、Exception byte arrB = strIn.getBytes(); int iLen = arrB.length; / 兩個字符表示一個字節(jié),所以字節(jié)數(shù)組長度是字符串長度除以2 byte arrOut = new byteiLen / 2; for (int i = 0; i < iLen; i = i + 2) String strTmp = new String(arrB, i, 2); arrOuti / 2 = (byte) Integer.parseInt(strTmp, 16); return arrOut; /* * 默認構(gòu)造方法,使用默認密鑰 * * thro
6、ws Exception */ public DesUtils() throws Exception this(strDefaultKey); /* * 指定密鑰構(gòu)造方法 * * param strKey * 指定的密鑰 * throws Exception */ public DesUtils(String strKey) throws Exception /Security.addProvider(new vider.SunJCE(); Key key = getKey(strKey.getBytes(); encryptCipher = Cipher.
7、getInstance("DES"); encryptCipher.init(Cipher.ENCRYPT_MODE, key); decryptCipher = Cipher.getInstance("DES"); decryptCipher.init(Cipher.DECRYPT_MODE, key); /* * 加密字節(jié)數(shù)組 * * param arrB * 需加密的字節(jié)數(shù)組 * return 加密后的字節(jié)數(shù)組 * throws Exception */ public byte encrypt(byte arrB) throws Exception
8、 return encryptCipher.doFinal(arrB); /* * 加密字符串 * * param strIn * 需加密的字符串 * return 加密后的字符串 * throws Exception */ public String encrypt(String strIn) throws Exception return byteArr2HexStr(encrypt(strIn.getBytes(); /* * 解密字節(jié)數(shù)組 * * param arrB * 需解密的字節(jié)數(shù)組 * return 解密后的字節(jié)數(shù)組 * throws Exception */ public b
9、yte decrypt(byte arrB) throws Exception return decryptCipher.doFinal(arrB); /* * 解密字符串 * * param strIn * 需解密的字符串 * return 解密后的字符串 * throws Exception */ public String decrypt(String strIn) throws Exception return new String(decrypt(hexStr2ByteArr(strIn); /* * 從指定字符串生成密鑰,密鑰所需的字節(jié)數(shù)組長度為8位 不足8位時后面補0,超出8位只
10、取前8位 * * param arrBTmp * 構(gòu)成該字符串的字節(jié)數(shù)組 * return 生成的密鑰 * throws java.lang.Exception */ private Key getKey(byte arrBTmp) throws Exception / 創(chuàng)建一個空的8位字節(jié)數(shù)組(默認值為0) byte arrB = new byte8; / 將原始字節(jié)數(shù)組轉(zhuǎn)換為8位 for (int i = 0; i < arrBTmp.length && i < arrB.length; i+) arrBi = arrBTmpi; / 生成密鑰 Key key
11、= new javax.crypto.spec.SecretKeySpec(arrB, "DES"); return key; public static void main(String args) System.out.println("請輸入加密前的字符:"); Scanner reader=new Scanner(System.in);while(reader.hasNext() try String test=reader.nextLine(); DesUtils des = new DesUtils("leemenz");/自定義密鑰 System.out.println("加密后的字符:" +
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 出口寵物食品合同范本
- 倉庫租賃 配送合同范本
- 主力商家合同范本
- 2025年超大型特厚板軋機項目建議書
- 第六課 友誼之樹常青 教學(xué)設(shè)計-2024-2025學(xué)年統(tǒng)編版道德與法治七年級上冊
- 包裝買賣合同范本
- 北京合伙合同范本咨詢
- 《認識面積》(教學(xué)設(shè)計)-2023-2024學(xué)年三年級下冊數(shù)學(xué)人教版
- 信用擔(dān)保借款合同范本你
- 制造珠寶生產(chǎn)訂單合同范本
- 2024-2025年江蘇專轉(zhuǎn)本英語歷年真題(含答案)
- 永磁滾筒設(shè)備操作規(guī)程
- 大班科學(xué)活動小實驗
- 裝配式混凝土結(jié)構(gòu)檢測技術(shù)規(guī)程
- 裝修合同違約解除通知書
- 湖南財政經(jīng)濟學(xué)院《中國文化史》2021-2022學(xué)年第一學(xué)期期末試卷
- (新版)六西格瑪綠帶認證考試復(fù)習(xí)題庫(含答案)
- 2024屆清華大學(xué)強基計劃數(shù)學(xué)學(xué)科筆試試題(附答案)
- 偵查學(xué)總論學(xué)習(xí)通超星期末考試答案章節(jié)答案2024年
- 2025屆高考語文一輪復(fù)習(xí):小說物象含義及作用
- 臨終關(guān)懷模式
評論
0/150
提交評論