⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 enc_parallel.c~

📁 imba OS program for all fuctards who use this site
💻 C~
字号:
#include <stdio.h>#include <fcntl.h>#include <sys/types.h>#include <unistd.h>#include <stdlib.h>#include <string.h>void encrypt(char data,char data1,int key[],int k);/* Function to encrypt a file 2 chars at a time */void decrypt (int l,int r,int key[]);/*function  to decrypt a file reading encrypted vallue of each char*/void enc_fie(char filename[],char key[]);/*function which calls the encrypt function*/void dec_file(char filename[],char key[]);/*function which calls the decrypt function*/void parallel_enc(char data[],int key[]);static char a[16];static char b[16];int main(){  char filename[50];//filename which user enters  int f1,f2;//file pointers to read from one file in which encrypted data is stored and appending             // it in one file for decryption  char buf1[500];//buffer for reading from enc_temp2 500 chars at a time  printf("\n enter file to be encrypted \n");    scanf("%s",filename);  printf("\n encrypting: %s \n",filename);  char key[8] = "12345678";  enc_file(filename,key);//call to encrypt the filename with key    f1 = open("enc_temp1.txt",O_RDWR |O_APPEND);//opening file descriptor f1 in read write append mode  f2 = open("enc_temp2.txt",O_RDONLY);   memset(buf1,0,sizeof(buf1));//setting the buffer to 0    while(read(f2,buf1,500)>0)//reading from file enc_temp2   {	write(f1,buf1,500);//writing into file enc_temp1   }  printf("enter the file you want to decrypt \n");     scanf("%s",filename);  dec_file(filename,key);//call to decrypt the file enc_temp1.txt}/* ---------------MAIN ENDS------------------*//*-----------enc_file STARTS----------------*/void enc_file(char filename[],char key[]){   int fp0;//file pointer    int i=0;//for loop   int key_int[8];//for getting ascii value of the key charadters   int thread_id;//thread id      fp0 = open(filename,O_RDONLY);//opening file for encrption     int file_size = (int) (lseek (fp0,0,SEEK_END));//finding the file size      printf("\n File size is: %d \n",file_size);   int half = file_size / 2;   close(fp0) ;//closing File Pointer   for (i=0;i<8;i++)//finding ascii value of key characters          key_int[i] = (int) key[i];    omp_set_num_threads(2);//Specifying no of threads     #pragma omp parallel   {       printf("\n---------In parallel region ------------\n");              thread_id = omp_get_thread_num();       printf("Thread id is : % d \n",thread_id);      if(thread_id==0)	{	 int count=0;//counting no of chars read         char ch,ch1;//characters for reading from file         FILE *fp1;//file pointer	        if ((fp1 = fopen(filename,"r")) == NULL )	       printf("\n error opening file in Thread 0 \n");	printf("\n In thread 0 half is:%d \n",half);			while(count<half)		{		ch1 = getc(fp1);				ch = getc(fp1);		                printf("\n Encrypting in thread 0 %c and %c \n",ch1,ch);		                encrypt(ch1,ch,key_int,thread_id);//calling encrypt to encrypt ch1 and ch			count = count+2;//2 chars encrypted		}//END OF WHILE 	fclose(fp1);//closing fp1		}/* -----------End of Thread 0------------*/ else if (thread_id==1)	{	printf("\n In thread 2 \n");	        char ch2,ch3;//characters for reading from the file	FILE *fp2;	int position = half;//position to which file pointer needs to be moved	       if (((fp2 = fopen(filename,"r")) == NULL));	          printf("cant open file \n");	       /*if (file_size %2 ==0)  	 position = half;       else 	  position = half;*/		if(fseek(fp2,position,SEEK_SET)!=0)//moving file pointer to middle of the file	      printf("\n lseek failed in Thread 1\n");       ch2 = getc(fp2);//reading a character      // printf("char read was : %c \n",ch2);              while(ch2 != EOF)//continue till end of file		{		                ch3 = ch2;				ch2 = getc(fp2);// read 2 character		printf("\n Encrypting in thread 1 %c and %c \n",ch3,ch2);		encrypt(ch3,ch2,key_int,thread_id);//encrypt ch3 and ch2		                ch2 = getc(fp2);		}//End of while	fclose(fp2);//close fp2	     }//END oF THREAD 1}//--------END of PRAGMA ---------------------- }//-----------------------END of enc_file-------------------------------------void dec_file(char filename[], char key[]){ FILE *fp1; int p,q,i=0; int key_int[8]; fp1 = fopen(filename,"r");  for (i=0;i<8;i++)   key_int[i] = (int) key[i];// omp_set_num_threads(8) ;   while(fscanf(fp1,"%d %d",&p,&q)>0)//read 2 chars from the file {  printf("read from file %d and %d \n",p,q); decrypt(p,q,key_int);//calling decrypt  } fclose(fp1);//closing fp1}void encrypt(char a,char b, int key[],int k1){ int data = (int)a;// getting ascii of 2 chars int data2 = (int)b; int l[16];//array for finding encrypted value of a and b int r[16]; l[0] = data; r[0] = data2; int i =0; int j=0;  int k =0;  for (i=1;i<16;i++)	{	if (i>8  && k==0)          {		j=0;k++;}	l[i] = r[i-1] ^ key[j];            	r[i] = l[i-1] ^ key[j];  	        printf("key index :%d \n",j);	        printf("l %d is  %d \n",i,l[i]);	printf("r %d is %d \n ",i,r[i]);	j++;	}  	printf("----------------------------- \n")	; 	 int buf[2] ={l[15],r[15]};	 printf("buffer :: %d and %d \n",buf[0],buf[1]); 	FILE *fp1, *fp2;	fp1 = fopen("enc_temp1.txt","a");	fp2 = fopen("enc_temp2.txt","a");if (k1==0){	printf("\n Now writing enc_temp1.txt \n");	fprintf(fp1,"%d %d ",l[15],r[15]);}else if(k1==1){	printf("\n Now writng enc_temp2.txt \n");	fprintf(fp2,"%d %d ",l[15],r[15]);}fclose(fp1);fclose(fp2); }//----------------------End of encrypt()---------------------------------------------void decrypt (int l, int r,int key[]){ int fp1; int p,q; fp1 = open("enc.txt",O_WRONLY|O_APPEND); int i = 16; int k=6; int l1[16]; int r1[16]; l1[15] = l; r1[15] = r; for (i=14;i>=0;i--)	{	if (k<0)		k=7;	l1[i] = r1[i+1] ^  key[k];	r1[i] = l1[i+1]  ^ key[k];	k--;        printf("l %d is  %d \n",i,l1[i]);        printf("r %d is %d \n ",i,r1[i]);	}// END of FOR	char a = (char)(l1[1] ^ key[0]);	char b = (char)(r1[1]^key[0]);	printf("char value is %c \n",a);	printf("char 2 value  is %c \n",b);	printf("\n******************** \n");		char buf[2];	buf[1]=a;	buf[0]=b;	if (write (fp1,buf,2)==-1)            printf("write error"); 	close(fp1);}//----------------END of decrypt--------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -