版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、/* java 版的* <p>Title: DES 加解密算法</p>* <p>Description: DES 加解密算法</p>* <p>Copyright: Copyright (c) 2004</p>* <p>Company: Aspire Corp</p>* author zhangji* version 1.0*/import java.security.*;import javax.crypto.*;public class DES private
2、 static String strDefaultKey = "hnzt" private Cipher encryptCipher = null; private Cipher decryptCipher = null; /* * 將byte數(shù)組轉(zhuǎn)換為表示16進(jìn)制值的字符串, * 如:byte8,18轉(zhuǎn)換為:0813, *
3、和public static byte hexStr2ByteArr(String strIn) * 互為可逆的轉(zhuǎn)換過(guò)程 * param arrB 需要轉(zhuǎn)換的byte數(shù)組 * return 轉(zhuǎn)換后的字符串 * throws Exception 本方法不處理任何異常,所有異常全部拋出 */ public static Str
4、ing byteArr2HexStr(byte arrB) throws Exception int iLen = arrB.length; /每個(gè)byte用兩個(gè)字符才能表示,所以字符串的長(zhǎng)度是數(shù)組長(zhǎng)度的兩倍 StringBu
5、ffer sb = new StringBuffer(iLen * 2); for (int i = 0; i < iLen; i+) int intTmp = arrBi;
6、; /把負(fù)數(shù)轉(zhuǎn)換為正數(shù) while (intTmp < 0) intTmp = intTmp + 256;
7、60; /小于0F的數(shù)需要在前面補(bǔ)0 if (intTmp < 16) sb.append("0");
8、160; sb.append(Integer.toString(intTmp, 16); return sb.toString(); /*
9、 * 將表示16進(jìn)制值的字符串轉(zhuǎn)換為byte數(shù)組, * 和public static String byteArr2HexStr(byte arrB) * 互為可逆的轉(zhuǎn)換過(guò)程 * param strIn 需要轉(zhuǎn)換的字符串 * return 轉(zhuǎn)換后的byte數(shù)組 * throws Exception 本方法不處理任何異常,所有異常全部拋
10、出 * author <a href="mailto:zhangjiaspire-">ZhangJi</a> */ public static byte hexStr2ByteArr(String strIn) throws Exception
11、; byte arrB = strIn.getBytes(); int iLen = arrB.length; /兩個(gè)字符表示一個(gè)字節(jié),所以字節(jié)數(shù)組長(zhǎng)度是字符串長(zhǎng)度除以2 byte arrOut = new byteiLen / 2; for (int i = 0; i < iLen; i =
12、 i + 2) String strTmp = new String(arrB, i, 2); arrOuti / 2 = (byte) Integer.parseInt(strTmp, 16);
13、60; return arrOut; /* * 默認(rèn)構(gòu)造方法,使用默認(rèn)密鑰 * throws Exception */ public DES() thr
14、ows Exception this(strDefaultKey); /* * 指定密鑰構(gòu)造方法 * param strKey 指定的密鑰 * throws Exception */
15、60; public DES(String strKey) throws Exception Security.addProvider(new vider.SunJCE(); Key key = getKey(strKey.getBytes();
16、160; encryptCipher = Cipher.getInstance("DES"); encryptCipher.init(Cipher.ENCRYPT_MODE, key); decryptCipher = Cipher.getInstance("DES");
17、 decryptCipher.init(Cipher.DECRYPT_MODE, key); /* * 加密字節(jié)數(shù)組 * param arrB 需加密的字節(jié)數(shù)組 * return 加密后的字節(jié)數(shù)組 * throws Exception */
18、; public byte encrypt(byte arrB) throws Exception return encryptCipher.doFinal(arrB); /* * 加密字符串 * param strIn 需
19、加密的字符串 * return 加密后的字符串 * throws Exception */ public String encrypt(String strIn) throws Exception return b
20、yteArr2HexStr(encrypt(strIn.getBytes(); /* * 解密字節(jié)數(shù)組 * param arrB 需解密的字節(jié)數(shù)組 * return 解密后的字節(jié)數(shù)組 * throws Exception */ public byte
21、 decrypt(byte arrB) throws Exception return decryptCipher.doFinal(arrB); /* * 解密字符串 * param strIn 需解密的字符串 &
22、#160; * return 解密后的字符串 * throws Exception */ public String decrypt(String strIn) throws Exception return new String(dec
23、rypt(hexStr2ByteArr(strIn); /* * 從指定字符串生成密鑰,密鑰所需的字節(jié)數(shù)組長(zhǎng)度為8位 * 不足8位時(shí)后面補(bǔ)0,超出8位只取前8位 * param arrBTmp 構(gòu)成該字符串的字節(jié)數(shù)組 * return 生成的密鑰 * throws java.lang.
24、Exception */ private Key getKey(byte arrBTmp) throws Exception /創(chuàng)建一個(gè)空的8位字節(jié)數(shù)組(默認(rèn)值為0) byte arrB = new byte8; &
25、#160; /將原始字節(jié)數(shù)組轉(zhuǎn)換為8位 for (int i = 0; i < arrBTmp.length && i < arrB.length; i+) arrBi = arrBTmpi;
26、60; /生成密鑰 Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES"); return key; /*
27、 * 單元測(cè)試方法 * param args */ public static void main(String args) String strOriginal = "1111" String strOp = &qu
28、ot;-de" / 檢查入?yún)€(gè)數(shù) if (args.length = 2 ) strOp = args0 ; strOriginal = args1;
29、0; else System.out.println("Wrong Parameter count , try use "java DES -de|-en 'the string you want to be Encrypted'"");
30、160; System.out.println("Now do Encrypt with "1111""); try DES des = n
31、ew DES(); / 加密測(cè)試 System.out.println("* 加密測(cè)試 *") ; &
32、#160; des.enTest("1111"); / 解密測(cè)試 System.out.println("* 解密測(cè)試 *") ;
33、; des.deTest("0fc7648b53e54cfb"); catch (Exception ex) e
34、x.printStackTrace(); return ; try &
35、#160; if ( strOp.equals("-de") DES des = new DES(); des.deTest(strOriginal);
36、60; else if ( strOp.equals("-en") DES des = new DES();
37、 des.enTest(strOriginal); else
38、; System.out.println("Wrong operater , try use "java DES -de|-en 'the string you want to be Encrypted'""); System.out.println("Now do Encrypt with "1111"");
39、 catch (Exception ex) ex.printStackTrac
40、e(); /* * 單元測(cè)試方法,打印對(duì)指定字符串加密后的字符串 */ private void enTest(String strOriginal) try
41、160; System.out.println("Plain String: " + strOriginal); String strEncrypt= encrypt(strOriginal);
42、 System.out.println("Encrypted String: " + strEncrypt); catch (Exception ex)
43、160; ex.printStackTrace(); /* * 單元測(cè)試方法,打印對(duì)指定字符串解密后的字符串 */ private void deTest(String strOriginal)
44、160; try System.out.println("Encrypted String: " + strOriginal); System.out.println("Encrypted String length = " + strOriginal.len
45、gth(); String strPlain = decrypt(strOriginal); System.out.println("Plain String: " + strPlain);
46、160; catch (Exception ex) ex.printStackTrace(); =c#版的=using System;using System.Text;using System.IO;using System.Security.Cryptography;class Class1static
47、void Main() Console.WriteLine("Encrypt String."); txtKey = "tkGGRmBErvc=" btnKeyGen(); Console.WriteLine("Encrypt Key :0",txtKey); txtIV = "Kl7ZgtM1dvQ=" btnIVGen(); Console.WriteLine("Encrypt IV :0",txtIV); Console.WriteLine(); string txtEncrypted = EncryptString("1111"); Console.WriteLine("Encrypt String : 0",txtEncrypted); string txtOriginal = DecryptString(txtEn
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年湖南客運(yùn)駕駛員考試試題及答案
- 2024年鶴崗道路客運(yùn)輸從業(yè)資格證考試
- 人教A版高中數(shù)學(xué)(必修第一冊(cè))同步講義 1.4充分條件與必要條件(原卷版)
- 《2024年 服務(wù)學(xué)習(xí)視角下的MSW實(shí)習(xí)過(guò)程研究》范文
- 西藏2016年監(jiān)理工程師合同管理:承擔(dān)違約責(zé)任的條件和原則試題
- 解除勞動(dòng)合同關(guān)系證明
- 解除勞動(dòng)合同通知書(shū)范文
- 2024年聚酰亞胺(PI)及改性材料項(xiàng)目合作計(jì)劃書(shū)
- 2024年薯、豆相關(guān)植物加工品項(xiàng)目發(fā)展計(jì)劃
- 2024年?duì)I養(yǎng)強(qiáng)化劑項(xiàng)目合作計(jì)劃書(shū)
- 攀登英語(yǔ)字母Tt-Tick Tock課件
- 人教版三年級(jí)上冊(cè)數(shù)學(xué) 兩位數(shù)乘兩位數(shù)(列豎式計(jì)算)
- 退部申請(qǐng)書(shū)20000字原文4篇
- GB/T 12224-2005鋼制閥門(mén)一般要求
- 成都市中小學(xué)在校學(xué)生健康檢查表
- 首長(zhǎng)保健手冊(cè)(下)-精選
- 5羥色胺再攝取抑制劑對(duì)心血管系統(tǒng)的影響
- 數(shù)學(xué)教師個(gè)人教學(xué)風(fēng)格總結(jié)
- 清廉醫(yī)院創(chuàng)建資料匯編(支委會(huì)會(huì)議和院長(zhǎng)辦公會(huì)議議事規(guī)則)
- 四年級(jí)下冊(cè)美術(shù)教案-9用彩墨畫(huà)魚(yú)-人美版
- 公司戰(zhàn)略規(guī)劃和落地方法之:五看三定工具解析課件
評(píng)論
0/150
提交評(píng)論