1、DES加密实验报告网络安全技术实验姓 名系 别实验地点学 号年级 班实验时间成绩评定教师签字实验项目DES加密解密程序设计与实现实验目的:1、 理解DES加密与解密的程序设计算法思想2、 编写DES加密与解密程序,实现对明文的加密与解密,加深对数据加密与解密的理解,掌握DES加密算法思想,提高网络安全的编程能力。所用软件:TC、VC等其它编程工具实现实验内容:掌握DES算法,编写DES算法。1)初始置换 初始置换在第一轮运算之前执行,对输入分组实施如下表所示的变换。此表应从左向右、从上向下读。例如,初始置换把明文的第58位换到第1位的位置,把第50位换到第2位的位置,把第42位换到第3位的位置
2、,等等。58504234261810260524436282012462544638302214664564840322416857494133251791595143352719113615345372921135635547393123157在将这64位数据分为左右两部分,每部分分别为32位,将左32位留下,将右32位按照下表进行排列32123454567898910111213121314151617161718192021202122232425242526272829282930313212)密钥置换 一开始,由于不考虑每个字节的第8位,DES的密钥由64位减至56位,如下表所示。每
3、个字节第8位可作为奇偶校验位以确保密钥不发生错误。 57494133251791585042342618102595143352719113605244366355473931231576254463830221466153453729211352820124接着,56位密钥被分成两部分,每部分28位。然后,根据轮数,这两部分分别循环左移l位或2位。下表给出了每轮移动的位数。轮12345678910111213141516位数1122222212222221在DES的每一轮中,从56位密钥选出以下表中的48位子密钥(Sub Key),这些子密钥Ki由下面的方式确定。14171124153281
4、562110231912426816727201324152313747553040514533484449395634534642503629323)S盒置换当产生了48位密钥后就可以和右边32位明文进行异或运算了,得到48位的密文。再经过下论的S盒跌带,其功能是把6bit数据变为4bit数据,每个S盒是一个4行、16列的表。盒中的每一项都是一个4位的数。S盒的6个位输入确定了其对应的输出在哪一行哪一列。下面列出所有的8个S盒。 1441312151183106125907015741421311061211953841148136211151297310501512824917511314
5、100613115181461134972131205103134715281412011069115014711104131581269321513810131542116712051492100914631551131271142813709346102851412115113649815301112125101471101306987415143115212371314306910128511124151381156503472121101491069012171315131452843150610138945111272144212417101168531513014914112124
6、713150151039864211110137815912563014181271142136150910453512110159268013341475111015427129561131401138914155281237041011311643212951510111417608136411214150813312975106113011749110143512215861411131237141015680592611138141079501514231271328461511110931450127115138103741256110149271141912142061013153
7、5821147410813151290356118假定将S盒的6位的输入标记为b1、b2、b3、b4、b5、b6。则b1和b6组合构成了一个2位的数,从0到3,它对应着表中的一行。从b2到b5构成了一个4位的数,从0到15,对应着表中的一列。 例如,假设第6个S盒的输入(即异或函数的第31位到36位)为110011。第1位和最后一位组合形成了11,它对应着第6个S盒的第三行。中间的4位组合在一起形成了1001,它对应着同一个S盒的第9列。S盒6的第三行第9列处的数是14(记住,行、列的记数均从0开始而不是从1开始),则值1110就代替了110011。4)P盒置换 S盒代替运算后的32位输出依照
8、P盒进行置换。该置换把每输入位映射到输出位,任意一位不能被映射两次,也不能被略去,这个置换叫做直接置换。下表给出了每位移到的位置。16720212912281711523265183110282414322739191330622114255)再次异或运算最后,将P盒置换的结果与最初的64位分组的左半部分异或,然后左、右半部分交换,接着开始另一轮。6)当进行到16论后,最终进行一次末置换,形成密文4084816562464323974715552363313864614542262303754513532161293644412522060283534311511959273424210501
9、8582633141949175725实验步骤:using System;using System.Collections.Generic;using System.Windows.Forms;using System.Text;namespace des class Des static readonly byte BitIP = 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, 56, 48, 4
10、0, 32, 24, 16, 8, 0, 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6 ; static readonly byte BitCP = 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, 3
11、4, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25, 32, 0, 40, 8, 48, 16, 56, 24 ; static readonly int BitExp = 31, 0, 1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 8, 9,10, 11,12,11,12,13,14,15,16,15,16,17,18,19,20,19,20, 21,22,23,24,23,24,25,26,27,28,27,28,29,30,31, 0 ; static readonly byte BitPM = 15,
12、 6,19,20,28,11,27,16, 0,14,22,25, 4,17,30, 9, 1, 7,23,13,31,26, 2, 8,18,12,29, 5,21,10, 3,24 ; static readonly byte , sBox = 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,
13、 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 , 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 , 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7,
14、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, 2, 12 , 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,
15、 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 , 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,
16、4, 5, 3 , 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 , 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13, 0, 11, 7, 4, 9,
17、 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 , 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,
18、5, 8, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 ; static readonly byte BitPMC1 = 56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19,
19、11, 3 ; static readonly byte BitPMC2 = 13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9, 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1, 40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47, 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 ; enum TDesMode dmEncry, dmDecry ; byte SubKey; public Des() SubKey = new byte
20、16; for (int i = 0; i SubKey.Length; i+) SubKeyi = new byte 6; void initPermutation(byte inData) byte newData = new byte 8; for (int i = 0; i 3 & (1 3 |= (byte)(1 (7 - (i & 0x07); Array.Copy(newData, inData, 8); void conversePermutation(byte inData) byte newData = new byte 8; for (int i = 0; i 3 & (
21、1 3 |= (byte)(1 (7 - (i & 0x07); Array.Copy(newData, inData, 8); void expand(byte inData, byte outData) Array.Clear(outData, 0, 6); for (int i = 0; i 3 & (1 3 |= (byte)(1 (7 - (i & 0x07); void permutation(byte inData) byte newData = new byte 4; for (int i = 0; i 3 & (1 3 |= (byte)(1 1) | (inByte & 0
22、x01) 4); return (byte)(sBoxs,c & 0x0f); void permutationChoose1(byte inData, byte outData) Array.Clear(outData, 0, 7); for (int i = 0; i 3 & (1 3 |= (byte)(1 (7 - (i & 0x07); void permutationChoose2(byte inData, byte outData) Array.Clear(outData, 0, 6); for (int i = 0; i 3 & (1 3 |= (byte)(1 (7 - (i & 0x07); void cycleMove(
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1