Ceaser Cipher encryption

Hi all!

I am having an issue with one of my assignment of Cypher decrypting. I wanted to create a cypher class. This class will have a constructor which takes a 2 parameter key, and applies an affine shift of the form f(x) = ax + b, where the output character is the cyphertext of the plaintext character x.
javascript:PostPreview()
I am getting several errors.

Following is my code:

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

using namespace std;

class Cypher

{
public:
	Cypher(int aVal, int bVal);

	Cypher();

	void setMessage(char Message[20]);
	void getMessage();
private:

	int a;
	int b;
	char decrypted[20];

};


int main()
{
	Cypher cypher1(3, 4);

	cout << "Decrypted message: \n";
	cypher1.setMessage("abcd");
	cout << cypher1.getMessage() << endl;
}

Cypher::Cypher(int aVal, int bVal) : a(aVal), b(bVal)
{
}

Cypher::Cypher() : a(1), b(1)
{
}

void Cypher::setMessage(char Message[20])
{
	int i = 0;
	while (Message[i] != "\0")){

	Message[i] = char((a*Message[i] + b)%26 + 65);
	}
	decrypted = Message;
}

void Cypher::getMessage()
{
	return decrypted;
}


I would really appreciate any of your help.
Last edited on
Welcome to cplusplus!

The nature of the error you are receiving is not known to us. It would help if you:

1. Put your code in code tags ("<>")
2. State what line is producing the error in your code
3. Paste the errors you are recieving
and applies an affine shift of the form f(x) = ax + b


The Caesar cipher works like this:
alphabet = "abcdefghijklmnopqrstuvwxyz"

(user enters shift n == 2)
(user enters message msg == "hello")

(computing message enc from msg and n)
h -> j
e -> g
l -> n
l -> n
o -> q

(enc == "jgnnq")


I would really appreciate any of your help.

I have a suggestion: use std::string (C++ string) instead of char[20] (C string).
http://cplusplus.com/reference/string/basic_string/
Thank you guys for your suggestions. I used std::string but it still showing lots of errors. some of them are showing below:

1
2
3
4
5
6
7
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]


I am pasting my changed code again:

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

using namespace std;

class Cypher

{
public:
	Cypher(int aVal, int bVal);

	Cypher();

	void setMessage(string Message);
	void getMessage();
private:

	int a;
	int b;
	string decrypted;

};


int main()
{
	Cypher cypher1(3, 4);

	cout << "Decrypted message: \n";
	cypher1.setMessage("abcd");
	cout << cypher1.getMessage() << endl;
}

Cypher::Cypher(int aVal, int bVal) : a(aVal), b(bVal)
{
}

Cypher::Cypher() : a(1), b(1)
{
}

void Cypher::setMessage(string Message)
{
	int i = 0;
	while (Message[i] != "\0")){

	Message[i] = char((a*Message[i] + b)%26 + 65);
	}
	decrypted = Message;
}

void Cypher::getMessage()
{
	return decrypted;
}


Thank you again for your help.
Last edited on
1
2
3
4
string Cypher::getMessage()
{
	return decrypted;
}


I do not understand why you use the combination of a and b instead of a single shift n.

Also your while() in Cypher::setMessage() can be rewritten as:

1
2
for (std::string::iterator it = Message.begin(); it != Message.end(); ++it)
    *it = char((a * (*it) + b) % 26 + 65);


or

1
2
for (size_t i=0; i < Message.length(); ++i)
    Message[i] = char((a*Message[i] + b)%26 + 65);


If you're using Visual Studio 2012 which supports C++11 (the C++ standard of 2011), you can use range-based for() loops.

1
2
for (char ch: Message)
    ch = char((a * ch + b) % 26 + 65);
Topic archived. No new replies allowed.