Is this a good example of encryption?

I have defined a class so that it can closely resemble to what i believe encryption is. Am I even using encryption at all in this class?

The setEncrypt() member function accept an integer representing a "key" and a file object as an argument. If the key passed to the function is 5, then the contents of the file will be displayed.

Is this a good example of what encryption is?

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
class Encrypt : public FileFilter
{
    int encryption;
    char line_ [10000];
    int num_of_bytes;

public:

    virtual void transform (fstream & n)
    { }

     virtual void readFile (fstream & file1)
    { }


    void setEncrypt (int key, fstream & file2)
    {
        int m = 0;
        char byte_position;
        encryption = key;

        while (!file2.eof())
        {
            file2.get (byte_position);
            line_[m] = byte_position;
            m++;
        }
        num_of_bytes = m;
    }


    void CheckKey ()
    {
        if (encryption == 5)
        {
            for (int m = 0; m <= num_of_bytes; m++)
            {
                cout << line_[m];
            }
        }
        else
            cout << "You have entered the wrong key number" << endl;
    }
};
Last edited on
Not really.

Encryption would be anything that obsures the content, called plaintext. But you're not obsuring it at all. You just have some logic that decides whether the plaintext should be displayed, without changing the content.

We can use XOR to encrypt the file content in this way. XOR is a reversible operation, so encryption and decryption are the same.
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
#include <iostream>
#include <string>

char Encrypt(char key, char in)
{
	return in ^ key;
}

std::string Encrypt(const std::string& content, char key)
{
	std::string out;
	for (char ch : content)
		out.push_back(Encrypt(key, ch));

	return out;
}

int main()
{
	const char secret = '7';
	const std::string plaintext = { "C++DotCom" };
	const std::string ciphertext = Encrypt(plaintext, secret);

	char guess = {};
	std::cout << "Please enter the key (a single character): ";
	std::cin >> guess;

	std::cout << Encrypt(ciphertext, guess) << std::endl;
}

https://en.wikipedia.org/wiki/Exclusive_or
Last edited on
Topic archived. No new replies allowed.