943,865 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2542
  • C++ RSS
You are currently viewing page 3 of this multi-page discussion thread; Jump to the first page
Sep 10th, 2009
0

Re: Want to change this to two keys one for encryption and other for decryption

Very Right sir,
I can imagine that this will solve our problem easily.
tux4life send me the enc and dec code in reply #8 of this post/thread and i ranit successfully and it worked.

Mathematically its ok
now the problem that tux4life is saying that we use it without file handling
actually problem pop-ups when we use file handling, i will explain the reason.

let say in the input file written is "a"
the ASCII code of " a" is 97
according to our formula i m using 200 mod because 200 mod is sufficient for ASCII code correct me if i m wrong. and 37 and 173 are modulo multiplicative inverse of each other base 200.
so for enc
(97*37)%200 = 189
now dec
(189*173)%200=97
according to my program and tux4life program quoted in number 8 reply, works the same
now problem arises at the place which i m againand again trying to covince is
when it saves the enc data into file let say for 97 it stores 189 it saves some equivalent charater of 189

when it comes to decryption it cannot recognise whether it is 1 or 18 or 189, somebody told me the problem is here
for detail kindly look at the post number 16 of this thread.

hope this might help u get my problem

Best wishes!
Reputation Points: 6
Solved Threads: 0
Newbie Poster
khanalone1 is offline Offline
20 posts
since Aug 2009
Sep 10th, 2009
0

Re: Want to change this to two keys one for encryption and other for decryption

It has nothing to do with files. It has to do with what a char can store versus what an integer can store. On my machine, a char is 8 bits and an int is 32 bits, thus a char ranges from -128 to 127. On my machine, it starts to go haywire when I try to encrypt 4. 4 * 37 = 148, the first number encountered that is over 127. All fine and good with integers. Now decrypt:

148 * 173 = 25, 604
25,604 % 200 = 4

which was the original number. All fine and good, except that isn't what's happening since it's treating 148 as -108 due to overflow. multiply -108 by 173 and you get -18,684. Take the mod of that by 200 and you get -84, which isn't 4. Post 7 tests it with integers, and there's no overflow. With a char, there's overflow, so everything gets screwed up. See the program below. The way it is, it doesn't deal with overflow and thus breaks. Comment the current functions out and uncomment the commented functions and compile/run it again and it should work. The commented functions do the math with integers rather than chars, so there's no overflow.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. /*
  5. char encrypt (char c)
  6. {
  7.   int a = c;
  8.   if (a < 0)
  9.   a = a + 256;
  10.   int b = a * 37;
  11.   char d = b % 200;
  12.   return d;
  13. }
  14.  
  15. char decrypt (char c)
  16. {
  17.   int a = c;
  18.   if (a < 0)
  19.   a = a + 256;
  20.   int b = a * 173;
  21.   char d = b % 200;
  22.   return d;
  23. }
  24. */
  25. char encrypt (char c)
  26. {
  27. return (c * 37) % 200;
  28. }
  29.  
  30. char decrypt (char c)
  31. {
  32. return (c * 173) % 200;
  33. }
  34.  
  35.  
  36. int main ()
  37. {
  38. for (int a = 0; a < 200; a++)
  39. {
  40. char c = a;
  41. char e = encrypt (c);
  42. char f = decrypt (e);
  43. cout << (int) c << " " << (int)e << " " << (int)f << endl;
  44.  
  45. if (c != f)
  46. {
  47. cout << "Problem!!!\n";
  48. cin.get ();
  49. }
  50. }
  51.  
  52. cin.get ();
  53. return 0;
  54. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Sep 10th, 2009
0

Re: Want to change this to two keys one for encryption and other for decryption

its working like a charm.

now if we have a file which contains charaters and numbers

e.g

i am trying to solve this problem dice 6 months, 6 months are equal to 30*6=189 days

will it enc and dec the input file successfully, if yes where shoud i put this code in my orignal program so that it work for me.

suggest the location where i put your code or please put the code in the program so that i can give input from the file, and also get output in the file just like my old program.

the code you have given , what i think will run in linux but my old program wasnot working in linux as header and syntax are different in linux c.

please help me out so that i can give inout from file and output to file as in my old program.

Regards!
Reputation Points: 6
Solved Threads: 0
Newbie Poster
khanalone1 is offline Offline
20 posts
since Aug 2009
Sep 10th, 2009
0

Re: Want to change this to two keys one for encryption and other for decryption

Click to Expand / Collapse  Quote originally posted by khanalone1 ...
its working like a charm.

now if we have a file which contains charaters and numbers

e.g

i am trying to solve this problem dice 6 months, 6 months are equal to 30*6=189 days

will it enc and dec the input file successfully, if yes where shoud i put this code in my orignal program so that it work for me.

suggest the location where i put your code or please put the code in the program so that i can give input from the file, and also get output in the file just like my old program.

the code you have given , what i think will run in linux but my old program wasnot working in linux as header and syntax are different in linux c.

please help me out so that i can give inout from file and output to file as in my old program.

Regards!

You don't have any Linux specific headers in the code you posted in post 5. You have C headers. Make them C++ headers (i.e. stdlib.h turns into cstdlib, time.h turns into ctime, etc.).


Your basic skeleton could be this:

C++ Syntax (Toggle Plain Text)
  1. char encrypt (char c);
  2. char decrypt (char c);
  3. // the above functions are the ones I wrote
  4.  
  5. string encryptString (string line);
  6. string decryptString (string line);
  7. // the above functions call the top functions a character at a time to encrypt an entire string.
  8.  
  9. void encryptFile (char* plainTextFileName, char* encryptedTextFileName);
  10. void decryptFile (char* plainTextFileName, char* encryptedTextFileName);
  11. // these functions open up the files, read a line at a time from the input file, call encryptString or decryptString for each line, and output the altered line to the output file.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Sep 11th, 2009
0

Re: Want to change this to two keys one for encryption and other for decryption

Thanks Sir,

I tried to put this code in my program but lots of errors

Can u make these changes for me in the program that i send on number 16 reply of this thread.

I m zero level programmer in c,
as i worked mostly on linux,server and data comm.

looking forward to your possitive response.
Reputation Points: 6
Solved Threads: 0
Newbie Poster
khanalone1 is offline Offline
20 posts
since Aug 2009
Sep 11th, 2009
0

Re: Want to change this to two keys one for encryption and other for decryption

we are near to the solution but unfortunatly i m layman in c language programing, i.e why i cannot cross the bridge.

is there anyone who can combine all this and edit the program, so that it may be a turn key solution.

i hace the code for RC4 algo which will be the next step after this program.

i will encrypt the enc file with rc4 algo which uses XOR

and send it to sensor node
the receing sensor node will xor again the file which will give the enc file
and the dec ot plaint text sended by the sender will be the output.

then i will put this code in NS2 simulator which is ready for just this program.

my 80% work is complete waiting for this program.

SOS please edit this program for me and give me a turn key solution.

means
the orignal program may be changed acording to the suggestions given by tux4life and VernonDozier
Reputation Points: 6
Solved Threads: 0
Newbie Poster
khanalone1 is offline Offline
20 posts
since Aug 2009
Sep 11th, 2009
0

Re: Want to change this to two keys one for encryption and other for decryption

Go to post 5. Change your red line to:

C++ Syntax (Toggle Plain Text)
  1. cryp = encrypt (x);

Change your green line to:

C++ Syntax (Toggle Plain Text)
  1. decr = decrypt (x);

Where encrypt and decrypt are the functions I posted earlier.

If the original program works, then I would imagine that all that is needed is the above adjustment.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Sep 12th, 2009
1

Re: Want to change this to two keys one for encryption and other for decryption

Okay, I really have enough of your double postings in your thread:

a) you won't get help faster by doing this
b) you just make your own thread unreadable
c) you waste your time, use that time to try figuring out the solution yourself
d) code tags seem useless to you, how did you miss using them?
Information about code tags is found all over Daniweb:
Quote ...
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) any place CODE tags were used
6) Even on the background of the box you actually typed your message in!
e) several other reasons: like asking to create the code for you

I don't see any reason why I should help you, the reasons why I shouldn't help you are denoted above.

I'm going to quote what I already have said before in this thread:
Click to Expand / Collapse  Quote originally posted by tux4life ...
First of all I want you to NOT PM me with your questions to do it for you, it won't work, I'm a free person and I'm free to do what I want, and not to do what I don't want (let that be clear).
What I want is to help you, what I don't want is spend my time to create a full-fledged encryption solution for you (or in other words: I rather provide paths to the solution, not the entire solution itself), so that you can turn it in, and pass (or fail ) using my work, that wouldn't be fair, not? The point of the assignments you get is to show off your knowledge, show what you can. This means that you have to do the work, passing using someone else's work won't get you far in real life. The point is that you do the entire thing yourself (with some small help and hints from the forum members (the so-called "gurus") here), and that you finally (when you finished the program) can say: I did it!
P.S.: all your double postings are horrible, I would opt for an immediate thread lock.

If you don't agree with me, then please go on and double post until you get banned, I don't care about that.

And here, a bunch of links you should read (and agree with) before making your next reply to this thread:
http://www.daniweb.com/forums/faq.ph...niweb_policies
http://www.daniweb.com/forums/thread78223.html
http://www.daniweb.com/forums/announcement8-2.html
http://www.daniweb.com/forums/announcement8-3.html
http://catb.org/~esr/faqs/smart-questions.html
http://cboard.cprogramming.com/c-pro...t-process.html

Have a nice time reading them.
Last edited by tux4life; Sep 12th, 2009 at 5:51 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Sep 12th, 2009
0

Re: Want to change this to two keys one for encryption and other for decryption

Dear Sir,
I am facing packet loss, in my connection, when i press the post quick reply button, the page didnt downloaded properly, so i refreshed it, i.e why it was posted double.

I wont PM you again, i m following you replies, sorry for that
i am new in this forum
and will learn slowly, yet i m trying to learn faster.

Thanks for your guidence
Last edited by khanalone1; Sep 12th, 2009 at 5:53 am.
Reputation Points: 6
Solved Threads: 0
Newbie Poster
khanalone1 is offline Offline
20 posts
since Aug 2009
Sep 12th, 2009
0

Re: Want to change this to two keys one for encryption and other for decryption

Click to Expand / Collapse  Quote originally posted by khanalone1 ...
Dear Sir,
I am facing packet loss, in my connection, when i press the post quick reply button, the page didnt downloaded properly, so i refreshed it, i.e why it was posted double.
Oh yes, and how do you explain the several minutes difference between all those posts then?
The post above this one (if not already removed by a moderator) is just the evidence that it hasn't something to do with your connection (it just again demonstrates you haven't read my previous post(s), otherwise you should have known you should use code tags, but no, copy-paste seems easier to you).
Last edited by tux4life; Sep 12th, 2009 at 5:58 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: template question
Next Thread in C++ Forum Timeline: Array passing problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC