Thread: one-time pad cryptor problem

  1. #1
    Registered User surgeon's Avatar
    Join Date
    Jan 2015
    Posts
    26

    one-time pad cryptor problem

    Hi guys, plz help. I write simple cryptor:
    Code:
    #include <stdio.h>
    
    int main(int argc, char* argv[])
    {
        FILE *fp;
        int z,i=0;
        int rbyte;
    
        printf("One-time pad cryptor.\n");
        if (argc!=3) {printf("Usage: %s file cryptkey\n",argv[0]); return 0;}
    
        if (fp=fopen(argv[1],"rw")==0) { printf("Error opening file %s\n",argv[1]); return 0;}
        z=strlen(argv[2]);
        printf("%d\n",z);
    
    
        while ((rbyte=fgetc(fp)))
        {
        
            if (i<=z) i=0;
            rbyte=rbyte^argv[2][i];
            fputc(rbyte,fp);
            i++;
        }
    
        fclose(fp);
        return 0;
    }
    I got segmentation fault. What's the problem?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your "rw" file open mode should probably be "r+" or "r+b".

    This is wrong:
    Code:
    while ((rbyte=fgetc(fp)))
    It should be:
    Code:
    while ((rbyte = fgetc(fp)) != EOF)
    You probably want to reset i to 0 only when i == z, though it may be simpler to just use i % z.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    To add onto what laserlight already noted,

    Code:
    if (fp = fopen(argv[1] , "r+") == 0)
    should be

    Code:
    if ((fp = fopen(argv[1], "r+")) == NULL)

  4. #4
    Registered User surgeon's Avatar
    Join Date
    Jan 2015
    Posts
    26
    You are right. I change
    Code:
    while ((rbyte = fgetc(fp)) != EOF)
    and made
    i==z
    but still got segmentation fault.
    Look:
    Code:
    while ((rbyte=fgetc(fp))!=EOF)
        {
            printf("Ok\n");
            if (i==z) i=0;
            printf("rbyte is %d, argv[2][i] is %d",rbyte,argv[2][i]);
            rbyte=rbyte^argv[2][i];
            fputc(rbyte,fp);
            i++;
        }
    but i got "segmentation fault".


    "Ok" didnt printf.
    P.S. Sorry for my english.

  5. #5
    Registered User surgeon's Avatar
    Join Date
    Jan 2015
    Posts
    26
    Yes, Aslaville, you are right.
    Now, next problem.
    Code:
       if (i==z) i=0;
            rbyte=rbyte^argv[2][i];
            printf("rbyte is %d\n",rbyte);
            fputc(rbyte,fp);
            i++;
    I write ./crypt test abc
    test -> "testtesttest"

    rbyte is 21
    rbyte is 7
    rbyte is 16
    rbyte is 21
    rbyte is 22
    rbyte is 6
    rbyte is 18
    rbyte is 22
    rbyte is 23
    rbyte is 4
    rbyte is 17
    rbyte is 23

    And...
    cat test -> "testtesttest"

    test file isn't changed!

  6. #6
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Post your current code

  7. #7
    Registered User surgeon's Avatar
    Join Date
    Jan 2015
    Posts
    26
    Code:
    #include <stdio.h>
    
    int main(int argc, char* argv[])
    {
        FILE *fp;
        int z,i=0;
        int rbyte;
    
        printf("One-time pad cryptor.\n");
    
        if (argc!=3) {printf("Usage: %s file cryptkey\n",argv[0]); return 0;}
    
        if ((fp=fopen(argv[1],"rw"))==NULL) { printf("Error opening file %s\n",argv[1]); return 0;}
    
        z=strlen(argv[2]);
    
        while ((rbyte=fgetc(fp))!=EOF)
        {
    
            if (i==z) i=0;
            rbyte=rbyte^argv[2][i];
            printf("rbyte is %d\n",rbyte);
            fputc(rbyte,fp);
            i++;
        }
    
        fclose(fp);
        return 0;
    }

  8. #8
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by surgeon View Post
    Code:
    #include <stdio.h>
    
    int main(int argc, char* argv[])
    {
        FILE *fp;
        int z,i=0;
        int rbyte;
    
        printf("One-time pad cryptor.\n");
    
        if (argc!=3) {printf("Usage: %s file cryptkey\n",argv[0]); return 0;}
    
        if ((fp=fopen(argv[1],"rw"))==NULL) { printf("Error opening file %s\n",argv[1]); return 0;}
    
        z=strlen(argv[2]);
    
        while ((rbyte=fgetc(fp))!=EOF)
        {
    
            if (i==z) i=0;
            rbyte=rbyte^argv[2][i];
            printf("rbyte is %d\n",rbyte);
            fputc(rbyte,fp);
            i++;
        }
    
        fclose(fp);
        return 0;
    }
    Re-read post #2, your file opening mode is not correct.

  9. #9
    Registered User surgeon's Avatar
    Join Date
    Jan 2015
    Posts
    26
    i change opening mode to r+
    cat "hello world" > test
    ./crypt test abc

    program changed test file
    then, i try to decrypt file with command
    ./crypt test abc

    but file test doesn't changed

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You're probably running into a problem with reading and writing to a file at the same time. I suggest that you either read the entire contents from the file, then write to it, or write to a different file. If you really do want to read and write at the same time, then you may need to use fseek to ensure that the write replaces the desired character rather than the next one.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-17-2013, 11:32 PM
  2. Replies: 2
    Last Post: 04-17-2013, 12:25 AM
  3. Replies: 23
    Last Post: 05-22-2011, 11:20 PM
  4. problem with time()
    By jas_atwal in forum Linux Programming
    Replies: 6
    Last Post: 12-10-2007, 05:02 PM
  5. time problem
    By sand_man in forum C Programming
    Replies: 9
    Last Post: 09-13-2005, 05:59 PM