//解密文件 int DES_Decrypt(char *cipherFile, char *keyStr,char *plainFile) { FILE *plain, *cipher; int count,times = 0; long fileLen; ElemType plainBlock[8],cipherBlock[8],keyBlock[8]; ElemType bKey[64]; ElemType subKeys[16][48]; if((cipher = fopen(cipherFile,"rb")) == NULL){ return CIPHER_FILE_OPEN_ERROR; } if((plain = fopen(plainFile,"wb")) == NULL){ return PLAIN_FILE_OPEN_ERROR; } //设置密钥 memcpy(keyBlock,keyStr,8); //将密钥转换为二进制流 Char8ToBit64(keyBlock,bKey); //生成子密钥 DES_MakeSubKeys(bKey,subKeys); //取文件长度 fseek(cipher,0,SEEK_END); //将文件指针置尾 fileLen = ftell(cipher); //取文件指针当前位置 rewind(cipher); //将文件指针重指向文件头 while(1){ //密文的字节数一定是8的整数倍 fread(cipherBlock,sizeof(char),8,cipher); DES_DecryptBlock(cipherBlock,subKeys,plainBlock); times += 8; if(times < fileLen){ fwrite(plainBlock,sizeof(char),8,plain); } else{ break; } } //判断末尾是否被填充 if(plainBlock[7] < 8){ for(count = 8 - plainBlock[7]; count < 7; count++){ if(plainBlock[count] != '\0'){ break; } } } if(count == 7){//有填充 fwrite(plainBlock,sizeof(char),8 - plainBlock[7],plain); } else{//无填充 fwrite(plainBlock,sizeof(char),8,plain); } fclose(plain); fclose(cipher); return OK; }
//解析文件成字符串 //file -> str int DES_Decrypt_file_to_str(char *cipherFile, char *keyStr,char *result) { int cipher; int count,times = 0; long fileLen; ElemType plainBlock[8],cipherBlock[8],keyBlock[8]; ElemType bKey[64]; ElemType subKeys[16][48]; if((cipher = open(cipherFile,O_RDONLY)) < 0){ return CIPHER_FILE_OPEN_ERROR; } printf("cipher = %d\n",cipher); //设置密钥 memcpy(keyBlock,keyStr,8); //将密钥转换为二进制流 Char8ToBit64(keyBlock,bKey); //生成子密钥 DES_MakeSubKeys(bKey,subKeys); //取文件长度 fileLen = lseek(cipher,0,SEEK_END); //将文件指针置尾 取文件指针当前位置 lseek(cipher,0,SEEK_SET); //将文件指针重指向文件头 while(1){ //密文的字节数一定是8的整数倍 read(cipher,cipherBlock,sizeof(cipherBlock)); DES_DecryptBlock(cipherBlock,subKeys,plainBlock); if(times < fileLen){ memcpy(result + times,plainBlock,8); times += 8; //printf("result = %s\n",result); } else{ break; } } //判断末尾是否被填充 if(plainBlock[7] < 8){ for(count = 8 - plainBlock[7]; count < 7; count++){ if(plainBlock[count] != '\0'){ break; } } } if(count == 7){//有填充 memcpy(result + times,plainBlock,8 - plainBlock[7]); } else{//无填充 memcpy(result + times,plainBlock,8); } close(cipher); return OK; }
//解密文件 unsigned char DES_Decrypt(unsigned char *keyStr,unsigned char *cipherFile,unsigned char *plainFile) { // unsigned char plainBlock[8],cipherBlock[8], unsigned char bKey[64],keyBlock[8],subKeys[16][48]; memcpy(keyBlock,keyStr,8); //设置密钥 Char8ToBit64(keyBlock,bKey); //将密钥转换为二进制流 DES_MakeSubKeys(bKey,subKeys); //生成子密钥 DES_DecryptBlock(cipherFile,subKeys,plainFile); return 1; }
//解密文件 int DES_Decrypt(const char *pSrcData, int &nLen, const char *keyStr,char *pDesData){ int count,times = 0; ElemType plainBlock[8],cipherBlock[8],keyBlock[8]; ElemType bKey[64]; ElemType subKeys[16][48]; //设置密钥 memcpy(keyBlock,keyStr,8); //将密钥转换为二进制流 Char8ToBit64(keyBlock,bKey); //生成子密钥 DES_MakeSubKeys(bKey,subKeys); while(times<nLen){ //密文的字节数一定是8的整数倍 memcpy(cipherBlock,pSrcData+times,8); DES_DecryptBlock(cipherBlock,subKeys,plainBlock); memcpy(pDesData+times,plainBlock,8); times += 8; } //判断末尾是否被填充 if(plainBlock[7] < 8){ for(count = 8 - plainBlock[7]; count < 7; count++){ if(plainBlock[count] != '\0'){ break; } } } if(count == 7){//有填充 times = times - plainBlock[7]; } nLen = times; return OK; }
/*解密文件 author by yx */ int DES_Decrypt(unsigned char *cipherbuf,unsigned char *keyStr,unsigned char *plainbuf, unsigned long Num_len) { long fileLen; ElemType plainBlock[8],cipherBlock[8],keyBlock[8]; ElemType bKey[64]; ElemType subKeys[16][48]; int count,times = 0; unsigned char *TempBufIn=NULL; unsigned char *TempBufOut=NULL; if(cipherbuf == NULL||keyStr==NULL||plainbuf==NULL||Num_len==0) return -1; TempBufIn=cipherbuf; TempBufOut=plainbuf; /*设置密钥*/ memcpy(keyBlock,keyStr,8); /*将密钥转换为二进制流*/ Char8ToBit64(keyBlock,bKey); /*生成子密钥*/ DES_MakeSubKeys(bKey,subKeys); while(1) { /*密文的字节数一定是8的整数倍*/ memset(cipherBlock,0,8); memset(plainBlock,0,8); //读取缓冲区数据 memcpy(cipherBlock,TempBufIn,8); TempBufIn+=8; DES_DecryptBlock(cipherBlock,subKeys,plainBlock); times += 8; if(times < Num_len) { //将解密后的数据写回缓冲区; memset(TempBufOut,0,8); memcpy(TempBufOut,plainBlock,8); TempBufOut+=8; } else { break; } } /*判断末尾是否被填充*/ if(plainBlock[7] < 8) { for(count = 8 - plainBlock[7]; count < 7; count++) { if(plainBlock[count] != '\0') { break; } } } if(count == 7) {/*有填充*/ memset(TempBufOut,0,8); memcpy(TempBufOut,plainBlock,8 - plainBlock[7]); } else {/*无填充*/ memset(TempBufOut,0,8); memcpy(TempBufOut,plainBlock,8); } return OK; }
//字符串解密 int DES_Decrypt_str(char *source,char *keyStr,char *reslut) { int count = 0,times = 0; long fileLen = 178; printf("fileLen = %ld\n",fileLen); ElemType plainBlock[8],cipherBlock[8],keyBlock[8]; ElemType bKey[64]; ElemType subKeys[16][48]; //设置密钥 memcpy(keyBlock,keyStr,8); //将密钥转换为二进制流 Char8ToBit64(keyBlock,bKey); //生成子密钥 DES_MakeSubKeys(bKey,subKeys); //加载解密数据 while(times < fileLen){ memset(cipherBlock,'\0',8); memcpy(plainBlock,source+times,8); for(int i = 0;i<8;++i) printf("times = %d cipherBlock[%d] = %x\n",times,i,plainBlock[i]); DES_DecryptBlock(plainBlock,subKeys,cipherBlock); printf("times = %d cipherBlock = ",times); for(int i = 0; i < 8;++i) printf("%x ",cipherBlock[i]); printf("\n"); memcpy(reslut+times,cipherBlock,8); printf("reslut = %s\n",reslut); times += 8; } printf("times = %d cipherBlock = ",times); for(int i = 0; i < 8;++i) printf("%x ",cipherBlock[i]); printf("\n"); //判断末尾是否被填充 if(cipherBlock[7] < 8){ printf("for cipherBlock = %s\n",cipherBlock); printf("for count = %d\n",8 - cipherBlock[7]); for(count = 8 - cipherBlock[7]; count < 7; count++){ // 4 5 6 7 if(cipherBlock[count] != '\0'){ break; } } } if(count == 7){//有填充 memcpy(reslut+times,cipherBlock,8 - cipherBlock[7]); printf("if cipherBlock = %s\n",reslut+times); } else{//无填充 memcpy(reslut+times,cipherBlock,8); printf("else cipherBlock = %s\n",reslut+times); } printf("cipherBlock = %s\n",reslut); for(int i = 0; i < 184;++i) printf("%x ",reslut[i]); printf("\n"); return 0; }