Char Encryption

So, pretty much, I have to read in chars from a file. Then I have to encrypt them. Then encryption code is
1
2
3
4
5
if ((originalChar + encryptedKey) > 126)
	{
		encryptedChar = 32 + ((originalChar + encryptedKey) - 127);
	}
	else encryptedChar = originalChar + encryptedKey;


I have that much. I have to use the getline function to retrieve the data from the file I'm opening, then change the int value of the inputed char to a new value which in turns outputs a different character, which in turn encrypts the message. This is the code I have so far
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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <vector>

using namespace std;

int main ()
{
	string fileName;
	char encryptedChar, originalChar;
	int encryptedKey;
	ifstream file;
	cout << "Please enter the name of the blog file: \n";
	cin >> fileName;
	file.open(fileName.c_str());
	if (file.fail())
	{
		perror ("The following error occured");
		exit(1);
	}
	cout << "Please enter an encryption key between 1 and 100: \n";
	cin >> encryptedKey;
	if (encryptedKey < 1 || encryptedKey > 100)
	{
		cout << "Invalid response.";
		exit(1);
	}
	while (!(file.eof()))
	{
		if ((originalChar + encryptedKey) > 126)
	{
		encryptedChar = 32 + ((originalChar + encryptedKey) - 127);
	}
	else encryptedChar = originalChar + encryptedKey;
	}

}


Thanks for your help. All help is appreciated
To use strings and getline, you'll need the string library. Getline works like this:

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
	string buffer;

	cout<<"Enter filename: "<< endl;
	cin>>buffer;
	ifstream infile(buffer.c_str());

	if(!infile.is_open()){
		cout<<"Could not read file \""<<buffer<<"\""<<endl;
		return -1;
	}

	int i=0;
	while(infile.good()){
		getline(infile,buffer);
		cout<<++i<<": "<<buffer<<endl;
	}
	cout<<"Read "<<i<<" lines."<<endl;

	infile.close();
	return 0;
}


I might add that getline() has the interesting property of including the return character at the end of the line. I don't know if you necessarily want to use C++ std::string for the actual algorithm. A better approach would be to allocate a C-style string instead.

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>

using namespace std;
int main(){
	char *c_buffer,*seek;
	string buffer;

	cout<<"Please enter a line of text."<<endl;
	getline(cin,buffer);

	//Allocate exactly enough memory to hold the entered text
	c_buffer = (char*) calloc(buffer.length()+1, sizeof(char));
	strcpy(c_buffer, buffer.c_str());

	//This for loop iterates through the string until the value of *seek is 0
	//Null-terminated strings always end with character value 0.
	for(seek=c_buffer; *seek; seek++){
		//Your encryption code would go here.
		//For example's sake, we'll just add one.
		*seek+=1;
	}

	cout<<"The encrypted string is: "<<endl
		<<c_buffer<<endl;

	free(c_buffer); //Memory has to be freed after we're done with it.
	return 0;
}


A program like that would encrypt a single line of text. Imagine how it could be expanded to encrypt an entire file.
Last edited on
Topic archived. No new replies allowed.