would like better implementation

I just did a small text encryption example. But I am unsure how I can make it better. eg. If I read in a text file how to have more "random" series of encryption but still being to reverse the effects to decrypt.
My main problem is I'm terrible with math, so can't really think of a better way to do this. Thanks for looking.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>

char * cstr;
std::string encrypt(std::string);
std::string decrypt(std::string);

int main()
{
	std::string input, temp;

	std::cout << "Enter string to be encrypted:";
	getline(std::cin,input);

	// now encrpyt and print results
	temp = encrypt(input);
	std::cout << "Encrypted text:" << std::endl;
	std::cout << temp << std::endl;

	// now decrypt same
	temp = decrypt(temp);
	std::cout << "Decrypted text:" << std::endl;
	std::cout << temp << std::endl;
	
	
	return 0;
}

std::string encrypt(std::string str)
{
	cstr = new char[str.size()+1];
	strcpy(cstr,str.c_str());

	for (int i=0; i<str.size(); i+=2)
	{
		if (cstr[i]!=' ')
			cstr[i]=++cstr[i];
	}
	
	int temp = str.size();
	str = "";
	for (int j=0;j<temp;j++)
		str += cstr[j];
		
	return str;
}

std::string decrypt(std::string str)
{
	cstr = new char[str.size()+1];
	strcpy(cstr,str.c_str());

	for (int i=0;i<str.size();i+=2)
	{
		if (cstr[i]!=' ')
			cstr[i]=--cstr[i];
	}

	int temp=str.size();
	str = "";
	for (int j=0;j<temp;j++)
		str += cstr[j];

	return str;
}


at the moment it is simply incrementing every 2nd letter that is not a space. was not until after I added the conditional that I realized I could probably increment the space also.

I'm guessing a math based recursion implementation would probably be better, but I have no idea what formula I could use.

EDIT: Also I had problems using strcpy to copy the string back which is why I had to add the :
int temp=str.size();
str = "";
for (int j=0;j<temp;j++)
str += cstr[j];

was just a slack work around...
Last edited on
well there's an encryption program in the source section of this website, you might want to study it.
there's also an awesome notepad example....

but anyhoo I'll check it out.

edit: the encryption program is riddled with errors, and not commented. That sourcecode page is useless.
Last edited on
i cant compile the notepad program but maybe you can. i think one thread discusses it here and says it works for old compilers. i dont know, but the ecryption program it good.
the readme at least proved to be useful:

"This is a program I wrote using vigenere algorithm."

is all it states.

Also I had problems using strcpy to copy the string back which is why I had to add the :
int temp=str.size();
str = "";
for (int j=0;j<temp;j++)
str += cstr[j];

was just a slack work around...


error message was cannot convert const char * to char *
I don't understand that error. well I do, but I don't understand why it thinks str.c_str() is const.
Last edited on
here is a simple encode/decoder that you might want to incorporate, you choose the 'key' and it will encrypt and decrypt according to the key you select.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

// encoder/decoder

#include <iostream>
using namespace std;

int main()

{

  char msg[] = "This is a test";
  char key = 88;
  cout << "Original message: " << msg << "\n";
  for (int i = 0 ; i < strlen(msg); i++)
    msg[i] = msg[i] ^ key;

  cout << "Encoded message: " << msg << "\n";
  
  for (int i = 0 ; i < strlen(msg); i++)
    msg[i] = msg[i] ^ key;

  cout << "Decoded message: " << msg << "\n";
return 0;

}





Original message: This is a test
Encoded message: ?01+x1+x9x,=+,
Decoded message: This is a test
Last edited on
gcampton wrote:
I don't understand why it thinks str.c_str() is const.
because it is
http://www.cplusplus.com/reference/string/string/c_str/
I am going to try the vigenere algorithm encryption, seems like something that will be somewhat
challenging...

thanks guys
Last edited on
because it is


so is there another way i can copy the cstring to a string? besides the clunky for loop
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

int main() {
    char cstr[] = "hello world";
    string str = "";
    //if null terminated
    str.assign(cstr);
    //else
    str.assign(cstr, cstr+11);
    cout << str << '\n';
	return 0;
}
1
2
string str;
str = "This is a null terminated C string";
Bazzy wrote:
1
2
string str;
str = "This is a null terminated C string";


i think gcampton means copying char array (c string) to a string object (c++ string)
I don't see why there is any need to go from a string to a C-style string and back in the original post. Why not just operate on the characters of the string in place?

1
2
3
4
5
6
7
string s = "asdf";

for( string::iterator i = s.begin(); i != s.end(); ++i )
{
    // do whatever encryption/decryption you like
    *i = *i ^ 25;
}

Last edited on
blackcoder41 wrote:
i think gcampton means copying char array (c string) to a string object (c++ string)
There's no difference, a string literal is a char array
1
2
3
string str;
char arr[] = "This is a null terminated C string";
str = arr;
bazzy wrote:
There's no difference, a string literal is a char array

sorry, i think you're right. anyway both methods work.
Topic archived. No new replies allowed.