Vernam Cipher Problem

Hello, I am having a problem with my Vernam cipher code. I'm not very C++ savvy and I'm having a hard time fixing my errors. I don't think my lines transferred correctly to the post. My apologies.

The errors I'm having are:
../src/Encryptor.h:16: error: extra qualification ‘Encryptor::’ on member ‘encrypt’
../src/Encryptor.h:17: error: extra qualification ‘Encryptor::’ on member ‘decrypt’
../src/Encryptor.cpp: In function ‘void First_test(std::string, std::string)’:
../src/Encryptor.cpp:54: error: no matching function for call to ‘Encryptor::encrypt(std::string&)’
../src/Encryptor.cpp:19: note: candidates are: std::string Encryptor::encrypt(std::string, std::string)
../src/Encryptor.cpp:57: error: no matching function for call to ‘Encryptor::decrypt(std::string&)’
../src/Encryptor.cpp:32: note: candidates are: std::string Encryptor::decrypt(std::string, std::string)
../src/Encryptor.cpp: In function ‘void Second_test(std::string, std::string)’:
../src/Encryptor.cpp:70: error: no matching function for call to ‘Encryptor::encrypt(std::string&)’
../src/Encryptor.cpp:19: note: candidates are: std::string Encryptor::encrypt(std::string, std::string)
../src/Encryptor.cpp:73: error: no matching function for call to ‘Encryptor::decrypt(std::string&)’
../src/Encryptor.cpp:32: note: candidates are: std::string Encryptor::decrypt(std::string, std::string)


This is my .hpp file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef ENCRYPTOR_H_
#define ENCRYPTOR_H_
#include <string>

class Encryptor {
public:
	Encryptor();
	Encryptor(std::string key);
	std::string Encryptor::encrypt(std::string plaintext, std::string value);
	std::string Encryptor::decrypt(std::string ciphertext, std::string str);
	void First_test(std::string key, std::string plaintext);
	void Second_test (std::string key, std::string plaintext);



private:
	std::string key;

};

#endif /* ENCRYPTOR_H_ */ 




Here is my .cpp file.
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
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>
#include "Encryptor.h"

Encryptor::Encryptor(std::string key) {
	this->key = key;
	// TODO Auto-generated constructor stub

}


//This is where we encrypt the plaintext.
std::string Encryptor::encrypt(std::string plaintext, std::string value) {
	std::string retval;
	std::string ciphertext;
	unsigned int k=0;

	for (unsigned int v = 0; v<value.length();v++) {
		retval.at(v) = value.at(7) ^ key.at(0);
		k = (++k < key.length()?k:0);
	}
	return retval;
}

//This is where we decrypt the cyphertext.
std::string Encryptor::decrypt(std::string ciphertext, std::string str) {
	std::string dec(str);
	unsigned int k = 0;

	for (unsigned int i = 0; i <str.length(); i++) {
		dec[i] = str[i] ^ key[k];
		k = (++k < key.length() ?k: 0);
	}
	return dec;
};

//First Test
void First_test(std::string key, std::string plaintext) {
	Encryptor en(key);
	if(key.length() < plaintext.length())
		std::cout << "The plaintext is longer than the key\n";
	else if (key.length() == plaintext.length())
		std::cout << "The plaintext is the same length as the key\n";
	else
		std::cout << "The plaintext is shorter than the key.\n";

//Encrypting the statement.
	std::string ciphertext = en.encrypt(plaintext);
	std::cout << "\nCipherText:" << ciphertext << std::endl;
//Decrypting the statement.
	std::string text = en.decrypt(ciphertext);
	std::cout <<"\nDecryptedText:" << text << std::endl;
if (text == plaintext)
	std::cout << "\n Encrypt and Decrypts are correct.\n";
else
	std::cout << "\nThey are not correct.\n";
}

//This is the function for the second test.
void Second_test(std::string key, std::string plaintext) {
	Encryptor en(key);
	Encryptor en2("newKey");
//Encrypting the statement.
	std::string ciphertext = en.encrypt(plaintext);
	std::cout << "\nCipherText:" << ciphertext << std::endl;
//Decrypting the statement.
	std::string text = en2.decrypt(ciphertext);
	std::cout << "\n DecryptedText:" << text << std::endl;
	if (text == plaintext)
		std::cout << "\n Encrypt and Decrypts are correct.\n";
	else
		std::cout << "\nThey are not correct.\n";


}
Last edited on
In your .hpp:
Line 9/10: Remove the extra Encryptor::

In your .cpp:
Both encrypt and decrypt require two parameter, but you provide just one.
First_test and Second_test are members of the class in your .hpp
How do I go about to fix the First_test and Second_test?
Can I have some more help?
They (that is, First_test() and Second_test()) should probably either be in your class in both files, or outside of it in both files, not mixed as you have it. Is that what you were asking about?
Topic archived. No new replies allowed.