1

Hi there.

I have an encryption method which handles a string char by char and converts it to base 32, then adds it onto the result string. However, it seems to completely replace the result string instead of adding onto the end of it. My code:

char *encrypt(std::string input, std::string key) {
	char* result;
	int lenIn = input.length();
	int lenKey = key.length();
	int i = 0;
	int numKey;
	while (i < lenIn) {
		numKey = charCodeAt(key, i % lenKey);
		long n = long(charCodeAt(input, i) + numKey);
		ltostr(n, base32, 32);
		if (i = 0) {
                        //Cannot concatenate a blank char* it seems
			result = base32;
		} else {
			char* tempresult = result;
			sprintf(result,"%s%s",tempresult,base32);
		}
		i++;
	}
	return result;
}

charCodeAt simply gets the unicode number in int format of the character entered. Can anyone help out?

Votes + Comments
Good job on using code tags with your first post. A feat so rare, people who manage it get cookies!
2
Contributors
2
Replies
5
Views
8 Years
Discussion Span
Last Post by Anon17
0

> char* result;
This is an uninitialised pointer!
Result - you're scribbling characters all over someone else's memory - and they're not going to be happy about it.

You're already using std::string, so carry on using std::string for everything else in your program.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.