encryption

This is a discussion on encryption within the C Programming forums, part of the General Programming Boards category; hello again... just wondered if you would check this code for me and give me some pointers.. (maybe i could ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    4

    encryption

    hello again...
    just wondered if you would check this code for me and give me some pointers.. (maybe i could improve it).
    it's a XOR encryption code.. , the only problem is that it is quite slow....

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <io.h>
    #include <conio.h>
    void ****it()
    {
    printf("can't write to file");
    getch();
    exit(1);
    }
    void main()
    {
    FILE *in,*out;
    char ch,key[100],fname[100],fname2[100];
    int i,s,len=0;
    float f,count=0;
    clrscr();
    printf("enter source file: ");
    gets(fname);
    if(access(fname,0))
    {
    printf("no file by that name");
    getch();
    exit(1);
    }
    printf("enter target file: ");
    gets(fname2);
    printf("enter key: ");
    gets(key);
    len=strlen(key);
    if((out=fopen(fname2,"wb"))==NULL) ****it();
    if((in=fopen(fname,"rb"))==NULL) ****it();
    fseek(in,SEEK_SET,0);
    for(f=0;!(feof(in));f++)
    fgetc(in);
    fseek(in,SEEK_SET,0);
    i=0;
    printf("processing please wait... ");
    printf("\npress any key to stop\n");
    while((!(feof(in)))&&(!kbhit()))
    {
    _setcursortype(_NOCURSOR);
    ch=fgetc(in);
    count++;
    cprintf("%8.0f precent done\r",(count/f)*100);
    if(i>len-1) i=0;
    ch=ch^key[i];
    fputc(ch,out);
    i++;
    }
    if(feof(in)) cprintf("\ncomplete");
    else cprintf("\n processing has been stoped - fail to complete");
    fclose(in);
    fclose(out);
    getch();
    exit(1);
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    There are a lot of ways you can speed up your code

    >for(f=0;!(feof(in));f++)
    >fgetc(in);

    Im assuming your trying to get the file length. Ist, dont read the entire file char-by-char, just seek to the end then get the position:

    fseek(fp, 0, SEEK_END);
    f = ftell(fp);

    also, declaring 'f' as a float seems pointless, long is better, then replace your percent done statement with this:

    cprintf("%8.0f precent done\r", 100.*count/f);

    the '.' after the 100 will tell the compliler it is of type float, and the rest will get promoted automatically

    >ch=ch^key[i];

    no no no, your reading in each char one at a time. Read in a large buffer - the bigger the faster - then encrypt the buffer and write it back to file.

    also printing the percent done between every char greatly slows down the encryption because printing to the screen is not very fast, but that will be taken care of when you read in large buffers instead of individual chars.

    >****it();
    lol

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    4

    thanks, that realy helps a lot but....

    I have allready tried doing it with strings..
    2 problems though...

    1. is the MAX of a string - 10,000 charecters?, the compiler doesn't let me do more then that (using borland turbo c++)...
    I've allready tried strings and still it's quite slow...
    2. I'm having problem recognizing the EOF with strings, does it take 2 bytes or just one??? the target file always adds an extra byte at the end of it and i'm confused....

    thanks for helping me, I've been around this site and I think it kicks ass...
    keep on the good code

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Yes arrays can only hold so much. Try doing it in 5,000 character chunks until you're finished.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    I FOUND YOUR PROBLEM.
    you're using printf too much for the percentage..it's slowing the whole thing down

    at the top of main do
    Code:
    float x=0;
    then in the percentage bit, do this:
    Code:
    if((count/f) > x)
    {
    x = (count/f);
    cprintf("%8.0f percent done",x*100);
    }
    speeds the whole thing up ****loads.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. help needed with edit control & encryption
    By willc0de4food in forum Windows Programming
    Replies: 2
    Last Post: 03-16-2006, 07:21 PM
  3. abt encryption algorithm
    By purIn in forum C Programming
    Replies: 9
    Last Post: 12-22-2003, 09:16 PM
  4. What's wrong with my Stream Cipher Encryption?
    By Davros in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2002, 09:51 PM
  5. File Encryption & Read/Write in Binary Mode
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 05:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21