Thread: File Encryption in C

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    21

    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");
    }

  2. #2
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    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;
    }
    pointer = NULL

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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
    &nbsp; encrypt();
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM