Ответы
Чтобы разработать программу шифрования и дешифрования с простым шифром замены на C++, вы можете использовать следующий код:
#include <iostream>
#include <string>
using namespace std;
string encrypt(string plaintext, string key)
{
string ciphertext = "";
for (int i = 0; i < plaintext.length(); i++)
{
char ch = plaintext[i];
int index = (int)(ch);
int newIndex = (index + key[i % key.length()]) % 128;
ciphertext += (char)(newIndex);
}
return ciphertext;
}
string decrypt(string ciphertext, string key)
{
string plaintext = "";
for (int i = 0; i < ciphertext.length(); i++)
{
char ch = ciphertext[i];
int index = (int)(ch);
int newIndex = (index - key[i % key.length()]) % 128;
plaintext += (char)(newIndex);
}
return plaintext;
}
int main()
{
string plaintext, key;
cout << "Введите открытый текст: ";
getline(cin, plaintext);
cout << "Введите ключ: ";
getline(cin, key);
string ciphertext = encrypt(plaintext, key);
cout << "Введите шифротекст: " << ciphertext << endl;
string decryptedText = decrypt(ciphertext, key);
cout << "Введите расшифрованный текст: " << decryptedText << endl;
return 0;
}