




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、package des;/* * 加密過程: * 1.初始置換IP:將明文順序打亂重新排列,置換輸出為64位。 * 2.將置換輸出的64位明文分成左右涼拌,左一半為L0,右一半稱為R0,各32位。 * 3。計算函數(shù)的16輪迭代。 * a)第一輪加密迭代:左半邊輸入L0,右半邊輸入R0:由輪函數(shù)f實現(xiàn)子密鑰K1對R0的加密, * 結果為32位數(shù)據(jù)組f(R0,K1), * b)第二輪加密迭代:左半邊輸入L1=R0,右半邊輸入R1=L0f(R0,K1),由輪函數(shù)f實現(xiàn)子密鑰 * K2對R1的加密,結果為32位數(shù)據(jù)組f(R1,K2),f(R1,K2)與L1模2相加,得到一個32為數(shù)據(jù)組 L1f(R1,
2、K2). * c)第3到16輪迭代分別用密鑰K3,K4K16進行。 4.再經(jīng)過逆初始置換IP-1,將數(shù)據(jù)打亂重排,生成64位密文。 * * 子密鑰生成過程: * 1.將64位的密鑰經(jīng)過PC-1置換生成56位密鑰。 * 2.將56位的密鑰分成左右兩部分,分別進行移位操作(一共進行16輪),產(chǎn)生16個56位長度的子密鑰。 * 3.將16個56位的子密鑰分別進行PC-2置換生成16個48位的子密鑰。 * * 輪函數(shù)f的工作過程: * 1.在第i次加密迭代過程中,擴展置換E對32位的Ri-1的各位通過置換表置換為48位的輸出。 * 2.將該48位的輸出與子密鑰Ki進行異或操作,運算結果經(jīng)過S盒代換運算
3、,得到一個32位比特的輸出。 * 3。該32位比特輸出再經(jīng)過P置換表進行P運算,將其各位打亂重排,生成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
4、, 16, 8, 57, 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,
5、12, 52, 20, 60, 28, 35, 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,
6、30, 22, 14, 6,61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 ; / 56private 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 fi
7、nal int E = 32, 1, 2, 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 ; / 48private 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,
8、27, 3, 9, 19, 13, 30, 6, 22,11, 4, 25 ; / 32private 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,
9、0, 6, 13 , / S_Box2 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,
10、8 , 13, 7, 0, 9, 3, 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,
11、 12, 11, 7, 13, 15, 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,
12、6, 15, 0, 9, 10, 4, 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,
13、9, 7, 5, 10, 6, 1 , 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,
14、2 , 7, 11, 4, 1, 9, 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 ;/* * 對64位明文進行IP置換處理, * param data 需要經(jīng)過初始IP置換的明文 * return 64位經(jīng)過置換處理的明文 */public int IpReplace(int data)in
15、t ipMingWen = 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;
16、for (int i = 0; i 8; i+) for (int j = 0; j 8; j+) IntVa(i * 8) + 7) - j = IntDai % 2;IntDai = IntDai / 2;return IntVa;/* * 功能:對給定的數(shù)組按照offset指定的位數(shù)進行循環(huán)左移,輸出經(jīng)過左移后的數(shù)組 * 作用:將經(jīng)過pc-1置換處理后的56位密鑰先分成左右各28位,然后根據(jù)左移表要求對密鑰進行左移 * param key 經(jīng)過pc-1處理后的56位密鑰 * return 單個56位的子密鑰(初步,未經(jīng)過pc2置換) */public int LeftMove(int k
17、ey,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+) C1i = C0
18、i+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)過PC-1置換后的56位密鑰生成16個56位的子密鑰。再經(jīng)過PC-2置換生成16個48位的子密鑰 * param key 經(jīng)過pc-1置換后的56位密鑰 * return 處理完全的16個48位子密鑰(以二維數(shù)組的形式存儲) */public int SubKeyGenerate(int key)int subKeyAr
19、rayTemp = new int1656;int subKey = new int1648;int K0 = new int56;/ 特別注意:xxxIPi-1等類似變換/對64位的密鑰進行pc-1置換變成56位的密鑰for (int i = 0; i 56; i+) K0i = keyPC_1i - 1; / 密鑰進行PC-1變換/由經(jīng)過pc-1置換生成的56位密鑰經(jīng)過16輪左移,pc-2置換等操作生成16個48位的子密鑰for (int i = 0; i 16; i+) subKeyArrayTempi = LeftMove(K0,LeftMovei);System.arraycopy(
20、subKeyArrayTempi,0 , K0, 0, 56);for (int i = 0; i 16; i+) for (int j = 0; j 48; j+) subKeyij = subKeyArrayTempiPC_2j-1;return subKey;/* * 對64位明文右半部分(32位)進行擴展,以進行與子密鑰異或的操作 * param data32位的經(jīng)過處理的明文 * return擴展后的48位明文 */public int EExpend(int data)int dataExpend = new int48;for (int i = 0; i dataExpend.l
21、ength; i+) dataExpendi = dataEi-1;return dataExpend;/* * 對輸入的數(shù)據(jù)進行按位異或操作后輸出,主要用在為明文右半部分(48位)和子密鑰(48)異或, * 經(jīng)過S盒代換后的f函數(shù)輸出(32位)與明文左半部分(32位)的異或操作 * param data 48位的明文部分 * param key 48位的子密鑰 * return 48位進過異或操作的輸出 */public int XOR(int data1,int data2)int XORResult = new intdata1.length;for (int i = 0; i data
22、1.length; i+) XORResulti = data1i+data2i;if(XORResulti = 2)XORResulti = 0;return XORResult;/* * 將48位的經(jīng)過異或處理的明密文進行S盒代換,然后對產(chǎn)生的32位中間結果進行P代換 * param temp 48位的經(jīng)過異或處理的中間結果 * return 32位的經(jīng)過S盒代換和P置換后的中間結果 */public static int SBoxReplace(int temp)int sBoxResult = new int32;/存儲經(jīng)過S盒代換的32位二進制處理中間結果int pReplaceRe
23、sult = new int32;/存儲經(jīng)過S盒代換和P代換的32位的F函數(shù)處理結果int tempArray = new int86;int tempRowTemp = new int8;int roundCount = temp.length/6;/將48位的經(jīng)過異或處理的中間結果以6個為一組(共分成8組)存儲在二維數(shù)組中for (int i = 0; i roundCount; i+) for (int j = 0; j 6; j+) tempArrayij = temp(i*6)+j;/將分成的8組(每組6個)數(shù)據(jù)根據(jù)S盒代換規(guī)則對每組6個數(shù)據(jù)進行拆分并進行S盒代換tempRowTem
24、pi=S_Boxi(tempArrayi01)+(tempArrayi5)(tempArrayi13)+(tempArrayi22)+(tempArrayi31)+(tempArrayi4);/將生成的8個數(shù)(每個都是經(jīng)過S盒代換后的十進制數(shù))轉換成二進制數(shù),并按順序存儲到結果數(shù)組中for (int j = 0; j 4; j+) sBoxResult(i*4+3)-j=tempRowTempi%2;tempRowTempi = tempRowTempi/2;for (int i = 0; i 32; i+) pReplaceResulti = sBoxResultPi-1;return pR
25、eplaceResult;public int Ip1Replace(int data)int ipMingWen = new int64;for (int i = 0; i ipMingWen.length; i+) ipMingWeni = dataIP_1i-1;return ipMingWen;/* * 實現(xiàn)加密和解密工作 * * param data * param key2 * param flag * return */public int EnAndDecrypt(int data, int flag) int mingwen = IpReplace(data);int min
26、gwenTemp = 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 R1 = new int32;int ER0 = new int48;if(1 = flag)System.arraycopy(mingwen, 0, L0,
27、 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.arraycopy(L1, 0, L0, 0, 32);System.arraycopy(R1, 0, R0, 0, 32);for (int i = 0; i 32; i+)
28、 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+) System.arraycopy(R0, 0, L1, 0, 32);ER0 = EExpend(R0);temp = XOR(ER0,keyArray15 - i);tempSBox = SBoxR
29、eplace(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 = ;System.out.println(明文為:+dataStr
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 軟件測試類型題目及答案
- 8 1 直線和圓-高考數(shù)學真題分類 十年高考
- 《經(jīng)濟與社會》選擇題100題(原卷版)
- 2023-2024學年河南省南陽市六校高二下學期期末考試數(shù)學試題(解析版)
- 2025年秋三年級上冊語文同步教案 語文園地
- 碳中和行業(yè)研究報告
- 自貢統(tǒng)計年鑒-2009-環(huán)境保護主要統(tǒng)計指標解釋
- 佳能公司人員管理制度
- 供水搶修應急管理制度
- 供水設備檢修管理制度
- 文史哲與藝術中的數(shù)學智慧樹知到期末考試答案章節(jié)答案2024年吉林師范大學
- 信息光學智慧樹知到期末考試答案章節(jié)答案2024年北京工業(yè)大學
- 《HSK標準教程1》課件
- 電大財務大數(shù)據(jù)分析編程作業(yè)3
- 諾貝爾生理學或醫(yī)學獎史話智慧樹知到期末考試答案2024年
- 行業(yè)分析報告模板(很全面-非常有用)
- 內分泌系統(tǒng)疾病教學設計教案1
- 法人變更書面催促通知合集3篇
- 廣東省初級中學教育裝備標準
- 售票員崗前培訓
- 教科版六年級下冊科學第一單元《小小工程師》教材分析及全部教案(定稿;共7課時)
評論
0/150
提交評論