版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、package des;/* 加密過(guò)程 :* 1、初始置換IP:將明文順序打亂重新排列,置換輸出為64位。* 2、將置換輸出的64位明文分成左右涼拌,左一半為L(zhǎng)0,右一半稱為R0各32位。* 3。計(jì)算函數(shù)的 16 輪迭代。* a)第一輪加密迭代:左半邊輸入L0,右半邊輸入R0:由輪函數(shù)f實(shí)現(xiàn)子密鑰K1對(duì)R0的加 密,* 結(jié)果為 32 位數(shù)據(jù)組 f(R0,K1),* b)第二輪加密迭代:左半邊輸入L仁R0右半邊輸入R1=L0 f(R0,K1),由輪函數(shù)f實(shí)現(xiàn)子密 鑰* K2對(duì)R1的加密結(jié)果為32位數(shù)據(jù)組f(R1,K2),f(R1,K2)與 L1模2相加,得到一個(gè)32為 數(shù)據(jù)組 L1 f(R1,K
2、2)、* c)第3到16輪迭代分別用密鑰K3,K4K16進(jìn)行。4、再經(jīng)過(guò)逆初始置換IP-1,將數(shù)據(jù)打亂重排 ,生成 64 位密文。* 子密鑰生成過(guò)程 :* 1、將64位的密鑰經(jīng)過(guò) PC-1置換生成56位密鑰。* 2、將 56位的密鑰分成左右兩部分 ,分別進(jìn)行移位操作 (一共進(jìn)行 16輪),產(chǎn)生 16個(gè)56位 長(zhǎng)度的子密鑰。* 3、將16個(gè)56位的子密鑰分別進(jìn)行 PC-2置換生成16個(gè)48位的子密鑰。*輪函數(shù)f的工作過(guò)程:* 1、在第i次加密迭代過(guò)程中,擴(kuò)展置換E對(duì)32位的Ri-1的各位通過(guò)置換表置換為48位的輸出。* 2、將該48位的輸出與子密鑰 Ki進(jìn)行異或操作,運(yùn)算結(jié)果經(jīng)過(guò)S盒代換運(yùn)算,得
3、到一個(gè)32 位比特的輸出。* 3。該32位比特輸出再經(jīng)過(guò) P置換表進(jìn)行P運(yùn)算,將其各位打亂重排,生成32位的輸出。* author Ouyang*/public class Des int byteKey;public Des(int byteKey) this、 byteKey = byteKey; private static final int IP = 58, 50, 42, 34, 26, 18, 10, 2, 60, 52,44, 36, 28,20, 12, 4, 62,54,46,38,30, 22, 14, 6, 64, 56, 48,40, 32, 24,16, 8, 57
4、, 49,41,33,25,17, 9,1, 59, 51, 43, 35,27, 19, 11,3, 61, 53, 45,37,29,21,13, 5,63, 55, 47, 39, 31,23, 15, 7 ;/ 64private static final int IP_1 = 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45,13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35
5、, 3, 43, 11,51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49,17, 57, 25 ; / 64private static final int PC_1 = 57, 49, 41, 33, 25, 17, 9, 1, 58, 50,42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44,36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6,61, 53,
6、45, 37, 29, 21, 13, 5, 28, 20, 12, 4 ; / 56 private static final int PC_2 = 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21,10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47,55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36,29, 32 ; / 48private static final int E = 32, 1, 2,
7、 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9,10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20,21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 ; / 48 private static final int P = 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23,26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30
8、, 6, 22,11, 4, 25 ; / 32 private static final int S_Box = / S- 盒/ S_Box1 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 , 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 , 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 , 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 , / S_Box
9、2 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 , 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 , 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 , 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 , / S_Box3 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 , 13, 7, 0, 9, 3
10、, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 , 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 , 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 , / S_Box4 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 , 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 , 10, 6, 9, 0, 12, 11, 7, 13, 15
11、, 1, 3, 14, 5, 2, 8, 4 , 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 , / S_Box5 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 , 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 , 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 , 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4
12、, 5, 3 , / S_Box6 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 , 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 , 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 , 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 , / S_Box7 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1
13、, 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 , 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 , 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 , / S_Box8 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 , 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 , 7, 11, 4, 1, 9
14、, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 , 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 ; private static final int LeftMove = 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2,2, 2, 2, 1 ;* 對(duì) 64 位明文進(jìn)行 IP 置換處理 ,* param data 需要經(jīng)過(guò)初始 IP 置換的明文* return 64 位經(jīng)過(guò)置換處理的明文*/public int IpReplace(int data) int ipMingWen =
15、new int64;for (int i = 0; i ipMingWen 、 length; i+) ipMingWeni = dataIPi-1;return ipMingWen;public static int StringToBits(String data) byte test = data 、 getBytes(); int IntVa = new int64;int IntDa = new int8;for (int i = 0; i 8; i+) IntDai = testi;if (IntDai 0) IntDai += 256;IntDai %= 256;for (int
16、 i = 0; i 8; i+) for (int j = 0; j 8; j+) IntVa(i * 8) + 7) - j = IntDai % 2; IntDai = IntDai / 2;return IntVa;/*,輸出經(jīng)過(guò)左移后的數(shù)組28 位 ,然后根據(jù)左移表要求* 功能 :對(duì)給定的數(shù)組按照 offset 指定的位數(shù)進(jìn)行循環(huán)左移* 作用 :將經(jīng)過(guò) pc-1 置換處理后的 56 位密鑰先分成左右各 對(duì)密鑰進(jìn)行左移* param key 經(jīng)過(guò) pc-1 處理后的 56 位密鑰* return 單個(gè) 56 位的子密鑰 ( 初步 ,未經(jīng)過(guò) pc2 置換 )*/public int Lef
17、tMove(int key,int offset)int subKey = new int56;int C0 = new int28;int D0 = new int28;int C1 = new int28;int D1 = new int28;for (int i = 0; i 28; i+) C0i = keyi;D0i = keyi+28;if(offset = 1)for(int i = 0; i 27;i+)C1i = C0i+1;D1i = D0i+1;C127 = C00;D127 = D00;else if(offset = 2)for (int i = 0; i 26; i
18、+) C1i = C0i+2;D1i = D0i+2;C126 = C00;D126 = D00;C127 = C01;D127 = D01;for (int i = 0; i 28; i+) subKeyi = C1i; subKeyi+28 = D1i;return subKey;*根據(jù)經(jīng)過(guò)PC-1置換后的56位密鑰生成16個(gè)56位的子密鑰。再經(jīng)過(guò)PC-2置換生成 16 個(gè) 48 位的子密鑰* param key 經(jīng)過(guò) pc-1 置換后的 56 位密鑰* return 處理完全的 16 個(gè) 48 位子密鑰 (以二維數(shù)組的形式存儲(chǔ) )*/public int SubKeyGenerate(i
19、nt key)int subKeyArrayTemp = new int1656;int subKey = new int1648;int KO = new int56;特別注意:xxxIPi-1等類似變換/對(duì) 64 位的密鑰進(jìn)行 pc-1 置換變成 56 位的密鑰 for (int i = O; i 56; i+) K0i = keyPC_1i - 1; / 密鑰進(jìn)行 PC-1 變換/ 由經(jīng)過(guò) pc-1 置換生成的 56 位密鑰經(jīng)過(guò) 16 輪左移 ,pc-2 置換等操作生成 16 個(gè) 48 位的子密鑰for (int i = O; i 16; i+) subKeyArrayTempi = L
20、eftMove(KO,LeftMovei); System、arraycopy(subKeyArrayTempi,O , KO, O, 56);for (int i = O; i 16; i+) for (int j = O; j 48; j+) subKeyij = subKeyArrayTempiPC_2j-1;return subKey;/* 對(duì) 64 位明文右半部分 (32 位)進(jìn)行擴(kuò)展 ,以進(jìn)行與子密鑰異或的操作* param data 32 位的經(jīng)過(guò)處理的明文* return 擴(kuò)展后的 48 位明文*/public int EExpend(int data) int dataExp
21、end = new int48;for (int i = O; i dataExpend 、 length; i+) dataExpendi = dataEi-1;return dataExpend;/* 對(duì)輸入的數(shù)據(jù)進(jìn)行按位異或操作后輸出,主要用在為明文右半部分(48 位 )與子密鑰(48)異或 ,*經(jīng)過(guò)S盒代換后的f函數(shù)輸出(32位)與明文左半部分(32位)的異或操作* param data 48 位的明文部分* param key 48 位的子密鑰* return 48 位進(jìn)過(guò)異或操作的輸出*/public int XOR(int data1,int data2)int XORResul
22、t = new intdata1 、 length;for (int i = 0; i data1 、 length; i+) XORResulti = data1i+data2i; if(XORResulti = 2)XORResulti = 0;return XORResult;/* 將 48 位的經(jīng)過(guò)異或處理的明密文進(jìn)行 S 盒代換 ,然后對(duì)產(chǎn)生的 32 位中間結(jié)果進(jìn)行 P 代換* param temp 48 位的經(jīng)過(guò)異或處理的中間結(jié)果* return 32位的經(jīng)過(guò)S盒代換與P置換后的中間結(jié)果*/public static int SBoxReplace(int temp)int sBo
23、xResult = new int32; 存儲(chǔ)經(jīng)過(guò) S盒代換的32位二進(jìn)制處理中間結(jié)果int pReplaceResult = new int32; 存儲(chǔ)經(jīng)過(guò) S盒代換與 P代換的32位的F函數(shù)處 理結(jié)果int tempArray = new int86;int tempRowTemp = new int8;int roundCount = temp 、 length/6;/將48位的經(jīng)過(guò)異或處理的中間結(jié)果以 6個(gè)為一組(共分成 8組)存儲(chǔ)在二維數(shù)組中 for (int i = 0; i roundCount; i+) for (int j = 0; j 6; j+) tempArrayij
24、= temp(i*6)+j; /將分成的8組(每組6個(gè))數(shù)據(jù)根據(jù)S盒代換規(guī)則對(duì)每組 6個(gè)數(shù)據(jù)進(jìn)行拆分并 進(jìn)行S盒代換tempRowTempi=S_Boxi(tempArrayi01)+(tempArrayi5) (tempArrayi13)+(tempArrayi22)+(tempArrayi31)+(tempArrayi4);/將生成的8個(gè)數(shù)(每個(gè)都就是經(jīng)過(guò) S盒代換后的十進(jìn)制數(shù))轉(zhuǎn)換成二進(jìn)制數(shù),并 按順序存儲(chǔ)到結(jié)果數(shù)組中for (int j = 0; j 4; j+) sBoxResult(i*4+3)-j=tempRowTempi%2; tempRowTempi = tempRowTem
25、pi/ 2;for (int i = 0; i 32; i+) pReplaceResulti = sBoxResultPi-1;return pReplaceResult;public int Ip1Replace(int data) int ipMingWen = new int64; for (int i = 0; i ipMingWen 、 length; i+) ipMingWeni = dataIP_1i-1;/*return ipMingWen;實(shí)現(xiàn)加密與解密工作* param data* param key2* param flag* return*/public int En
26、AndDecrypt(int data, int flag) int mingwen = IpReplace(data); int mingwenTemp = new int64; int miwen = IpReplace(data); int miwenTemp = new int64; int keyArray = SubKeyGenerate(byteKey); int temp = new int48; int tempSBox = new int32; int L0 = new int32; int R0 = new int32; int L1 = new int32; int R
27、1 = new int32; int ER0 = new int48;if(1 = flag)System、 arraycopy(mingwen, 0, L0, 0, 32); System、 arraycopy(mingwen, 32, R0, 0, 32); for (int i = 0; i 16; i+) System、 arraycopy(R0, 0, L1, 0, 32); ER0 = EExpend(R0); temp = XOR(ER0,keyArrayi); tempSBox = SBoxReplace(temp); R1 = XOR(L0,tempSBox);System、
28、 arraycopy(L1, 0, L0, 0, 32); System、 arraycopy(R1, 0, R0, 0, 32);for (int i = 0; i 32; i+) mingwenTempi = R1i; mingwenTempi+32 = L1i;mingwen = Ip1Replace(mingwenTemp);else if(0 = flag)System、 arraycopy(mingwen, 0, L0, 0, 32);System、 arraycopy(mingwen, 32, R0, 0, 32); for (int i = 0; i 16; i+) Syste
29、m、 arraycopy(R0, 0, L1, 0, 32);ER0 = EExpend(R0);temp = XOR(ER0,keyArray15 - i); tempSBox = SBoxReplace(temp); R1 = XOR(L0,tempSBox); System、 arraycopy(L1, 0, L0, 0, 32); System、 arraycopy(R1, 0, R0, 0, 32);for (int i = 0; i 32; i+) miwenTempi = R1i; miwenTempi+32 = L1i;miwen = Ip1Replace(miwenTemp); return (flag=1) ? mingwen : miwen; public static void main(String args) String dataString = 10101001;System、 out 、 println( 明文為 :+dataString); int data = StringToBits(dataStr
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 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ì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 冀少版八年級(jí)生物上冊(cè)第三單元第二節(jié)根對(duì)水分的吸收課件
- 《媽媽睡了》教學(xué)設(shè)計(jì)
- 《學(xué)習(xí)探究-計(jì)算機(jī)硬件及其故障》教案
- 印刷工程監(jiān)理管理與評(píng)標(biāo)規(guī)范
- 定州市公園環(huán)境衛(wèi)生維護(hù)辦法
- 知識(shí)產(chǎn)權(quán)定向合作協(xié)議
- 電力工程師解除聘用合同模板
- 紡織品業(yè)保密承諾書樣本
- 水利工程保險(xiǎn)合同范本
- 深圳汽車4S店租賃合同模板
- 科捷物流介紹(中文版)ppt課件
- 軍事地形學(xué)地形圖基本知識(shí)
- 2022版義務(wù)教育(生物學(xué))課程標(biāo)準(zhǔn)(含2022年修訂和新增部分)
- 六年級(jí)綜合實(shí)踐活動(dòng)課件-珍愛(ài)生命遠(yuǎn)離毒品 全國(guó)通用(共24張PPT)
- 建設(shè)工程竣工消防驗(yàn)收記錄表(DOC36頁(yè))
- 沉井專項(xiàng)施工方案DOC
- 切削力計(jì)算參考模板
- 一年級(jí)海洋教育教案
- 聚氨酯硬泡沫配方及計(jì)算
- 關(guān)于大學(xué)生體育鍛煉情況調(diào)查報(bào)告
- 運(yùn)動(dòng)的描述-主題單元設(shè)計(jì)
評(píng)論
0/150
提交評(píng)論