GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #11  
Old 17-Feb-2004, 13:11
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Here you go. Main source code is in the file CryptSource.cpp. I changed everything back to the way it was before I tried to fix the problem... making the variable 'line' in the DecryptBtnClick function static helped, because it seems to get mangled whenever the function is not in control. Same with the ifstream variable. I think it was getting mangled too. I put in a bunch of quick comments to make the code a little easier to read. Thanks for your help!
Attached Files
File Type: zip CryptIt.zip (4.8 KB, 54 views)
  #12  
Old 17-Feb-2004, 14:21
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
I think you may be having a memory problem here.
CPP / C++ / C Code:
char Decrypt(int a, int b)
{
  char intString[2]; //Just using this to put 'a' and 'b' together.
 
  sprintf(intString,"%d%d",a,b); //Put the integer together

Your string is going to be at least 3 digits long (remember the string terminator). So you are defining a string to be 2 charecters and then cramming in 3 charecters. The *problem* with C is that it allows you to do this, which means you could be overwriting anything.

Also as far as I can tell, this:
CPP / C++ / C Code:
x = x+33; //Undo the bias of -33

  for (ch = '!'; ch < 127; ch++) //Just keep incrementing ch until the char
    {                            //matches the ascii value in 'x'
      if (ch == x)
        return ch;
    }
  return ch;
Should be able to be reduced to this:
CPP / C++ / C Code:
return ((char) (x+33));
Last edited by dsmith : 17-Feb-2004 at 14:25. Reason: Made small syntax error (+ instead of -)
  #13  
Old 17-Feb-2004, 16:06
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Yeah, as you can see, I'm not a very experienced coder at all. Thanks for the help. I really should've spotted the problem with intString, but I just am not experienced enough to reduce things quite so easily as far as the other stuff goes.

edit: re-wrote things to match your advice. works great now. although i feel pretty stupid for not being able to reduce such a simple encryption/decryption algorithm. Thanks for all of your help! Apparently my algorithm still needs a little work, because it spits out some weird stuff when I start putting more than one line of text into it :-P

edit again: ok well i figured that i was getting garbled text because i hadn't counted on encrypting a character that would be 3 digits long. so i just said screw it all and added a space each time i encrypt a character. now i just read an int of any length, since that doesn't include whitespace, and cast it as a char. viola. much easier, and no headaches.
  #14  
Old 17-Feb-2004, 16:38
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
I think you are doing a fine job. Little things like this can always get you, especially in C. C is such a powerful language, because it really exposes the underneath workings to the coder. That is why it is so easy to tromp all over your memory.

Another thing to remember about C is that the types are really quite basic. On most modern compilers there is absolutely no difference between a char and an int and all any type comes down to is how big of memory allocation does it need. (Earlier compilers used 8-bit chars & 16-bit ints). Expanding on Walts code a bit, try:
CPP / C++ / C Code:

char ch;

for (ch = 'A'; ch <= 'Z'; ch++)
    printf("%d:%c\n", ch,ch);
This will print the ASCII integer value and the represented charecter.

Anyway, I will take another look at your code and see if I can see why it doesn't work after the first line...
  #15  
Old 17-Feb-2004, 21:02
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Actually it starts garbling after a space. I don't know why the bias would cause it, so I'm going to say the bias isn't causing it. I'm pretty sure there are only 127 basic ascii values that i'm dealing with... and since 127 - 33 leaves me with a number less than 100, I shouldn't be dealing with any numbers greater than 2 digits... it's weird though, because if I typed it out, some of it would come out ok, but it would be all garbled up. Kind of like a burnt letter. Some parts survive, but most of it's crap. Usually starts getting garbled after a space is entered.

Ok got it to accept spaces and newlines, changed the test in the encryption algorithm from a-33 > 0 to a-33 >= 0, and changed the return 0; at the end of my encryption algorithm to return 00; It won't take a newline unless i have an exclamation point at the end. I'm running through debug to see what the heck is going on.
  #16  
Old 03-Mar-2004, 15:08
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Hi, Aaron. If I understand your problem, I think the attached code
is an abstraction of your problem. Note that this illegal activity in
memory may not cause obvious problems in the small test case, but could
be disastrous in the context of a larger application.

Bottom line: you can't prove a program is correct by testing (!)
Or, as one of my old math professors might put it, "Testing is
a necessary, but not sufficient condition to have full confidence
in your code."


Regards,

Dave

CPP / C++ / C Code:
/* demo of how to get in trouble in ways that may not
 * show up in test programs
 * Davekw7x  March, 2004
 */


#include <stdio.h>
#include <string.h>
int main()
{
  void Decrypt(int a, int b);
  char is[100];

  sprintf(is, "%d%d", 'a', 'b');
  printf("Main: strlen(is)  = %d\n", strlen(is));
  printf("Main: is          = <%s>\n", is);

  Decrypt('a', 'b');
  return 0;
}
void Decrypt(int a, int b)
{
  char intString[3]; //Just using this to put 'a' and 'b' together.
  int length;
 
  sprintf(intString,"%d%d",a,b); //Put the integer together
  length = strlen(intString);
  printf("Decrypt: length   = %d\n", length);
  printf("Decrypt: inString = <%s>\n", intString);
  /* Note: two two-digit integers written by sprintf result in
   * a total of five characters written to the array (four
   * digits plus the terminating '\0'
   * (!)
   *
   * Now, on my Windows machine with Borland BCC 5.5.1 and gnu
   * gcc 3.3.1, it seems to work ok, in this simple case, even
   * though it is writing into unknown territory (more bytes 
   * written to intString[] than were allocated).
   *
   * This results in "buffer overflow" conditions that can
   * be very dangerous.
   */
}


  #17  
Old 03-Mar-2004, 16:37
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Thank you. I am fully aware of buffer overflow and how to keep it from happening. I designed that program just to see if I could successfully convert a file full of numbers into some readable text. I didn't really go through it very logically, and I ended up tossing it out. Thanks again for the input, though!
  #18  
Old 03-Mar-2004, 16:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by aaroncohn
Thank you. I am fully aware of buffer overflow and how to keep it from happening. I designed that program just to see if I could successfully convert a file full of numbers into some readable text. I didn't really go through it very logically, and I ended up tossing it out. Thanks again for the input, though!



My final word:

If you printf() with %c rather than %d, you may get closer to what
you were attempting.

Cheers!

Dave
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help.... SLR * algorithm tay C Programming Language 4 10-Sep-2004 12:48
Color scale algorithm Ilya C++ Forum 0 28-Oct-2003 12:07
algorithm question calculus87 C Programming Language 1 11-Oct-2003 10:24
simple form from generated page zuzupus Web Design Forum 0 17-Sep-2003 10:27
Generating simple URLs for search engines jrobbio MySQL / PHP Forum 1 17-Mar-2003 12:22

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:11.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.