Neuron
Electronic.
Re: Post ur C/C++ Programs Here
Okay ,a file encryptor and decryptor.
In Vista or Win7 run as admin.
File path format eg: c:/abc.exe
No spaces allowed in path.eg: c:/Program Files/abc.exe is invalid.
Okay ,a file encryptor and decryptor.
Code:
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main() {
FILE *fs,*ft;
char c=0,n1[89],n2[89],pass[20],pass1[20],p,n3[89],ext[7];
int passlen,i,skp=0,nlen,tflg=0,slen,k,j=0,l;
while(1) {
i=0;
printf("\n1.Encrypt\t2.Decrypt\t3.Exit\n>>>");
c=getch();
switch(c) {
case '1':
printf("Enter the name of the source file>>>");
scanf("%s",n1);
strcpy(n2,n1);
strcat(n2,".ncrpt");
slen=strlen(n1);
if(n1[slen-4]=='.'&&n1[slen-3]=='t'&&n1[slen-2]=='x'&&n1[slen-1]=='t')
tflg=1;
else tflg=0;
fs=fopen(n1,"rb");
if(fs==NULL) {
puts("Unable to open the source file");
break;
}
printf("Enter the password>>>");
while((p=getch())&&p!=13) {
pass[i]=p;
i++;
}
pass[i]='\0';
printf("\nConfirm password>>>");
i=0;
while((p=getch())&&p!=13) {
pass1[i]=p;
i++;
}
pass1[i]='\0';
if(!strcmp(pass,pass1)) {
printf("\n\nEncrypting...\n");
passlen=strlen(pass);
ft=fopen(n2,"wb");
if(ft==NULL) {
puts("Unable to open the destination file");
break;
}
i=0;
if(!feof(fs))
do {
if(tflg==1) {
c=fgetc(fs);
fputc(c+pass[i],ft);
i++;
if(i==passlen)
i=0;
}
else if(skp<180) {
c=fgetc(fs);
fputc(c,ft);
skp++;
}
else {
c=fgetc(fs);
fputc(c+pass[i],ft);
i++;
if(i==passlen)
i=0;
}
if(feof(fs))
break;
} while(!feof(fs));
}
else {
printf("\nPassword Confirmation failed");
break;
}
skp=0;
printf("\nDone!!!\n");
printf("\n\nThe encrypted file is <<<%s",n2);
fclose(fs);
fclose(ft);
getch();
break;
case '2':
printf("\nEnter the name of the file to be decrypted>>>");
scanf("%s",n1);
nlen=strlen(n1);
if(n1[nlen-6]!='.') {
printf("\nCannot decrypt\n");
break;
}
if(n1[nlen-5]!='n') {
printf("\nCannot decrypt\n");
break;
}
if(n1[nlen-4]!='c') {
printf("\nCannot decrypt\n");
break;
}
if(n1[nlen-3]!='r') {
printf("\nCannot decrypt\n");
break;
}
if(n1[nlen-2]!='p') {
printf("\nCannot decrypt\n");
break;
}
if(n1[nlen-1]!='t') {
printf("\nCannot decrypt\n");
break;
}
if(n1[nlen-10]=='.'&&n1[nlen-9]=='t'&&n1[nlen-8]=='x'&&n1[nlen-7]=='t')
tflg=1;
printf("\nEnter the password>>>");
while((p=getch())&&p!=13) {
pass[i]=p;
i++;
}
pass[i]='\0';
printf("\nConfirm password>>>");
i=0;
while((p=getch())&&p!=13) {
pass1[i]=p;
i++;
}
pass1[i]='\0';
passlen=strlen(pass);
if(!strcmp(pass,pass1)) {
printf("\n\nDecrypting...\n");
for(i=0;i<nlen-6;i++)
n2[i]=n1[i];
n2[i]='\0';
for(i=nlen-7,j=0;n2[i]!='.';i--,j++)
ext[j]=n2[i];
l=strlen(n2);
ext[j]='\0';
n2[l-strlen(ext)-1]='_';
n2[l-strlen(ext)]='.';
for(i=(l-strlen(ext)+1),j--;j>=0;j--,i++)
n2[i]=ext[j];
n2[i]='\0';
fs=fopen(n1,"rb");
if(fs==NULL) {
puts("Unable to open the source file");
break;
}
ft=fopen(n2,"wb");
if(ft==NULL) {
puts("Unable to open the destination file");
break;
}
i=0;
if(!feof(fs))
do {
if(tflg==1) {
c=fgetc(fs);
if(!feof(fs))
fputc(c-pass[i],ft);
i++;
if(i==passlen)
i=0;
}
else if(skp<180) {
c=fgetc(fs);
fputc(c,ft);
skp++;
}
else {
c=fgetc(fs);
fputc(c-pass[i],ft);
i++;
if(i==passlen)
i=0;
}
} while(!feof(fs));
printf("\nDone!!!\n");
printf("\n\nThe decrypted file is>>%s",n2);
}
else {
printf("\nPassword confirmation failed");
break;
}
fclose(fs);
fclose(ft);
break;
case '3':
exit(0);
default:
printf("\nInvalid choice\n");
break;
}
}
}
In Vista or Win7 run as admin.
File path format eg: c:/abc.exe
No spaces allowed in path.eg: c:/Program Files/abc.exe is invalid.
Last edited: