#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0

    Problem in c program coding and illetracy with C programming language


    Code:
    .#include<stdio.h>
      #include<sys/types.h>
      #include<unistd.h>
      typedef struct decrypt
      {
      int p;
      int k;
     int c;
     c=p^k;
      }decrypt;
      int main()
      {
      decrypt s[10];
      int n,fd,i;
      fd=open("p.c",O_CREAT,777);//creates a new file
      if(fd<0)
      {
      printf("\nFailed to create file\n\n");exit(-1) 
      }
      fd=open("p.c",O_RDWR,777)//open the file for read and write
      printf("Enter the number to decrypt:");
      scanf("%d",&n);
      printf("Enter the key\n");
      for(i=0;i<n;i++0
      { 
      
      scanf("%d",&s[i].p);
      
      scanff("%d",&s[i].n);
      write( fd,s[i],sizeof(s) );//write to the file which the file discriptor is pointing to
      }
      
     return 0;//return success to kernel
      }
    didn't know if I wrote it properly or even how to use semaphores please someone help me in here

    We are requested to implement a code cracking system. The system will deploy four processes (p1,p2,p3 & p4) to try to guess the password of an encrypted code.
    For encryption, we consider that the formula is: C = P XOR K, where
    C = encrypted code
    P = plain text
    K = key
    For decryption the formula is: P = C XOR K. So, your program will try to find K using this formula. The XOR operator is ^.
    Given an encrypted code C and plain code P your program should deploy three processes to try and find the key used to encrypt the plain code. These three processes will be under the control of process P1.
    All processes will send their results to process P1 and process P1 will terminate the three processes and display the encryption key and decrypted text once any of the three processes finds the key.
    For simplicity we will consider that we have the original number P stored in a file called plain.txt and the encrypted code is stored in a file called cipher.txt. Consider that only numbers are stored in these files and they have a range between 0 and 100000000.
    So we have P and C your program should find the key K. Consider that K is a positive number between 0 and 1000000.
    the things I understood is : that I have to fork 4 processes and write them into a buffer where they can be given to the xor operation but didn't understand what is P and K and couldn't code them even or didn't know how to write them in c language
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0
    [Code]
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>

    void CallArgError(char *appname)
    {
    printf("Usage: %s MODE\n\n", appname);
    puts("MODE: e = encrypt/d = decrypt");
    exit(1);
    }

    int ReadVar(char *Var)
    {
    fgets(Var, 258, stdin);
    fflush(stdin);

    int length = strlen(Var);

    int i;
    for(i = 0; i < length; i++)
    Var[i] = tolower(Var[i]);

    return length - 1;
    }

    void EnDecrypt(char *PlainText, int PlainTextLength, char *Key, int KeyLength, char option)
    {
    int i, a = 0;
    char buf = 0;
    for(i = 0; i < PlainTextLength; i++)
    {
    if(option == 'e')
    buf = ((PlainText[i] - 97) + (Key[a] - 97)) % 26;
    else if(option == 'd')
    buf = ((PlainText[i] - 97) - (Key[a] - 97)) % 26;

    buf += 97;
    if(isalpha(buf))
    printf("%c", tolower(buf));

    a++;
    if(a > KeyLength)
    a = 0;
    }
    }

    int main(int argc, char **argv)
    {
    char PlainText[258];
    printf("Enter a 256-length Plaintext:");
    int PlainTextLength = ReadVar(PlainText);

    puts("");

    char Key[258];
    printf("Enter a 256-length key:");
    int KeyLength = ReadVar(Key);

    puts("");

    if((strcmp("e", argv[1]) == 0) || (strcmp("d", argv[1]) == 0))
    EnDecrypt(PlainText, PlainTextLength, Key, KeyLength, argv[1][0]);
    else
    CallArgError(argv[0]);

    puts("");

    return 0;
    }
    [Code/]
    Still no idea about using semaphores and forks

    Comments on this post

    • ptr2void disagrees : FAIL FAIL FAIL FAIL FAIL
  4. #3
  5. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3314
    "illetracy" seems to be going around....
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
    DaWei on Pointers Politically Incorrect.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0
    Originally Posted by DaWei_M
    "illetracy" seems to be going around....
    I am unable to put the correct Ideas using fork(), pipe() buffers and semaphores
    I am trying my best please help me
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally Posted by tigerfady11
    [Code/]
    Well of course that's not going to work, because the syntax is all wrong. These forum code tags work almost exactly like HTML, which means that the slash comes before the tag name, not after; ie, it should be like this, [/Code] (not case sensitive).

    But then you don't even bother to indent your code. Now you're just adding insult to injury and killing any hope that anyone would ever want to bother to try to help you, especially since you have demonstrated that you are beyond all help.

    And it doesn't help any more that you have only bothered to start working on this problem now when we've been getting hit by your fellow students for the better part of the month. What have you been doing in the meantime?

    Also, fork is simple. It was covered in class and in your textbook, and yet here you are begging us for help on fork? Where have you been all this time? What have you been doing? Why didn't bother to read your textbook or to pay attention in class, assuming that you have even bothered to attend class?

    Now, here is how your code should appear when displayed in code tags -- at least you aren't using K&R format to hide open braces at the ends of lines, the only plus so far:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    void CallArgError(char *appname)
    {
        printf("Usage: %s MODE\n\n", appname);
        puts("MODE: e = encrypt/d = decrypt");
        exit(1);
    }
    
    int ReadVar(char *Var)
    {
        fgets(Var, 258, stdin);
        fflush(stdin);
    
        int length = strlen(Var);
    
        int i;
        for(i = 0; i < length; i++)
            Var[i] = tolower(Var[i]);
    
        return length - 1;
    }
    
    void EnDecrypt(char *PlainText, int PlainTextLength, char *Key, int KeyLength, char option)
    {
        int i, a = 0;
        char buf = 0;
        for(i = 0; i < PlainTextLength; i++)
        {
            if(option == 'e')
                buf = ((PlainText[i] - 97) + (Key[a] - 97)) % 26;
            else if(option == 'd')
                buf = ((PlainText[i] - 97) - (Key[a] - 97)) % 26;
    
            buf += 97;
            if(isalpha(buf))
                printf("%c", tolower(buf));
    
            a++;
            if(a > KeyLength)
                a = 0;
        }
    }
    
    int main(int argc, char **argv)
    {
        char PlainText[258];
        printf("Enter a 256-length Plaintext:");
        int PlainTextLength = ReadVar(PlainText);
    
        puts("");
    
        char Key[258];
        printf("Enter a 256-length key:");
        int KeyLength = ReadVar(Key);
    
        puts("");
    
        if((strcmp("e", argv[1]) == 0) || (strcmp("d", argv[1]) == 0))
            EnDecrypt(PlainText, PlainTextLength, Key, KeyLength, argv[1][0]);
        else
            CallArgError(argv[0]);
    
        puts("");
    
        return 0;
    }
    What language are you using? Why didn't you tell us what language you're using? That is not standard C, but rather appears to be C99, which no other professional I know ever uses. If you are using something that is not standard, then you need to let us know that.

    And there's this line which is sheer idiocy:
    fflush(stdin);
    fflush is specified as only working on output files, but not on input files. So here you are trying to use it on an input file, which is undefined, which means that you will never know what you will get. And if it's your instructor who taught you to write that, then tell him that we say that he's an idiot and that he should join this forum so that we can explain that to him.

    Now, if you have specific questions about specific problems that you are having, then ask those specific questions. Doing so will greatly improve your chances.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0
    Dear sir,
    I need to implement this question using unix IPC routines which are pipes, forks and semaphores. and I am trying to solve it since more than 20 days actually and I am unable to do so and I still have 2 days to do that and no one is heling me. I know the logic but I dunno how to write anything writing C language because I am better in java , and .net languages. plz if you can help me I will be really thankful because the instructor is an idiot actually and he gets lost whenever explaining a lecture and more over he explained nothing about these issues he just said this is a project to do , make your own researches and you may ask someone for help !!
    I read pipes, forks and semaphores on my own and I am still feeling stupid and unable to make piece of code using them !
  12. #7
  13. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,556
    Rep Power
    2338
    The provided code is nothing but typical copy-pasta.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0
    Originally Posted by ptr2void
    The provided code is nothing but typical copy-pasta.
    I don't want to give up but I will tell you about the logic I came to think about sir... first I have to write a pipe in the parent process p1 which reads the plaintext and ciphertext containing numbers generated randomly (and is more easier if provided by user) between 0 and 100000000 then sends it throught another pipe after a fork to child process p2, then p2 must contain a function which transforms the decimal numbers in ciphertext and plaintext to binary since the XOR works bitwise ,then sends it throught another pipe after a fork to child process p3, then p3 must contain the function K=P^C which gets the key in binary if we have answer 1 then it is true else it is false , then sends the answer throught another pipe after a fork to child process p4, then p4 must contain a function which transforms the key again to decimal then resends it to P1 which displays the answers from all forks ... is that right ???? but what is a semaphore? and how do I use it in here ? and i think he said 3 trials ? trials for what ?
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    So then shouldn't you have posted your own code so that you could point to where you're having the problem? Instead of stealing somebody else's code which works with an entirely different encryption method and then lying to us?

    And obviously you are also lying to us about having been working on this for 20 days already. And lying to us about knowing Java and .NET.

    Post your own code and point to us where you are having a problem. Though now that we know what an unscrupulous liar you are, we would have to check it out thoroughly. Or else just give up on you.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0
    Originally Posted by dwise1_aol
    So then shouldn't you have posted your own code so that you could point to where you're having the problem? Instead of stealing somebody else's code which works with an entirely different encryption method and then lying to us?

    And obviously you are also lying to us about having been working on this for 20 days already. And lying to us about knowing Java and .NET.

    Post your own code and point to us where you are having a problem. Though now that we know what an unscrupulous liar you are, we would have to check it out thoroughly. Or else just give up on you.
    no sir I am not lying I do know java and vb.net and a little c#.net and I have being trying to understand really what the code implies to since 20 days and I cam up to understand well within today and yesterday because I read alots about ciphering like Caesar and Vigenere and how they work using a key to encrypt and decrypt alphabets. yes I copied the code from somewhere when I read that it works with encryption and decryption to see if someone can guide me to anything that may help me because I am not familiar with C programming language! so if you're so good in C doesn't mean that everyone is , if I don't need help why then am I here ?
    and thanks for your hurting words
  20. #11
  21. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Not hurting, but rather the truth.

    We still do not see any honest effort on your part.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0
    Originally Posted by dwise1_aol
    Not hurting, but rather the truth.

    We still do not see any honest effort on your part.
    sir I just need you to help me and tell me if the logic I am thinking with is true and the steps so I can start writing my code then showing it up and tell the programming problems I had , so I am trying to depend on myself but I need some help to start please...
    the way I came to is that first I have to write a pipe in the parent process p1 which reads the plaintext and ciphertext containing numbers generated randomly (and is more easier if provided by user) between 0 and 100000000 then sends it throught another pipe after a fork to child process p2, then p2 must contain a function which transforms the decimal numbers in ciphertext and plaintext to binary since the XOR works bitwise ,then sends it throught another pipe after a fork to child process p3, then p3 must contain the function K=P^C which gets the key in binary if we have answer 1 then it is true else it is false , then sends the answer throught another pipe after a fork to child process p4, then p4 must contain a function which transforms the key again to decimal then resends it to P1 which displays the answers from all forks ... is that right ???? but what is a semaphore? and how do I use it in here ?
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    86
    Rep Power
    0
    Originally Posted by tigerfady11
    sir I just need you to help me and tell me if the logic I am thinking with is true and the steps so I can start writing my code then showing it up and tell the programming problems I had , so I am trying to depend on myself but I need some help to start please...

    it is better to show the code to know the logic of the program
    all of them here have a helping hands to you will learn more just read and stay here in this group ..
    :rockon: :rockon: :tntworth:

IMN logo majestic logo threadwatch logo seochat tools logo