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

📄 enc_parallel1.c

📁 imba OS program for all fuctards who use this site
💻 C
字号:
#include <stdio.h>#include <fcntl.h>#include <omp.h>int* encrypt(char data,char data1,int key[]); // this function takes 2 charecters and encrypts them using key to return 2 integerschar* decrypt (int l,int r,int key[]); // this function does the reverse of encrypt.void enc_fie(char filename[],char key[]); // this is the fucntion called when a user wants to encrypt a filevoid dec_file(char filename[],char key[]); // this is the function called when user wants to decrypt a filevoid parallel_enc(char data[],int key[]); // this function encrypts given char array  in parallelvoid parallel_dec(int data[],int key[]) ; // hti fucntion decrypts the given integer array in parallelstatic int a[16];static char a1[16];int main(){char filename[50];double start_time,end_time;char key[12];int choice = 0;int exit = 0;while(exit == 0)	{	printf("what do you want to do? \n 1.Encrypt a file \n 2. Decrypt a file \n 3.Exit \n" );	scanf("%d",&choice);	switch(choice)		{		case(1):			 start_time = 0;			 end_time = 0;			printf("enter file to be encrypted \n");			scanf("%s",filename);			printf("enter the key (8 charecters)to be used for encryption \n");			scanf("%s",key);		//	printf("%s \n",key);			start_time  = omp_get_wtime();			printf("encryption of %s started at : %f \n ",filename,start_time);			enc_file(filename,key);						end_time = omp_get_wtime();			printf("encryption of %s ended at %f \n", filename,end_time );			printf("total time taken for encryption was %f \n",(end_time - start_time));			break;		case(2):			start_time = 0;			end_time = 0;                        printf("enter file to be decrypted \n");                        scanf("%s",filename);                        printf("enter the key (8 charecters)to be used for decryption \n");                        scanf("%s",key);			start_time = omp_get_wtime();			printf("decryptio started at: %f \n", start_time);                        printf("decrypting: %s",filename);                        dec_file(filename,key);			end_time = omp_get_wtime();			printf("decryption ended at %f \n", end_time);			printf("time taken for decryptionwa s%f \n" , end_time - start_time);			break;		case(3):			exit = 1;			break;		}	}}/* this code takes a file, read chares in chunk of 16 chars at a time and then calls enc_parallel*/void enc_file(char filename[],char key[]){int fp0;fp0 = open(filename,O_RDONLY); 	// open the file for reading..char data[16];			// a buf of size 16 to store the erad charsint i =0;int key_int[8];			// here we convert the chars in "key" into their ASCII vaule for XORing for (i=0;i<8;i++)key_int[i] = (int)key[i];int l=0;char ch,ch1;int tp=0;// reading data in chunks of 16 bytes and then calling enc_parallelwhile( read(fp0,data,16) > 0  )          {	if (tp==0){	lseek(fp0,16,SEEK_CUR);	tp=1;	}//	printf("calling parallel_enc with data = %s\n",data);	parallel_enc(data,key_int);	       }close(fp0);   // closing the file descriptor }/* this method takes char array of size 16, spawns 8 threads each of which take 2 chars and then calls encrypt. The encrypt method returns 2 integers which its stores in an array and after all 8 threads have completed their execution it writes the integer array onto afile by thename of "enc_temp.txt" */void parallel_enc (char data[],int key[]){ omp_set_num_threads(8);		// creating 8 threads int i =0; int o=0; int *j; FILE * fp1; #pragma omp parallel		// starting parallel region { switch(omp_get_thread_num())	{	case 0:   	   j = encrypt(data[0],data[1],key);	   a[0] = j[0];	   a[1] = j[1];	   break;        case 1:           j = encrypt(data[2],data[3],key);           a[2] = j[0];           a[3] = j[1];           break;        case 2:           j = encrypt(data[4],data[5],key);           a[4] = j[0];           a[5] = j[1];           break;        case 3:           j= encrypt(data[6],data[7],key);           a[6] = j[0];           a[7] = j[1];           break;        case 4:           j = encrypt(data[8],data[9],key);           a[8] = j[0];           a[9] = j[1];           break;        case 5:           j = encrypt(data[10],data[11],key);           a[10] = j[0];           a[11] = j[1];           break;        case 6:           j = encrypt(data[12],data[13],key);           a[12] = j[0];           a[13] = j[1];           break;        case 7:           j = encrypt(data[14],data[15],key);           a[14] = j[0];           a[15] = j[1];           break;	} }		// end of parallel region, by now we have the integer array assigned.fp1 = fopen("enc_temp.txt","a");  // opening the file in append mode (so that when we write on it the next time, data doesnt get overwritten.for (o=0;o<16;o++)						// wting to the file.							fprintf(fp1,"%d ",a[o]);fclose(fp1);	// closing the file descriptor.} //  end of enc_parallel/*this function takes a file reads the inetegrs in it in chunks of 16 at at time and then calls dec_parallel,  basically it does the reverse of enc_file*/void dec_file(char filename[], char key[]){ FILE *fp1; int p,q; fp1 = fopen(filename,"r"); // opening the file int key_int[8];  int i =0; for (i=0;i<8;i++)		// converting char key into its ASCII value for XORing. key_int[i] = (int) key[i]; int c[16];// reading 16 integers at a time and then calling parallel_dec while(fscanf(fp1,"%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d ",&c[0],&c[1],&c[2],&c[3],&c[4],&c[5],&c[6],&c[7],&c[8],&c[9],&c[10],&c[11],&c[12],&c[13],&c[14],&c[15])==16)  {   parallel_dec(c,key_int);  } fclose(fp1); // closing the file descriptor}/* this method takes an array of 16 integers, spawns 8 threads to decrypt them aprallely and the writes it  output to a file by the name of enc.txt*/void parallel_dec(int data[], int key[]) { omp_set_num_threads(8); // setting number of threads int i =0; int o=0; char *j; FILE * fp1; #pragma omp parallel       // starting parallel region { switch(omp_get_thread_num())	{	case 0:   	   j = decrypt(data[0],data[1],key);	   a1[0] = j[0];	   a1[1] = j[1];	   break;        case 1:           j = decrypt(data[2],data[3],key);           a1[2] = j[0];           a1[3] = j[1];           break;        case 2:           j = decrypt(data[4],data[5],key);           a1[4] = j[0];           a1[5] = j[1];           break;        case 3:           j= decrypt(data[6],data[7],key);           a1[6] = j[0];           a1[7] = j[1];           break;        case 4:           j = decrypt(data[8],data[9],key);           a1[8] = j[0];           a1[9] = j[1];           break;        case 5:           j = decrypt(data[10],data[11],key);           a1[10] = j[0];           a1[11] = j[1];           break;        case 6:           j = decrypt(data[12],data[13],key);           a1[12] = j[0];           a1[13] = j[1];           break;        case 7:           j = decrypt(data[14],data[15],key);           a1[14] = j[0];           a1[15] = j[1];           break;	} } // ending the parallel regionfp1 = fopen("enc.txt","a"); // opening the file for writing decrypted datafor (o=0;o<16;o++)fprintf(fp1,"%c",a1[o]);fclose(fp1);	// closing the file desciprtor. } // end of parallel_dec/* this is the actual encryption method, it takes 2 chars and a key, based on Feistel Algorithm it converts them into 2 integers na dreturns it.*/int* encrypt(char a,char b, int key[]){ int data = (int)a; int data2 = (int)b; int l[16]; int r[16]; l[0] = data; r[0] = data2; int i =0; int j=0; int k =0;	// start of feistel fucntion. 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++;	}// end of feistel funtion.printf("----------------------------- \n")	; int buf[2] ={l[15],r[15]};//printf("buffer :: %d and %d \n",buf[0],buf[1]); return buf; // returns the integer array } // end of encrypt./*this method does the reverse of encrypt, its is based of Feistel Algorithm , takes 2 integers values   converts them to their respective char values and returns it.*/char* decrypt (int l, int r,int key[]){ int fp1; int p,q; int i = 16; int k=6; int l1[16]; int r1[16]; l1[15] = l; r1[15] = r;// start of feistel function 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]);	}char a = (char)(l1[1] ^ key[0]);char b = (char)(r1[1]^key[0]);// end of feirtel function//printf("char value is %c \n",a);//printf("char value 2 is %c \n",b);//printf("******************** \n");char buf[2];buf[1]=a;buf[0]=b;return buf;}// end of derccrypt

⌨️ 快捷键说明

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