Esempio n. 1
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. 2
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. 3
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. 4
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. 5
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. 6
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;
}