Esempio n. 1
0
File: des.cpp Progetto: yzmin/Des
//解密文件 
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;  
}  
Esempio n. 2
0
File: des.cpp Progetto: yzmin/Des
//str —> file 加密
int DES_Encrypt_str_to_file(char *source, char *keyStr,char *cipherFile)
{
	int cipher;
	int count = strlen(source);
	printf("count = %d\n",count);
	int times = 0;
	ElemType plainBlock[8],cipherBlock[8],keyBlock[8];
	ElemType bKey[64];  
	ElemType subKeys[16][48]; 

	if((cipher = open(cipherFile,O_CREAT | O_RDWR,0660)) < 0)
	{
		return CIPHER_FILE_OPEN_ERROR; 
	}

	//设置密钥
	memcpy(keyBlock,keyStr,8);  
	//将密钥转换为二进制流
	Char8ToBit64(keyBlock,bKey);  
	//生成子密钥
	DES_MakeSubKeys(bKey,subKeys);

	//加密8个字节
	while(1)
	{
		memcpy(plainBlock,source+times,8); 
		printf("%s\n",plainBlock);
		count -= 8;
		printf("count = %d\n",count);

		DES_EncryptBlock(plainBlock,subKeys,cipherBlock);
		write(cipher,cipherBlock,sizeof(cipherBlock));
		printf("cipherBlock = %s\n",cipherBlock);
		times += 8;
		if(count < 8)
			break;
	}

	if(count){ 
		printf("if times = %d\n",times);
		printf("if count = %d\n",count);
		//填充 
		memset(plainBlock,'\0',8);
		memcpy(plainBlock,source + times ,count);
		printf("if = %s\n",plainBlock);

		//最后一个字符保存包括最后一个字符在内的所填充的字符数量  
		plainBlock[7] = 8 - count;
		DES_EncryptBlock(plainBlock,subKeys,cipherBlock);
		write(cipher,cipherBlock,sizeof(cipherBlock));
		printf("if cipherBlock = %s\n",cipherBlock);
	}  

	close(cipher);
	return OK;

}
Esempio n. 3
0
File: des.cpp Progetto: yzmin/Des
//解析文件成字符串
//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;
}
Esempio n. 4
0
//加密文件
unsigned char DES_Encrypt(unsigned char *keyStr,unsigned char *plainFile,unsigned char *cipherFile)
{
    unsigned char keyBlock[8],bKey[64],subKeys[16][48];

    memcpy(keyBlock,keyStr,8);							 //设置密钥
    Char8ToBit64(keyBlock,bKey);						 //将密钥转换为二进制流
    DES_MakeSubKeys(bKey,subKeys);						 //生成子密钥

	DES_EncryptBlock(plainFile,subKeys,cipherFile);  	
	return 1;
}
Esempio n. 5
0
/**************************对文件进行加密*****************************/
int DES_Encrypt(char *plainFile, char *keyStr,char *cipherFile)
{
 FILE *plain,*cipher;
 int count,num=0;
 ElemType plainBlock[8],cipherBlock[8],keyBlock[8];
 ElemType bKey[64];
 ElemType subKeys[16][48];
 if((plain = fopen(plainFile,"rb")) == NULL){
  return PLAIN_FILE_OPEN_ERROR;
 }
 if((cipher = fopen(cipherFile,"wb")) == NULL){
  return CIPHER_FILE_OPEN_ERROR;
 }
 /*设置密钥*/
 memcpy(keyBlock,keyStr,8);
 /*将密钥转换为二进制流*/
 Char8ToBit64(keyBlock,bKey);
 /*生成子密钥*/
 DES_MakeSubKeys(bKey,subKeys);
 
 while(!feof(plain))
 {
  /*每次读8个字节,并返回成功读取的字节数*/
  if((count = fread(plainBlock,sizeof(char),8,plain)) == 8)
  {
	num+=count;
	DES_EncryptBlock(plainBlock,subKeys,cipherBlock);
	fwrite(cipherBlock,sizeof(char),8,cipher);  
  }
 }

 if(count)
 {
   num+=count;
  /*填充*/
  memset(plainBlock + count,'\0',7 - count);
  /*最后一个字符保存包括最后一个字符在内的所填充的字符数量*/
  plainBlock[7] = 8 - count;
  DES_EncryptBlock(plainBlock,subKeys,cipherBlock);
  fwrite(cipherBlock,sizeof(char),8,cipher);
 }
 fclose(plain);
 fclose(cipher);
 printf("Plain File Length is %d\n",num);
 return OK;
}
Esempio n. 6
0
 //加密文件   
 int DES_Encrypt(const char *pSrcData,int &nLen,const char *keyStr,char *pDesData){

     int count;   
     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);   
        
	 int  nRead = 0;
	 while(nRead < nLen)
	 {
		 if(nLen - nRead>=8)
		 {
			 count = 8;
			 memcpy(plainBlock,pSrcData+nRead,count);
			//每次读8个字节,并返回成功读取的字节数   
			 DES_EncryptBlock(plainBlock,subKeys,cipherBlock);   
			 memcpy(pDesData+nRead,cipherBlock,8); 

		 }else
		 {
			 count = nLen - nRead;
			 memcpy(plainBlock,pSrcData+nRead,count);
		          //填充   
			 memset(plainBlock + count,'\0',7 - count);   
             //最后一个字符保存包括最后一个字符在内的所填充的字符数量   
             plainBlock[7] = 8 - count;   
             DES_EncryptBlock(plainBlock,subKeys,cipherBlock);   
			 memcpy(pDesData+nRead,cipherBlock,8); 
		 }
		 nRead += count;
     }
	 nLen = (nLen+7)&0xfffffff8;
     return OK;   
 }   
Esempio n. 7
0
File: des.cpp Progetto: yzmin/Des
//加密文件  
int DES_Encrypt(char *plainFile, char *keyStr,char *cipherFile)
{  
	FILE *plain,*cipher;  
	int count;  
	ElemType plainBlock[8],cipherBlock[8],keyBlock[8];  
	ElemType bKey[64];  
	ElemType subKeys[16][48];  
	if((plain = fopen(plainFile,"rb")) == NULL){  
		return PLAIN_FILE_OPEN_ERROR;  
	}     
	if((cipher = fopen(cipherFile,"wb")) == NULL){  
		return CIPHER_FILE_OPEN_ERROR;  
	}  
	//设置密钥  
	memcpy(keyBlock,keyStr,8);  
	//将密钥转换为二进制流  
	Char8ToBit64(keyBlock,bKey);  
	//生成子密钥  
	DES_MakeSubKeys(bKey,subKeys);  

	while(!feof(plain)){  
		//每次读8个字节,并返回成功读取的字节数  
		if((count = fread(plainBlock,sizeof(char),8,plain)) == 8){  
			DES_EncryptBlock(plainBlock,subKeys,cipherBlock);  
			fwrite(cipherBlock,sizeof(char),8,cipher);    
		}  
	}  
	if(count){  
		//填充  
		memset(plainBlock + count,'\0',7 - count);  
		//最后一个字符保存包括最后一个字符在内的所填充的字符数量  
		plainBlock[7] = 8 - count;  
		DES_EncryptBlock(plainBlock,subKeys,cipherBlock);  
		fwrite(cipherBlock,sizeof(char),8,cipher);  
	}  
	fclose(plain);  
	fclose(cipher);  
	return OK;  
}  
Esempio n. 8
0
/*
 *功能说明:DES算法解密函数
 *入口参数:key:密钥,长度为8字节
 *s_text:待解密的密文,长度为8字节;
 *d_text:解密得到的明文,长度为8字节。
 */
int _QDES(char *key, char *s_text, char *d_text)
{
	char plainBlock[8];
	char cipherBlock[8];
	char keyBlock[8];

	char bKey[64];
	char subKeys[16][48];
	memcpy(keyBlock, key, 8); //设置密钥
	Char8ToBit64(keyBlock, bKey); //将密钥转换为二进制流
	DES_MakeSubKeys(bKey, subKeys); //生成子密钥
	memcpy(cipherBlock, s_text, 8);

	char cipherBits[64];
	char copyRight[48];
	int cnt;
	Char8ToBit64(cipherBlock, cipherBits);
	DES_IP_Transform(cipherBits); //初始置换(IP置换)
	for (cnt = 15; cnt >= 0; cnt--) //16轮迭代
	{
		memset(copyRight, 0, 48);
		memcpy(copyRight, cipherBits + 32, 32);
		DES_E_Transform(copyRight); //将右半部分进行扩展置换,从32位扩展到48位
		DES_XOR(copyRight, subKeys[cnt], 48); //将右半部分与子密钥进行异或操作
		DES_SBOX(copyRight); //异或结果进入S盒,输出32位结果
		DES_P_Transform(copyRight); //P置换
		DES_XOR(cipherBits, copyRight, 32); //将明文左半部分与右半部分进行异或
		if (cnt != 0)
		{
			DES_Swap(cipherBits, cipherBits + 32); //最终完成左右部的交换
		}
	}
	DES_IP_1_Transform(cipherBits); //逆初始置换(IP^1置换)
	Bit64ToChar8(cipherBits, plainBlock);
	memcpy(d_text, plainBlock, 8);
	return 0;
}
Esempio n. 9
0
 //解密文件   
 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;   
 }   
Esempio n. 10
0
/*解密文件 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;
}  
Esempio n. 11
0
File: des.cpp Progetto: yzmin/Des
//加密字符串
int DES_Encrypt_str(char *source,char *keyStr,char *result)
{
	int count = 178;
	printf("count = %d\n",count);
	int 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);

	//加密8个字节
	while(1)
	{
		memcpy(plainBlock,source+times,8); 
		printf("%s\n",plainBlock);
		count -= 8;
		printf("count = %d\n",count);

		DES_EncryptBlock(plainBlock,subKeys,cipherBlock);
		for(int i = 0; i < 8;++i)
			printf("%x ",cipherBlock[i]);
		printf("\n");
		memcpy(result+times,cipherBlock,8);
		printf("result = %s\n",result);
		times += 8;
		if(count < 8)
			break;
	}

	if(count){ 
		printf("if times = %d\n",times);
		printf("if count = %d\n",count);
		//填充 
		memset(plainBlock,'\0',8);
		memcpy(plainBlock,source + times ,count);
		printf("plainBlock = ");
		for(int i = 0; i < 8;++i)
			printf("%x ",plainBlock[i]);
		printf("\n");

		//最后一个字符保存包括最后一个字符在内的所填充的字符数量  
		plainBlock[7] = 8 - count;
		DES_EncryptBlock(plainBlock,subKeys,cipherBlock);
		for(int i = 0; i < 8;++i)
			printf("%x ",cipherBlock[i]);
		printf("\n");
		memcpy(result+times,cipherBlock,8); 
		printf("if result = %s\n",result);
	}  

	for(int i = 0;i < 184;++i)
		printf("%x ",result[i]);
	printf("\n");

	return 0;
}
Esempio n. 12
0
File: des.cpp Progetto: yzmin/Des
//字符串解密
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;
}