/*
Nome: Mario
Cognome: Montanari
Classe: 3AIN
Data: 11/03/2025
*/

#include <iostream>
#include <cctype>

#define SIZE 100+1

using namespace std;

char *decryptAffineCipher(char *const str, const int a, const int b);

int main(void) {
	char str[SIZE];
	int a;
	int b;
	
	cout << "Inserisci una frase: ";
	cin.getline(str, SIZE);
	
	cout << "a: ";
	cin >> a;
	
	cout << "b: ";
	cin >> b;
	
	cout << "Frase codificata:    " << decryptAffineCipher(str, a, b) << endl;
	
	return 0;
}

char *decryptAffineCipher(char *const str, const int a, const int b) {
	int a_inv = 0;
	
	for (int i = 0; i < 26; i++) {
		if ((a * i) % 26 == 1) {
			a_inv = i;
			break;
		}
	}
	
	for (int i = 0; str[i] != '\0'; i++) {
		if (isupper(str[i])) {
			str[i] = ((a_inv * ((str[i] - 'A') - b + 26)) % 26) + 'A';
		} else if (islower(str[i])) {
			str[i] = ((a_inv * ((str[i] - 'a') - b + 26)) % 26) + 'a';
		}
	}
			
	return str;
}