Я пишу две функции, которые шифруют и дешифруют сообщения с использованием аффинного шифра. По некоторым причинам мое шифрование и дешифрование отключены несколькими буквами. Я чувствую, что проблема связана с числами ASCII, не соответствующими формату a = 0, z = 25. Может ли кто-нибудь помочь мне выяснить, что происходит?
Cleopatra
следует зашифровать до whkcjilxi
,
MZDVEZC
следует расшифровать до anthony
Но вместо этогоЯ получаю
Cleopatra = ZKNFMLOAL
и
MZDVEZC = NAGUBAL
.
Основная функция:
int main() {
plaintext = "cleopatra";
ciphertext = affine_encrypt(plaintext, 7, 8);
cout << "4. Encryption of the plaintext: " << plaintext << endl;
cout << " Key: 7x+8" << endl;
cout << " Ciphertext: " << ciphertext;
cout << endl << endl;
ciphertext = "MZDVEZC";
plaintext = affine_decrypt(ciphertext, 5, 12);
cout << "5. Decryption of the ciphertext: " << ciphertext << endl;
cout << " Key: 5x+12" << endl;
cout << " Inverse of 5 is " << affineInverse(5) << endl;
cout << " Plaintext: " << plaintext << endl << endl;
return 0;
}
string affine_decrypt(string message, int a, int b)
{
string dtxt;
for (int i = 0; i < message.length(); i++)
{
dtxt = dtxt + (char)(((affineInverse(a)*(message[i] - b) % 26)) + 65);
}
return dtxt;
}
string affine_encrypt(string message, int a, int b)
{
string ctext = "";
for (int i = 0; i < message.length(); i++)
{
ctext = ctext + (char)((((a * message[i]) + b) % 26) + 65);
}
return ctext;
}
int affineInverse(int input)
{
int check = 0;
for (int i = 0; i < 26; i++)
{
check = (input * i) % 26;
if (check == 1)
{
check = i;
}
}
return check;
}