DES 加解密算法(java和c#版)_第1頁(yè)
DES 加解密算法(java和c#版)_第2頁(yè)
DES 加解密算法(java和c#版)_第3頁(yè)
DES 加解密算法(java和c#版)_第4頁(yè)
DES 加解密算法(java和c#版)_第5頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論