# Thread: Encryption Algorithm...

1. ## 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. 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.

3. tech your changes wont let it compile.

4. 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.

5. Okay, thanks.

6. 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. 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.

8. 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. 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);
}```

10. Since when does the string class use string literals?

11. 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. Sean ^

It doesnt.

13. 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.

14. Originally Posted by RoD
It doesnt.
I know that... I wasn't asking the question for educational gain.

Hey The Brain - No.

15. 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.

Popular pages Recent additions