C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 26th, 2012, 04:00 PM
tigerfady11 tigerfady11 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 13 tigerfady11 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 3 h 49 m 15 sec
Reputation 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

Quote:
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

Reply With Quote
  #2  
Old December 26th, 2012, 05:44 PM
tigerfady11 tigerfady11 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 13 tigerfady11 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 3 h 49 m 15 sec
Reputation 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

Reply With Quote
  #3  
Old December 27th, 2012, 12:46 PM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Renaissance Redneck
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,511 DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Weeks 18 h 26 m 26 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
"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.

Reply With Quote
  #4  
Old December 27th, 2012, 02:56 PM
tigerfady11 tigerfady11 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 13 tigerfady11 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 3 h 49 m 15 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #5  
Old December 27th, 2012, 04:21 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 33 m 48 sec
Reputation Power: 1974
Quote:
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.

Reply With Quote
  #6  
Old December 27th, 2012, 04:40 PM
tigerfady11 tigerfady11 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 13 tigerfady11 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 3 h 49 m 15 sec
Reputation 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 !

Reply With Quote
  #7  
Old December 27th, 2012, 05:34 PM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,477 ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 7 h 35 m 10 sec
Reputation Power: 2194
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!

Reply With Quote
  #8  
Old December 27th, 2012, 05:42 PM
tigerfady11 tigerfady11 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 13 tigerfady11 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 3 h 49 m 15 sec
Reputation Power: 0
Quote:
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 ?

Reply With Quote
  #9  
Old December 27th, 2012, 06:02 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 33 m 48 sec
Reputation Power: 1974
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.

Reply With Quote
  #10  
Old December 27th, 2012, 06:10 PM
tigerfady11 tigerfady11 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 13 tigerfady11 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 3 h 49 m 15 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #11  
Old December 27th, 2012, 06:30 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 33 m 48 sec
Reputation Power: 1974
Not hurting, but rather the truth.

We still do not see any honest effort on your part.

Reply With Quote
  #12  
Old December 27th, 2012, 06:34 PM
tigerfady11 tigerfady11 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 13 tigerfady11 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 3 h 49 m 15 sec
Reputation Power: 0
Quote:
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...
Quote:
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 ?

Reply With Quote
  #13  
Old December 28th, 2012, 01:58 AM
lhon12006 lhon12006 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 56 lhon12006 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 15 h 12 m 50 sec
Reputation Power: 0
Quote:
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 ..

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Problem in c program coding and illetracy with C programming language

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap