-
File Encryption in C
hi again
here's another trouble
I'm encrypting a file using bit manipulation
the program is compiling succesfully.......and executing sucessfully too
but the result is none......it can't make a encrypted file
I'm using VC++6.0 on windows98 and turboc3.0
but it didn't worked out with any of these two.
here's the code
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
void encrypt();
getch();
return 0;
}
void encrypt()
{
FILE *fs,*ft;
char ch;
fs=fopen("d:\\cprogs\\try.txt","r"); /*normal file */
ft=fopen("d:\\cprogs\\encrypt.txt","w"); /* Encrypted file */
if(fs==NULL||ft==NULL)
{
puts("\nError opening one of the files!!! ");
exit(1);
}
while((ch=fgetc(fs))!=EOF)
fputc(~ch,ft);
fclose(fs);
fclose(ft);
remove("d:\\cprogs\\try.txt");
rename("d:\\cprogs\\encrypt.txt","d:\\windows\\try .txt");
}
-
It looks like all you're doing is reversing the value of the bits. I actually had better results using fprintf than fputc.
Code:
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] ) {
FILE *INSTREAM, *OUTSTREAM;
char ch;
if( ( INSTREAM = fopen( argv[1], "r" ) && ( OUTSTREAM = fopen( argv[2], "w" ) ) == NULL){
printf("Error opening file!");
exit(1);
}
while( fscanf( INSTREAM, "%c", &ch ) != EOF )
fprintf( OUTSTREAM, "%c", ~ch );
fclose( INSTREAM );
fclose( OUTSTREAM );
return 0;
}
-
> ft=fopen("d:\\cprogs\\encrypt.txt","w"); /* Encrypted file */
This should really be opened in "wb" mode.
> char ch;
And this should be
int ch;
otherwise you might never recognise EOF properly.
> void encrypt();
This is a prototype, not a function call.
To call the function, its just
encrypt();