用c语言写des加密算法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用c语言写des加密算法相关的知识,希望对你有一定的参考价值。

要求:① 定义并实现位ADT,包括其上的基本操作:与、或、非、异或等。
③ 实现DES 算法。
④ 应用DES 算法对一文本文件(全是ASCII 码)加密。
④ 用ASCII 方法对加密结果进行显示。
可以发我邮箱1013667718@qq.com 谢谢

参考技术A #include <stdio.h> #include <string.h> #include <windows.h> #include <conio.h> #include "Schedle.h" class CShift public: DWORDLONG mask[16]; int step[16]; CShift() for(int i=0;i<16;i++) step[i]=2; mask[i]=0xc000000; step[0]=step[1]=step[8]=step[15]=1; mask[0]=mask[1]=mask[8]=mask[15]=0x8000000; ; class CDES public: CDES() m_dwlKey=0; m_dwlData=0; ConvertTableToMask(dwlKey_PC_1,64); //PrintTable(dwlKey_PC_1,7,8); ConvertTableToMask(dwlKey_PC_2,56); ConvertTableToMask(dwlData_IP,64); ConvertTableToMask(dwlData_Expansion,32); ConvertTableToMask(dwlData_FP,64); ConvertTableToMask(dwlData_P,32); Generate_S(); void PrintBit(DWORDLONG); void EncryptKey(char *); unsigned char* EncryptData(unsigned char *); unsigned char* DescryptData(unsigned char*); private: void ConvertTableToMask(DWORDLONG *,int); void Generate_S(void); void PrintTable(DWORDLONG*,int,int); DWORDLONG ProcessByte(unsigned char*,BOOL); DWORDLONG PermuteTable(DWORDLONG,DWORDLONG*,int); void Generate_K(void); void EncryptKernel(void); DWORDLONG Generate_B(DWORDLONG,DWORDLONG*); /*For verify schedule permutation only*/ DWORDLONG UnPermuteTable(DWORDLONG,DWORDLONG*,int); /**************************************/ DWORDLONG dwlData_S[9][4][16]; CShift m_shift; DWORDLONG m_dwlKey; DWORDLONG m_dwlData; DWORDLONG m_dwl_K[17]; ; void CDES::EncryptKey(char *key) printf("\nOriginal Key: %s",key); m_dwlKey=ProcessByte((unsigned char*)key,TRUE); // PrintBit(m_dwlKey); m_dwlKey=PermuteTable(m_dwlKey,dwlKey_PC_1,56); // PrintBit(m_dwlKey); Generate_K(); // printf("\n******************************************\n"); void CDES::Generate_K(void) DWORDLONG C[17],D[17],tmp; C[0]=m_dwlKey>>28; D[0]=m_dwlKey&0xfffffff; for(int i=1;i<=16;i++) tmp=(C[i-1]&m_shift.mask[i-1])>>(28-m_shift.step[i-1]); C[i]=((C[i-1]<<m_shift.step[i-1])|tmp)&0x0fffffff; tmp=(D[i-1]&m_shift.mask[i-1])>>(28-m_shift.step[i-1]); D[i]=((D[i-1]<<m_shift.step[i-1])|tmp)&0x0fffffff; m_dwl_K[i]=(C[i]<<28)|D[i]; m_dwl_K[i]=PermuteTable(m_dwl_K[i],dwlKey_PC_2,48); DWORDLONG CDES::ProcessByte(unsigned char *key,BOOL shift) unsigned char tmp; DWORDLONG byte=0; int i=0; while(i<8) while(*key) if(byte!=0) byte<<=8; tmp=*key; if(shift) tmp<<=1; byte|=tmp; i++; key++; if(i<8) byte<<=8; i++; return byte; DWORDLONG CDES::PermuteTable(DWORDLONG dwlPara,DWOR 基于des算法的rfid安全系统
DLONG* dwlTable,int nDestLen) int i=0; DWORDLONG tmp=0,moveBit; while(i<nDestLen) moveBit=1; if(dwlTable[i]&dwlPara) moveBit<<=nDestLen-i-1; tmp|=moveBit; i++; return tmp; DWORDLONG CDES::UnPermuteTable(DWORDLONG dwlPara,DWORDLONG* dwlTable,int nDestLen) DWORDLONG tmp=0; int i=nDestLen-1; while(dwlPara!=0) if(dwlPara&0x01) tmp|=dwlTable[i]; dwlPara>>=1; i--; return tmp; void CDES::PrintTable(DWORDLONG *dwlPara,int col,int row) int i,j; for(i=0;i<row;i++) printf("\n"); getch(); for(j=0;j<col;j++) PrintBit(dwlPara[i*col+j]); void CDES::PrintBit(DWORDLONG bitstream) char out[76]; int i=0,j=0,space=0; while(bitstream!=0) if(bitstream&0x01) out[i++]='1'; else out[i++]='0'; j++; if(j%8==0) out[i++]=' '; space++; bitstream=bitstream>>1; out[i]='\0'; strcpy(out,strrev(out)); printf("%s **:%d\n",out,i-space); void CDES::ConvertTableToMask(DWORDLONG *mask,int max) int i=0; DWORDLONG nBit=1; while(mask[i]!=0) nBit=1; nBit<<=max-mask[i]; mask[i++]=nBit; void CDES::Generate_S(void) int i; int j,m,n; m=n=0; j=1; for(i=0;i<512;i++) dwlData_S[j][m][n]=OS[i]; n=(n+1)%16; if(!n) m=(m+1)%4; if(!m) j++; unsigned char * CDES::EncryptData(unsigned char *block) unsigned char *EncrytedData=new unsigned char(15); printf("\nOriginal Data: %s\n",block); m_dwlData=ProcessByte(block,0); // PrintBit(m_dwlData); m_dwlData=PermuteTable(m_dwlData,dwlData_IP,64); EncryptKernel(); // PrintBit(m_dwlData); DWORDLONG bit6=m_dwlData; for(int i=0;i<11;i++) EncrytedData[7-i]=(unsigned char)(bit6&0x3f)+46; bit6>>=6; EncrytedData[11]='\0'; printf("\nAfter Encrypted: %s",EncrytedData); for(i=0;i<8;i++) EncrytedData[7-i]=(unsigned char)(m_dwlData&0xff); m_dwlData>>=8; EncrytedData[8]='\0'; return EncrytedData; void CDES::EncryptKernel(void) int i=1; DWORDLONG L[17],R[17],B[9],EK,PSB; L[0]=m_dwlData>>32; R[0]=m_dwlData&0xffffffff; for(i=1;i<=16;i++) L[i]=R[i-1]; R[i-1]=PermuteTable(R[i-1],dwlData_Expansion,48); //Expansion R EK=R[i-1]^m_dwl_K[i]; //E Permutation PSB=Generate_B(EK,B); //P Permutation R[i]=L[i-1]^PSB; R[16]<<=32; m_dwlData=R[16]|L[16]; m_dwlData=PermuteTable(m_dwlData,dwlData_FP,64); unsigned char* CDES::DescryptData(unsigned char *desData) int i=1; unsigned char *DescryptedData=new unsigned char(15); DWORDLONG L[17],R[17],B[9],EK,PSB; DWORDLONG dataPara; dataPara=ProcessByte(desData,0); dataPara=PermuteTable(dataPara,dwlData_IP,64); R[16]=dataPara>>32; L[16]=dataPara&0xffffffff; for(i=16;i>=1;i--) R[i-1]=L[i]; L[i]=PermuteTable(L[i],dwlData_Expansion,48); //Expansion L EK=L[i]^m_dwl_K[i]; //E Permutation PSB=Generate_B(EK,B); //P Permutation L[i-1]=R[i]^PSB; L[0]<<=32; dataPara=L[0]|R[0]; dataPara=PermuteTable(dataPara,dwlData_FP,64); // PrintBit(dataPara); for(i=0;i<8;i++) DescryptedData[7-i]=(unsigned char)(dataPara&0xff); dataPara>>=8; DescryptedData[8]='\0'; printf("\nAfter Decrypted: %s\n",DescryptedData); return DescryptedData; DWORDLONG CDES::Generate_B(DWORDLONG EKPara,DWORDLONG *block) int i,m,n; DWORDLONG tmp=0; for(i=8;i>0;i--) block[i]=EKPara&0x3f; m=(int)(block[i]&0x20)>>4; m|=block[i]&0x01; n=(int)(block[i]<<1)>>2; block[i]=dwlData_S[i][m][n]; EKPara>>=6; for(i=1;i<=8;i++) tmp|=block[i]; tmp<<=4; tmp>>=4; tmp=PermuteTable(tmp,dwlData_P,32); return tmp; void main(void) CDES des; des.EncryptKey("12345678"); unsigned char *result=des.EncryptData((unsigned char*)"DemoData"); des.DescryptData(result); [1]

参考资料:百度百科

参考技术B 首先c语言要熟悉,然后去图书馆借一本加密解密的书,要里面有c语言des实现代码的(这种书是有的,我看到过)。论文先对加密解密的历史及发展现状进行介绍,然后着重对des加密的发展历史及原理进行阐述(以上内容要多借几本相关书综合一下用自己的语言表达出来)。然后对des的算法写个程序(可以利用书里面的程序),然后运行结果截几张图下来。最后总结一下,论文就可以了。本回答被提问者和网友采纳 参考技术C 呵呵,这里成了抄作业的地方了。

des加密 c++ java

谁可以把下面的java des加密算法写成c++的,要求java加密的可以用c++解密
c++加密的可以用java解密哦

private String encryptData(String retData)
if (!encryptFlag)
return retData;

String returnData = "";
try
String oriDataValue = retData;
BASE64Decoder base64decoder = new BASE64Decoder();
BASE64Encoder base64encoder = new BASE64Encoder();
byte[] keyBytes = base64decoder.decodeBuffer("liana");
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom(keyBytes));
Key key = generator.generateKey();
Cipher cipher = Cipher.getInstance("DES" + "/" + "ECB" + "/"
+ "PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptBytes = cipher
.doFinal(oriDataValue.getBytes("UTF-8"));
String encryptStr = base64encoder.encode(encryptBytes);
returnData = encryptStr;
catch (Exception e)
// TODO Auto-generated catch block
e.printStackTrace();

return returnData;


private String decryptData(String reqData)
if (!encryptFlag)
return reqData;

String returnData = "";
try
returnData = "";
String encryptDataValue = reqData;
BASE64Decoder base64decoder = new BASE64Decoder();
byte[] keyBytes = base64decoder.decodeBuffer("liana");
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom(keyBytes));
Key key = generator.generateKey();
Cipher cipher = Cipher.getInstance("DES" + "/" + "ECB" + "/"
+ "PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] oriBytes = cipher.doFinal(base64decoder
.decodeBuffer(encryptDataValue));
returnData = new String(oriBytes, "UTF-8");
catch (Exception e)
// TODO Auto-generated catch block
e.printStackTrace();

return returnData;

Java写DES一般用现成的类库,而C++来完成加密算法工作都是用openssl库来实现的。
openssl库调用不算很难,编译网上也有操作流程。
当不同语言实现加密解密操作的时候,注意一下反馈模式和填充模式就行。
常用的反馈模式是ECB和CBC,你这套代码是用的ECB就是无反馈模式,这个就可以不用考虑了。而填充模式标准的填充方法是pkcs#5标准,如果按照这种标准填充的,那什么语言只要数据对应,都没问题,因为算法的实现是相同的。
参考技术A 只要你用的是标准的DES算法,无论什么语言都是一样

我这有个C++ des加密的小程序,如果你想要,可以留个邮箱
参考技术B 没尝试过。不过,生成exe文件之后,那就无所谓了

以上是关于用c语言写des加密算法的主要内容,如果未能解决你的问题,请参考以下文章

用C语言来实现DES加密算法(很急)两天内

des算法加密解密的实现

如何实现C语言的DES加密算法实现,请关注

des加密算法(c/c++)

des加密 c++ java

JAVA和.NET使用DES对称加密的区别