DES加密算法實(shí)驗(yàn)報(bào)告_第1頁(yè)
DES加密算法實(shí)驗(yàn)報(bào)告_第2頁(yè)
DES加密算法實(shí)驗(yàn)報(bào)告_第3頁(yè)
DES加密算法實(shí)驗(yàn)報(bào)告_第4頁(yè)
DES加密算法實(shí)驗(yàn)報(bào)告_第5頁(yè)
已閱讀5頁(yè),還剩9頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、蘇 州 科 技 學(xué) 院實(shí) 驗(yàn) 報(bào) 告學(xué)生姓名:楊劉濤 學(xué) 號(hào): 指導(dǎo)教師:陶滔 劉學(xué)書(shū) 實(shí)驗(yàn)地點(diǎn):計(jì)算機(jī)學(xué)院大樓東309 實(shí)驗(yàn)時(shí)間:2015-04-20一、 實(shí)驗(yàn)室名稱:軟件實(shí)驗(yàn)室 二、實(shí)驗(yàn)項(xiàng)目名稱:DES加解密算法實(shí)現(xiàn)三、實(shí)驗(yàn)學(xué)時(shí):4學(xué)時(shí)四、實(shí)驗(yàn)原理:DES算法由加密、子密鑰和解密的生成三部分組成?,F(xiàn)將DES算法介紹如下。1加密DES算法處理的數(shù)據(jù)對(duì)象是一組64比特的明文串。設(shè)該明文串為m=m1m2m64 (mi=0或1)。明文串經(jīng)過(guò)64比特的密鑰K來(lái)加密,最后生成長(zhǎng)度為64比特的密文E。其加密過(guò)程圖示如下:圖2-1:DES算法加密過(guò)程對(duì)DES算法加密過(guò)程圖示的說(shuō)明如下:待加密的64比特明文

2、串m,經(jīng)過(guò)IP置換(初始置換)后,得到的比特串的下標(biāo)列表如下:表2-1:得到的比特串的下標(biāo)列表IP58504234261810260524436282012462544638302214664564840322416857494133251791595143352719113615345372921135635547393123157該比特串被分為32位的L0和32位的R0兩部分。R0子密鑰K1(子密鑰的生成將在后面講)經(jīng)過(guò)變換f(R0,K1)(f變換將在下面講)輸出32位的比特串f1,f1與L0做不進(jìn)位的二進(jìn)制加法運(yùn)算。運(yùn)算規(guī)則為: f1與L0做不進(jìn)位的二進(jìn)制加法運(yùn)算后的結(jié)果賦給R1,R0則

3、原封不動(dòng)的賦給L1。L1與R0又做與以上完全相同的運(yùn)算,生成L2,R2 一共經(jīng)過(guò)16次運(yùn)算。最后生成R16和L16。其中R16為L(zhǎng)15與f(R15,K16)做不進(jìn)位二進(jìn)制加法運(yùn)算的結(jié)果,L16是R15的直接賦值。R16與L16合并成64位的比特串。值得注意的是R16一定要排在L16前面。R16與L16合并后成的比特串,經(jīng)過(guò)置換IP-1(終結(jié)置換)后所得比特串的下標(biāo)列表如下:表2-2:置換后所得比特串的下標(biāo)列表IP-1408481656246432397471555236331386461454226230375451353216129364441252206028353431151195927

4、34242105018582633141949175725經(jīng)過(guò)置換IP-1后生成的比特串就是密文e。變換f(Ri-1,Ki):它的功能是將32比特的輸入再轉(zhuǎn)化為32比特的輸出。其過(guò)程如圖2-2所示:圖2-2:將32比特的輸入再轉(zhuǎn)化為32比特的輸出 f變換說(shuō)明:輸入Ri-1(32比特)經(jīng)過(guò)變換E(擴(kuò)展置換E)后,膨脹為48比特。膨脹后的比特串的下標(biāo)列表如下:表2-3:膨脹后的比特串的下標(biāo)列表E:3212345456789891011121312131415161716171819202120212223242524252627282928293031321膨脹后的比特串分為8組,每組6比特。各組

5、經(jīng)過(guò)各自的S盒后,又變?yōu)?比特(具體過(guò)程見(jiàn)后),合并后又成為32比特。該32比特經(jīng)過(guò)P變換(壓縮置換P)后,其下標(biāo)列表如下:表2-4:壓縮置換P后的下標(biāo)列表P:1672021291228171152326518311028241432273919133062211425經(jīng)過(guò)P變換后輸出的比特串才是32比特的f(Ri-1,Ki).S盒的變換過(guò)程: 任取一S盒。見(jiàn)圖2-3:圖2-3在其輸入b1,b2,b3,b4,b5,b6中,計(jì)算出x=b1*2+b6, y=b5+b4*2+b3*4+b2*8,再?gòu)腟i表中查出x 行,y 列的值Sxy。將Sxy化為二進(jìn)制,即得Si盒的輸出。(S表如圖2-4所示)圖2

6、-4以上是DES算法加密原理五、實(shí)驗(yàn)?zāi)康模毫私釪ES加密算法及原理,掌握其基本應(yīng)用。六、實(shí)驗(yàn)內(nèi)容:了解DES加密算法及原理,掌握其基本應(yīng)用,利用java編程實(shí)現(xiàn)。七、實(shí)驗(yàn)器材(設(shè)備、元器件):(1) PC(2) Windows 8.1系統(tǒng)平臺(tái)(3) java程序開(kāi)發(fā)環(huán)境。八、源代碼:package WindowsDemo;public class DesUtil byte bytekey; public DesUtil(String strKey) this.bytekey = strKey.getBytes(); / 聲明常量字節(jié)數(shù)組 private static final int IP

7、= 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, 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 ; / 64 private static final int IP_1 = 40, 8, 48, 16

8、, 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, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 ; / 64 private static final int PC_1 = 57, 49, 41, 33, 25, 17, 9, 1

9、, 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, 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, 2

10、7, 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 ; / 48 private static final 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,

11、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, 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

12、, 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_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,

13、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, 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,

14、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, 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 ,

15、 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, 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

16、, 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, 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

17、, 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, 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,

18、 2, 2, 2, 2, 1, 2, 2, 2,2, 2, 2, 1 ; / 左移位置列表 private byte UnitDes(byte des_key, byte des_data, int flag) / 檢測(cè)輸入?yún)?shù)格式是否正確,錯(cuò)誤直接返回空值(null) if (des_key.length != 8) | (des_data.length != 8)| (flag != 1) & (flag != 0) throw new RuntimeException(Data Format Error !); int flags = flag;/ 二進(jìn)制加密密鑰 int keydata

19、 = new int64;/ 二進(jìn)制加密數(shù)據(jù) int encryptdata = new int64; / 加密操作完成后的字節(jié)數(shù)組 byte EncryptCode = new byte8;/ 密鑰初試化成二維數(shù)組 int KeyArray = new int1648;/ 將密鑰字節(jié)數(shù)組轉(zhuǎn)換成二進(jìn)制字節(jié)數(shù)組 keydata = ReadDataToBirnaryIntArray(des_key);/ 將加密數(shù)據(jù)字節(jié)數(shù)組轉(zhuǎn)換成二進(jìn)制字節(jié)數(shù)組 encryptdata = ReadDataToBirnaryIntArray(des_data);/ 初試化密鑰為二維密鑰數(shù)組 KeyInitializ

20、e(keydata, KeyArray); / 執(zhí)行加密解密操作 EncryptCode = Encrypt(encryptdata, flags, KeyArray); return EncryptCode; / 初試化密鑰數(shù)組 private void KeyInitialize(int key, int keyarray) int i; int j; int K0 = new int56;/ 特別注意:xxxIPi-1等類似變換 for (i = 0; i 56; i+) K0i = keyPC_1i - 1; / 密鑰進(jìn)行PC-1變換 for (i = 0; i 16; i+) Lef

21、tBitMove(K0, LeftMovei); / 特別注意:xxxIPi-1等類似變換 for (j = 0; j 48; j+) keyarrayij = K0PC_2j - 1; / 生成子密鑰keyarrayij / 執(zhí)行加密解密操作 private byte Encrypt(int timeData, int flag, int keyarray) int i; byte encrypt = new byte8; int flags = flag; int M = new int64; int MIP_1 = new int64;/ 特別注意:xxxIPi-1等類似變換 for (

22、i = 0; i 64; i+) Mi = timeDataIPi - 1; / 明文IP變換 if (flags = 1) / 加密 for (i = 0; i -1; i-) LoopF(M, i, flags, keyarray); for (i = 0; i 64; i+) MIP_1i = MIP_1i - 1; / 進(jìn)行IP-1運(yùn)算 GetEncryptResultOfByteArray(MIP_1, encrypt);/ 返回加密數(shù)據(jù) return encrypt; private int ReadDataToBirnaryIntArray(byte intdata) int i

23、; int j; / 將數(shù)據(jù)轉(zhuǎn)換為二進(jìn)制數(shù),存儲(chǔ)到數(shù)組 int IntDa = new int8; for (i = 0; i 8; i+) IntDai = intdatai; if (IntDai 0) IntDai += 256; IntDai %= 256; int IntVa = new int64; for (i = 0; i 8; i+) for (j = 0; j 8; j+) IntVa(i * 8) + 7) - j = IntDai % 2; IntDai = IntDai / 2; return IntVa; private void LeftBitMove(int k

24、, int offset) int i; / 循環(huán)移位操作函數(shù) int c0 = new int28; int d0 = new int28; int c1 = new int28; int d1 = new int28; for (i = 0; i 28; i+) c0i = ki; d0i = ki + 28; if (offset = 1) for (i = 0; i 27; i+) / 循環(huán)左移一位 c1i = c0i + 1; d1i = d0i + 1; c127 = c00; d127 = d00; else if (offset = 2) for (i = 0; i 26; i

25、+) / 循環(huán)左移兩位 c1i = c0i + 2; d1i = d0i + 2; c126 = c00; d126 = d00; c127 = c01; d127 = d01; for (i = 0; i 28; i+) ki = c1i; ki + 28 = d1i; private void LoopF(int M, int times, int flag, int keyarray) int i; int j; int L0 = new int32; int R0 = new int32; int L1 = new int32; int R1 = new int32; int RE =

26、 new int48; int S = new int86; int sBoxData = new int8; int sValue = new int32; int RP = new int32; for (i = 0; i 32; i+) L0i = Mi; / 明文左側(cè)的初始化 R0i = Mi + 32; / 明文右側(cè)的初始化 for (i = 0; i 48; i+) REi = R0Ei - 1; / 經(jīng)過(guò)E變換擴(kuò)充,由32位變?yōu)?8位 REi = REi + keyarraytimesi; / 與KeyArraytimesi按位作不進(jìn)位加法運(yùn)算 if (REi = 2) REi

27、= 0; for (i = 0; i 8; i+) / 48位分成8組 for (j = 0; j 6; j+) Sij = RE(i * 6) + j; / 下面經(jīng)過(guò)S盒,得到8個(gè)數(shù) sBoxDatai = S_Boxi(Si0 1) + Si5(Si1 3) + (Si2 2) + (Si3 1) + Si4; / 8個(gè)數(shù)變換輸出二進(jìn)制 for (j = 0; j 4; j+) sValue(i * 4) + 3) - j = sBoxDatai % 2; sBoxDatai = sBoxDatai / 2; for (i = 0; i 32; i+) RPi = sValuePi - 1

28、; / 經(jīng)過(guò)P變換 L1i = R0i; / 右邊移到左邊 R1i = L0i + RPi; if (R1i = 2) R1i = 0; / 重新合成M,返回?cái)?shù)組M / 最后一次變換時(shí),左右不進(jìn)行互換。此處采用兩次變換實(shí)現(xiàn)不變 if (flag = 0) & (times = 0) | (flag = 1) & (times = 15) Mi = R1i; Mi + 32 = L1i; else Mi = L1i; Mi + 32 = R1i; private void GetEncryptResultOfByteArray(int data, byte value) int i; int j

29、; / 將存儲(chǔ)64位二進(jìn)制數(shù)據(jù)的數(shù)組中的數(shù)據(jù)轉(zhuǎn)換為八個(gè)整數(shù)(byte) for (i = 0; i 8; i+) for (j = 0; j 8; j+) valuei += (data(i 3) + j (7 - j); for (i = 0; i 128) valuei -= 255; private byte ByteDataFormat(byte data, int flag) int len = data.length; int padlen = 8 - (len % 8); int newlen = len + padlen; byte newdata = new bytenewl

30、en; System.arraycopy(data, 0, newdata, 0, len); for (int i = len; i newlen; i+) newdatai = (byte) padlen; return newdata; public byte DesEncrypt(byte des_data, int flag) byte format_key = ByteDataFormat(bytekey, flag); byte format_data = ByteDataFormat(des_data, flag); int datalen = format_data.length; int unitcount = datalen / 8; byte result_data = new bytedatalen; for (int i = 0; i = 1) & (delete_len = 8

溫馨提示

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

評(píng)論

0/150

提交評(píng)論