Affine Encoding Problem

I have the encryption code working so I commented out the functions in main. I'm trying to do a decryption using a^-1(x-b) with the code you see but I get all As when I enter a word like hello. I then tested it with just one letter and still got an A.
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
#include<iostream>
#include<string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

class cypher
{
public:
	char apply_cypher(char c);
	char apply_decryption(char c);
	cypher(int key1, int key2);
	void encode(string answer);
	void decode(string answer);
private:
	int a;
	int b;
};
cypher::cypher(int key1, int key2)
{
	a = key1;
	b = key2;
}
char cypher::apply_decryption(char c)
{
	char result_2 = (((1/a)*(c-b ))%26) + 97;
	return result_2;
}
char cypher::apply_cypher(char c)
{
	char result = ((a*c+b ) % 26)+97;
	return result;
}
void cypher::encode(string answer)
{
	for (std::string::iterator it = answer.begin(); it != answer.end(); ++it){
		if (isalpha(*it)){
			*it = tolower(*it);
			*it = apply_cypher(*it);
			*it = toupper(*it);
			cout << *it;
		}
	}
	cout << endl;
}
void cypher::decode(string answer)
{
	for (std::string::iterator it = answer.begin(); it != answer.end(); ++it){
                if (isalpha(*it)){
                        *it = tolower(*it);
                        //*it = apply_cypher(*it);
                        *it = apply_decryption(*it);
                        *it = toupper(*it);
                        cout << *it;
                }
        }
        cout << endl;
}
int main()
{
	int a = 5, b = 8;
	cypher cyph(a, b );
	string answer;
	cout << "Enter the text you want to cipher:";
	getline(cin,answer);
	//cout << "Cypher:";
	//cyph.encode(answer);
	cout << "Decryption:";
	cyph.decode(answer);
	return 0;
}       
closed account (D4S8vCM9)
Just a quick note:
Line 28: char result_2 = (((1/a)*(c-b ))%26) + 97;

1/a will give in almost all cases an float truncated back to an integer, maybe to look there?
I kinda understand what you are saying and I thought that was maybe an issue too. However, I just briefly looked up ways to fix it and I tried changing it so it said float inverse = 1/a and then plugged the word inverse in place of the 1/a on line 28. However it didn't like it as the % only works with int types. One site suggested somebody use fmod but I see that gives back exact values in decimal but I don't think I want that?
closed account (D4S8vCM9)
Sorry I have not the time today to analyze them complete algorithm, but I traced line 28:

Assume:
1
2
3
4
int a = 5, b = 8;
char c = 65; // 'A'

char result_2 = (((1/a)*(c-b ))%26) + 97;

Drilled down:
char result_2
= ((0.2) * (57))) % 26) + 97
= ((11) % 26) + 97
= (11) + 97
= 108


You can see the 1/a produces an uncatched error which is processed further.

There exist algorithms which trace the "error" up until they reach an threshold to either round them up or down. Sorry, I forgot the name of this class of algorithms, but the Bresenham algorithm for smooth line drawing is a fine example.
I mis-understood the formula for decoding. So now it's fixed and I assume my error is in the wrap around portion so it doesn't go out of bounds. If you type in the word hello, the encryption is ITCCR, which is correct. However, when you type in the word ITCCR the decode function outputs PMTTW.

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

using std::cin;
using std::cout;
using std::endl;
using std::string;

class cypher
{
public:
	char apply_cypher(char c);
	char apply_decryption(char c);
	cypher(int key1, int key2);
	void encode(string answer);
	void decode(string answer);
private:
	int a;
	int b;
};
cypher::cypher(int key1, int key2)
{
	a = key1;
	b = key2;
}
char cypher::apply_decryption(char c)
{
	char result_2 = ((-1*a*(c-b))%26)+97;
	return result_2;
}
char cypher::apply_cypher(char c)
{
	char result = ((a*c+b) % 26)+97;
	return result;
}
void cypher::encode(string answer)
{
	for (std::string::iterator it = answer.begin(); it != answer.end(); ++it){
		if (isalpha(*it)){
			*it = tolower(*it);
			*it = apply_cypher(*it);
			*it = toupper(*it);
			cout << *it;
		}
	}
	cout << endl;
}
void cypher::decode(string answer)
{
	for (std::string::iterator it = answer.begin(); it != answer.end(); ++it){
                if (isalpha(*it)){
                        *it = tolower(*it);
                        *it = apply_decryption(*it);
                        *it = toupper(*it);
                        cout << *it;
                }
        }
        cout << endl;
}
int main()
{
	int a = 5, b = 8;
	cypher cyph(a, b);
	string answer;
	cout << "Enter the text you want to cipher:";
	getline(cin,answer);
	cout << "Cypher:";
	cyph.encode(answer);
	cout << "Decryption:";
	cyph.decode(answer);
	return 0;
}       
Topic archived. No new replies allowed.