Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Affine Cipher Decryption

$
0
0
I'm in the process of writing a code that will decrypt text from the user. From researching, I found the decryption code is a^-1(x-b ) . So I tried to implement it that way in my code but with whatever I input, I get all As. I even tried to basically replace what I had for my encode function with this function. Help me please.
#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;
}       




The smiley face above is supposed to be the letter b. I don't know why it won't let me put b.

Viewing all articles
Browse latest Browse all 51036

Trending Articles