Encryption Algorithm...

This is a discussion on Encryption Algorithm... within the C++ Programming forums, part of the General Programming Boards category; Okay, I haven't done this programming thing in a long while, so I'm coming back to it slowly... I'm writing ...

  1. #1
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942

    Encryption Algorithm...

    Okay, I haven't done this programming thing in a long while, so I'm coming back to it slowly...

    I'm writing an extremely simple word encryption program, that'll I'll improve later, add decryption and more complex algorithms and such, but first I need to debug it. What it's supposed to do is take the word and add one to each letter(ie. abc would become bcd). For some reason, though, which I can't come up with, it just outputs garbage...

    Here's the program:

    Code:
    #include <iostream>
    #include <cstdlib>
    
    char collect(void);
    char encrypt(char word[40]);
    
    int main()
    {
      char output[40];
      output[40] = collect();
      output[40] = encrypt(output);
      std :: cout << "Your encryption of the word is " << output << ".\n";
      std :: system("pause");
      return (0);
    }
      
    char collect(void)
    {
      char word[40];
      std::cout << "Input your word.\n";
      std::cin >> word;
      return (word[40]);
    }
    
    char encrypt(char word[40])
    {
      int index;
      for(index = 0; index <= 40; index++)
      {
        word[index]++;
        if(!word[index])
        {
          break;
        }
      }
      return (word[40]);
    }
    It's pretty self-explanatory, but in case you guys don't get it, which is unlikely, the function char encrypt(char word[40]) at the end of the program adds one to each letter, and then it checks if there is data in the following array entries. If not, it breaks from the loop and returns the encrypted word. I must have done something wrong, though, and I wondered if you guys would mind helping me out on this one.

    Thanks in advance!(Don't worry. I'll be sure to thank you afterward, also :P)

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    When you use the array like this:
    Code:
      output[40] = collect();
      output[40] = encrypt(output);
    it should be

    Code:
      output = collect();
      output = encrypt(output);
    because in the first example, you are just telling it that the character at 39 in "output" should equal what collect does, it doesn't leave you with a string like you want.

    And the collect and encrypt functions should look like this:
    Code:
    char *collect(void)
    {
      char word[40];
      std::cout << "Input your word.\n";
      std::cin >> word;
      return (word);
    }
    
    char *encrypt(char word[40])
    {
      int index;
      for(index = 0; index <= 40; index++)
      {
        word[index]++;
        if(!word[index])
        {
          break;
        }
      }
      return (word);
    }
    Because you want to return a pointer to a character instead of just one character, the former will be your string.
    Last edited by techrolla; 01-02-2005 at 08:51 PM.
    you make me rery ascared

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    tech your changes wont let it compile.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    sorry, you have to change the prototypes of the functions and change
    Code:
    char output[40];
    to
    Code:
    char *output;
    . There are still errors in the implementation, you don't get the right results, but that is a start.
    you make me rery ascared

  5. #5
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    Okay, thanks.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You think you are passing arrays around but you aren't.

    Lets make this a little easier by using std::string

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    std::string collect(void);
    std::string encrypt(std::string &);
    
    int main()
    {
      std::string output = collect();
      output = encrypt(output);
      std :: cout << "Your encryption of the word is " << output << ".\n";
      std::cin.ignore();
      return 0;
    }
      
    std::string collect(void)
    {
      std::string word;
      std::cout << "Input your word.\n";
      std::cin >> word;
      return word;
    }
    
    std::string encrypt(std::string& word)
    {
      int index;
      for(index = 0; index <= word.length(); index++)
        word[index]++;
      return word;
    }

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    i'm not sure about this implementation.. but just as a general rule, i'm kinda hesitant about using <string> when trying to encrypt a password.. since string literals can be seen with an editor. I think this is a good example of when <cstring> character arrays have an advantage over using the string class.
    Last edited by The Brain; 01-02-2005 at 09:20 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The Brain please show me one string literal in my program tht isn't a prompt.
    In fact std::string is the better option. Here is why.
    A null terminated array of characters (aka c-string) stores the characters in contigious memory.
    A std::string may use array segments, meaning that the entire string may not be stored in the same contigious block of memory.

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    Here it is with c strings, this works for me, and it gets rid of returning local variables, etc.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    void collect(char *str);
    char *encrypt(char *word);
    
    int main()
    {
      char str[40];
      collect(str);
      std::cout << "your word: " << str << std::endl;
      std :: cout << "Your encryption of the word is " << encrypt(str) << ".\n";
      return (0);
    }
    
    void collect(char *str)
    {
      std::cout << "Input your word.\n";
      std::cin >> str;
    }
    
    char *encrypt(char *word)
    {
      int index;
      for(index = 0; index < strlen(word); index++)
      {
        word[index]++;
      }
      return (word);
    }
    you make me rery ascared

  10. #10
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Since when does the string class use string literals?

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by techrolla
    Here it is with c strings, this works for me, and it gets rid of returning local variables, etc.

    Code:
    void collect(char *str)
    {
      std::cout << "Input your word.\n";
      std::cin >> str;
    }
    Please shut up now. This is your third post of crap in this thread.

    What happens if I input more then 40 characters?

  12. #12
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Sean ^

    It doesnt.

  13. #13
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    perhaps i am not using the correct terminology.. but from my (somewhat limited) experience.. when doing something like this for example:

    Code:
    string password = "I love c++";
    it would appear like this when viewed through a hex editor:

    Code:
    L:KJ:L@KH%LKH%H^"I love c++"!#@$@#% @#

    as opposed to:
    Code:
    char password[] = "I love c++";
    which would appear as miscelaneous shizzle when viewed through a hex editor.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  14. #14
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Quote Originally Posted by RoD
    It doesnt.
    I know that... I wasn't asking the question for educational gain.


    Hey The Brain - No.

  15. #15
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    Quote Originally Posted by Thantos
    Please shut up now. This is your third post of crap in this thread.

    What happens if I input more then 40 characters?
    I was simply helping him with what he already had. His didn't offer for input of more than 40 characters. Just trying to help the guy, relax man.
    you make me rery ascared

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RSA Encryption Algorithm help
    By gL_nEwB in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2008, 04:14 AM
  2. My Encryption Algorithm...
    By Junior89 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-26-2007, 03:53 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. abt encryption algorithm
    By purIn in forum C Programming
    Replies: 9
    Last Post: 12-22-2003, 09:16 PM
  5. What's wrong with my Stream Cipher Encryption?
    By Davros in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2002, 09:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21