Thursday, August 6, 2009

encrypt and decrypt Your file

Well, iam writing some text files and saving in my pc and all my friends have access it. so, i thought to have some security for it. You can find more advanced tools for files and folders to lock. But this program just gives very minimal sequrity.
you can encrypt your file with four digit key and when ever you want you can retrive it with that key.
If your programer you can break this key with in a minute itself.( i said it gives minimal security.
let see code------

#include "stdio.h"
#include "string.h"
#include "fcntl.h"
#include "malloc.h"
#include "stdlib.h"
#include "termios.h"

/* if you get any error above include change double codes to less than and grater than*/
#define MAX_CHAR 10


void create_write_to_file(char *file_name, char *out_file);
void manuply_pin(char *pin1,char *pin);
char take_input(char *file_name,int *fd);
void read_pin(char *pin1);



typedef struct string_l
{
char ten_chars[MAX_CHAR];
struct string_l *pnext;
}string_link_t;

string_link_t *phead;

int main()
{
char pin1[4]={0},
pin[10]={0},
i=0,
len=10,
enc_or_dec=-1,
flag = 1;
char file_name[31],
out_file[33],
temp_string[11]={0};
int fd;
string_link_t *ptemp, *pmove;



read_pin(pin1);
manuply_pin(pin1,pin);
enc_or_dec=take_input(file_name,&fd);

lseek(fd,0,SEEK_SET);
flag = 1 ;
len = 10;
while( len == 10 && flag == 1)
{
len = read(fd,temp_string,MAX_CHAR);

if(len <= 0) { break; } else if(len < flag =" 0;" ptemp =" (string_link_t">pnext = NULL;
if(!ptemp)
{
printf("CRITICAL ERROR CON'T PROCEED \n");
exit(0);
}
if(!phead)
{
phead = ptemp;
pmove = ptemp;
}
else
{
pmove->pnext = ptemp;
pmove = ptemp;
}
memcpy(ptemp->ten_chars,temp_string,10);
for(i=0; i<10; enc_or_dec ="=">ten_chars[i] = ptemp->ten_chars[i] + pin[i];
}
else if(enc_or_dec == '2')
{
ptemp->ten_chars[i] = ptemp->ten_chars[i] - pin[i];
}
}
else
{
if(enc_or_dec == '1')
{
ptemp->ten_chars[i] = ' ' + pin[i];
}
else if(enc_or_dec == '2')
{
ptemp->ten_chars[i] = ' ' - pin[i];
}

flag = 0;
}

}

}
create_write_to_file(file_name, out_file);
close(fd);
}



char take_input(char *file_name,int *fd)
{
char enc_or_dec=-1;


do
{
printf("enter 1 for encryption 2 for decription\n");
__fpurge(stdin);
scanf("%c",&enc_or_dec);
}
while(!(enc_or_dec == '1'|| enc_or_dec == '2'));

printf("enter file name(Max 30 char): \n");
scanf("%s",file_name);

*fd=open(file_name,O_RDONLY);
if(*fd < len="0," i="0;" len="(int)" i="0;"> '9')
{
printf("pin contain illigal digits\n try again \n");
goto enter_pin;
}
i++;
}
}

read_password(char *pin1)
{
struct termios cuset,newset;
char ch;
char ii=0;
/*Disable echo and canonical mode of processing***/
tcgetattr(0,&cuset);
newset = cuset;
//newset.c_lflag &= ~ICANON;
newset.c_lflag &= ~ECHO;
tcsetattr(0,TCSANOW,&newset);
//setbuf(stdin,NULL);
//setbuf(stdout,NULL);
__fpurge(stdin);
while( ( ch=getchar() ) != '\n' )
{
if(ii<4) fd1="-1;" flag =" 1;" fd1 =" open(out_file,O_WRONLY" flag ="=" flag =" 0;" ptemp =" phead;">ten_chars,MAX_CHAR);
pfree = ptemp;
ptemp = ptemp->pnext;
free(pfree);
}
close(fd1);
}

void manuply_pin(char *pin1,char *pin)
{
pin[0] = pin1[0] + pin1[1] + pin1[2] + pin1[3];
pin[1] = pin1[0] + pin1[1] + pin1[2] - pin1[3];
pin[2] = pin1[0] + pin1[1] - pin1[2] + pin1[3];
pin[3] = pin1[0] + pin1[1] - pin1[2] - pin1[3];
pin[4] = -pin1[0] + pin1[1] + pin1[2] - pin1[3];
pin[5] = -pin1[0] - pin1[1] + pin1[2] + pin1[3];
pin[6] = -pin1[0] - pin1[1] - pin1[2] + pin1[3];
pin[7] = -pin1[0] - pin1[1] - pin1[2] + pin1[3];
pin[8] = pin1[0] - pin1[1] - pin1[2] - pin1[3];
pin[9] = -pin1[0] - pin1[1] + pin1[2] - pin1[3];
}
so now point is how to use it.
save this file with some name say enc_dec.c
$gcc -o enc_dec enc_dec.c (in linux)
you wil get enc_dec executable
now you have secret.txt

now run
./enc_dec
enter 4 digit pin no:****
enter 1 for encryption 2 for decription
1
enter file name(Max 30 char):
secret.txt
output file name
secret.enq
output file is secret.enq

so now you can do this on secret again with diff key.

for
reverse
./enc_dec
enter 4 digit pin no:**** (enter same pin)
enter 1 for encryption 2 for decription
2
enter file name(Max 30 char):
secret.enq
output file name
secret.txt
output file is secret.txt

if you applied keys multiple order then you should follow stack rule.
first key must use last and last one used first only.


No comments: