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?

Salem commented: Good job on using code tags with your first post. A feat so rare, people who manage it get cookies! +36

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

Cheers, it worked perfectly when I did that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.