




已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
DES算法由加密、解密和子密钥的生成三部分组成。 DES算法处理的数据对象是一组64比特的明文串。设该明文串为m=m1m2m64 (mi=0或1)。明文串经过64比特的密钥K来加密,最后生成长度为64比特的密文E。 加密过程主要涉及 明文串的IP置换,F函数,子密钥的生成,SBox置换,IP逆置换。 DES算法密钥有效位是56位,即用56位的密钥来加密64位的数据。加密期间56位密钥要扩展成64位的密钥,加入奇偶效验位。我想主要是用来传输密钥用的吧,怕传输过程中出错。当然传输过程中还得对密钥加密(一般使用非对称加密)。 uchar VerifyOddEven(uchar x) unsigned int n; uchar ucTemp =x; x = 1; for(n=0; x; n+) x &= x-1; if (n%2 = 0) if (!(ucTemp & 1) ucTemp += 1; return ucTemp;void ChgKey56To64(uchar *pszKey,uchar *pszKeyOut) uchar ucSave = 0; uchar ucTemp,ucFill; for (int i=0; ii; ucFill = ucSave + ucTemp; pszKeyOuti = VerifyOddEven(ucFill); ucSave = pszKeyi 4; if (ucTemp 10) ucDisp = ucTemp+0; coutucDisp; else ucDisp = ucTemp+55; coutucDisp; ucTemp = ucData&15; if (ucTemp 10) ucDisp = ucTemp+0; coutucDisp; else ucDisp = ucTemp+55; coutucDisp; void DispHexBySz(uchar *pszString) int i; for(i=0; istrlen(char *)pszString); i+) DispHex(pszStringi); coutendl;void DispHexByUl(ulong ulData) int i; ulong ulTemp; uchar ucTemp; for(i=0; i(3-i)*8); ucTemp = ulTemp & 0x000000FF; DispHex(ucTemp); coutendl; 接下来就是置换算法了,我写了一个专门的置换函数REPLACECORE();可用此来进行64至56,64To64,56To48的置换。 void Replace64To64(ulong ulDataL,ulong ulDataR,ulong *ulDataOutL,ulong *ulDataOutR,int Array,int nArrLen,int core) ReplaceCore(ulDataL,ulDataR,ulDataOutL,ulDataOutR,Array,nArrLen,core,0,0x80000000);void Replace64To56(ulong ulDataL,ulong ulDataR,ulong *ulDataOutL,ulong *ulDataOutR,int Array,int nArrLen,int core) ReplaceCore(ulDataL,ulDataR,ulDataOutL,ulDataOutR,Array,nArrLen,core,4,0x80000000);void Replace56To48(ulong ulDataL,ulong ulDataR,ulong *ulDataOutL,ulong *ulDataOutR,int Array,int nArrLen,int core) ReplaceCore(ulDataL,ulDataR,ulDataOutL,ulDataOutR,Array,nArrLen,core,8,0x80000000);void ReplaceCore(ulong ulDataL,ulong ulDataR,ulong *ulDataOutL,ulong *ulDataOutR,int Array,int nArrLen,int core,int move,ulong ulComTran) int i; ulong ulTemp; ulong ulCompute = ulComTran; int nLocation; *ulDataOutL = 0; *ulDataOutR = 0; for (i=0; i core) nLocation = Arrayi-core-1; ulCompute = nLocation; ulTemp = ulDataR; ulTemp &= ulCompute; if (nLocation=i) ulTemp = (i-nLocation); *ulDataOutL += ulTemp; ulCompute = ulComTran; else nLocation = Arrayi-1; ulCompute = nLocation; ulTemp = ulDataL; ulTemp &= ulCompute; if (nLocation=i) ulTemp = (i-nLocation); *ulDataOutL += ulTemp; ulCompute = ulComTran; for (i=nArrLen/2+move; i core) nLocation = Arrayi-move-core-1; ulCompute = nLocation; ulTemp = ulDataR; ulTemp &= ulCompute; if (nLocation=(i-32) ulTemp = (i-32)-nLocation); *ulDataOutR += ulTemp; ulCompute = ulComTran; else nLocation = Arrayi-move-1; ulCompute = nLocation; ulTemp = ulDataL; ulTemp &= ulCompute; if (nLocation=(i-32) ulTemp = (i-32)-nLocation); *ulDataOutR += ulTemp; ulCompute = ulComTran; void Replace32To48(ulong ulDataR,ulong *ulDataOutL,ulong *ulDataOutR,int Array,int nArrLen) int i,j; ulong ulTemp; ulong ulCompute=0x80000000; int nLocation; *ulDataOutL = 0; *ulDataOutR = 0; for (i=0; i nLocation; ulTemp = ulDataR & ulCompute; if (nLocation=i) ulTemp = (i-nLocation); *ulDataOutL += ulTemp; ulCompute=0x80000000; for (i=nArrLen/2; i nLocation; ulTemp = ulDataR & ulCompute; if (nLocation=j) ulTemp = (j-nLocation); *ulDataOutR += ulTemp; ulCompute=0x80000000; ulong Replace32To32(ulong ulData,int Array,int nArrLen) ulong ulDataRet=0; ulong ulTemp=0; ulong ulCompute = 0x80000000; int i; int nLocation; for (i=0; i= nLocation; ulTemp = ulData&ulCompute; if (nLocation=i) ulTemp = (i-nLocation); ulDataRet += ulTemp; ulCompute = 0x80000000; return ulDataRet; 子密钥的生成算法: ulong Move(ulong ulKey,int movesize) ulong ulTemp=0; ulong ulSave=0; ulong ulCompute=0; if (movesize=1) ulCompute = 0x80000000; else if (movesize=2) ulCompute = 0xC0000000; ulTemp = ulCompute&ulKey; ulSave = ulKey (28-movesize); return (ulTemp+ulSave);void GetSubKey16(ulong ulKeyL,ulong ulKeyR,int nTime) ulong ulKeyComputeL=0; ulong ulKeyComputeR=0; ulong ulKeyOutL = 0; ulong ulKeyOutR = 0; if (nTime = 16) return; ulKeyComputeL = Move(ulKeyL,KeyMoveBitnTime); ulKeyComputeR = Move(ulKeyR,KeyMoveBitnTime); /DispHexByUl(ulKeyComputeL); /DispHexByUl(ulKeyComputeR); Replace56To48(ulKeyComputeL,ulKeyComputeR,&ulKeyOutL,&ulKeyOutR,SelectReplace2,sizeof(SelectReplace2)/sizeof(int),28); coutthe nTime subkey := 1; *Column = ulTemp; ulTemp = ulData&ulComputeL; ulSave = ulTemp&0x00000020; ulSave = 4; ulTemp &= 0x00000001; *line = ulSave+ulTemp;void SBoxChgTo32(ulong ulDataL,ulong ulDataR,ulong *ulData) int i; ulong ulCompute=0xFC000000; ulong ulTemp = 0; ulong ulSave = 0; int nLine,nColumn; *ulData = 0; for (i=0; i= (i*6); ulTemp = ulDataL & ulCompute; ulTemp = (3-i)*6+8); GetCL(ulTemp,&nLine,&nColumn); ulSave = SBoxDatai*4+nLinenColumn; ulSave = (i*6); ulTemp = ulDataR & ulCompute; ulTemp = (3-i)*6+8); GetCL(ulTemp,&nLine,&nColumn); ulSave = SBoxDatai*4+4*4+nLinenColumn; ulSave =16) return; else if (DesType = DEENCRYPTION) if (nTimepszKey; /cinpszData; ChgKey56To64(pszKey,pszKeyOut); ChgSzToUl(pszKeyOut,&ulKeyL,&ulKeyR); coutthe 64 key:endl; /DispHexByUl(ulKeyL); /DispHexByUl(ulKeyR); coutendl; ChgSzToUl(pszData,&ulDataL,&ulDataR); coutthe original Data:endl; DispHexByUl(ulDataL); DispHexByUl(ulDataR); coutendl; Replace64To64(ulDataL,ulDataR,&ulDataL0,&ulDataR0,InitArray,sizeof(InitArray)/sizeof(int),32); couttha Init replaced Data:endl; /DispHexByUl(ulDataL0); /DispHexByUl(ulDataR0); coutendl; Replace64To56(ulKeyL,ulKeyR,&ulKeyL0,&ulKeyR0,SelectReplace1,sizeof(SelectReplace1)/sizeof(int),32); /DispHexByUl(ulKeyL0); /DispHexByUl(ulKeyR0); GetSubKey16(ulKeyL0,ulKeyR0,0); DesProcess(ulDataL0,ulDataR0,ENCRYPTION,0); GetCode(&ulResultL,&ulResultR); coutthe encryption result:endl; DispHexByUl(ulResultL); DispHexByUl(ulResultR); coutendl; ulDecrypDataL = ulResultL; ulDecrypDataR = ulResultR; /DispHexByUl(ulDecrypDataL); /DispHexByUl(ulDecrypDataR); /coutendl; Replace64To64(ulDecrypDataL,ulDecrypDataR,&ulDataL0,&ulDataR0,InitArray,sizeof(InitArray)/sizeof(int),32); couttha deencryp Data:endl; DispHexByUl(ulDataL0); DispHexByUl(ulDataR0); coutendl; DesProcess(ulDataL0,ulDataR0,DEENCRYPTION,15); GetCode(&ulResultL,&ulResultR); coutthe deencrption result:endl; DispHexByUl(ulResultL); DispHexByUl(ulResultR); coutendl; return 1; 下面是我写的DES使用的头文件,SBOX盒数据等. #ifndef _DES_H#define _DES_H#include using namespace std;typedef unsigned char uchar;typedef unsigned long ulong;typedef unsigned int uint;typedef struct ulong subkeyl; ulong subkeyr;subkey;int InitArray=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;int ExtendArray= 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;int SelectReplace1= 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, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4;int SelectReplace2= 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;int PReplace=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;int ReverseReplace= 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, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25;int KeyMoveBit=1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1;int SBoxData16= / S114, 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, / S215, 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, / S310, 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, 2, 12, / S47, 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, / S52, 12, 4, 1,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 热点练05 课外文言文(道理启示类寓言)-2024年中考语文专练
- 酸与碱的中和反应课件
- CN120202875A 一种金耳液体菌种自动接种装置及其接种方法
- CN120198169A 一种充电场站占位管理方法、装置、电子终端及存储介质
- 老师安全培训知识点课件
- 老师不会使用课件的原因
- 老人护理知识培训内容
- 老人安全与防护知识培训课件
- 建筑施工高处坠落防治技术
- 2025版碳晶片工程招投标代理合同
- 新媒体视听节目制作
- 外贸公司品质管理制度
- 纪委档案借阅管理制度
- 灭火员初级习题库
- T/CCMA 0177-2023工程机械数字化水平评估规范
- 燃气入户可行性报告
- 智慧民航数据治理典型实践案例2023
- 2025年重点信访人员稳控实施方案重点信访人稳控
- 六年级上册 道德与法治 全册公开课一等奖创新教案
- (完整)蜘蛛人安全技术交底
- 建筑工程三级安全教育内容
评论
0/150
提交评论