所以我正在制作一个替换密码,其中 Encipher 和 Decipher 在由单独的各个函数传递特定参数后由同一函数处理。出于某种原因,它正在加密,但它不会解密我的消息。在我的代码中,'cipherAlphabet' 是从关键字创建的,并与普通字母一起使用来解密/加密,同时还打印到文本文件。然而,解密后的代码与加密代码的输出完全相同,我似乎无法理解为什么。任何想法或想法表示赞赏,谢谢!
static string Cipher(string input, string oldAlphabet, string newAlphabet,
string &output)
{
output = "";
int inputLen = input.size();
if (oldAlphabet.size() != newAlphabet.size())
{
cout << "New alphabet doesn't match size of original alphabet";
exit(1);
}
for (int i = 0; i < inputLen; ++i)
{
int oldCharIndex = oldAlphabet.find(tolower(input[i]));
if (oldCharIndex >= 0)
output += isupper(input[i]) ? toupper(newAlphabet[oldCharIndex]) : newAlphabet[oldCharIndex];
else
output += input[i];
}
return output;
}
static string Encipher(string input, string cipherAlphabet, string &output)
{
string plainAlphabet = "abcdefghijklmnopqrstuvwxyz";
return Cipher(input, plainAlphabet, cipherAlphabet, output);
}
static string Decipher(string input, string cipherAlphabet, string &output)
{
string plainAlphabet = "abcdefghijklmnopqrstuvwxyz";
return Cipher(input, cipherAlphabet, plainAlphabet, output);
}
string encoder(string key)
{
string encoded = "";
// This array represents the
// 26 letters of alphabets
bool arr[26] = {0};
// This loop inserts the keyword
// at the start of the encoded string
for (int i = 0; i < key.size(); i++)
{
if (key[i] >= 'A' && key[i] <= 'Z')
{
// To check whether the character is inserted
// earlier in the encoded string or not
if (arr[key[i] - 65] == 0)
{
encoded += key[i];
arr[key[i] - 65] = 1;
}
}
else if (key[i] >= 'a' && key[i] <= 'z')
{
if (arr[key[i] - 97] == 0)
{
encoded += key[i] - 32;
arr[key[i] - 97] = 1;
}
}
}
// This loop inserts the remaining
// characters in the encoded string.
for (int i = 0; i < 26; i++)
{
if (arr[i] == 0)
{
arr[i] = 1;
encoded += char(i + 65);
}
}
return encoded;
}
string removeDuplicate(string str)
{
set<char> S, Sd;
string duplicates = "";
string reduced = "";
for (char c : str)
{
if (S.insert(c).second)
reduced += c;
else if (Sd.insert(c).second)
duplicates += c;
}
int length = duplicates.length();
for (int i = 0; i < length; i++)
{
for (int j = 0; j < str.length(); j++)
{
str.erase(std::remove(str.begin(), str.end(), duplicates[i]),
str.end());
}
}
return str;
}
int main(int argc, char **argv)
{
string keyword1;
if (argc > 2)
{
cout << "Invalid input. There should only be 1 input (the keyword)." << endl;
exit(1);
}
else
{
keyword1 = argv[1];
}
string keyword = removeDuplicate(keyword1);
ifstream inFile("plaintext.txt");
ofstream outFile("ciphertext.txt");
ofstream outFile1("decrypted.txt");
inFile.open("plaintext.txt");
std::string content((std::istreambuf_iterator<char>(inFile)),
(std::istreambuf_iterator<char>()));
inFile.close();
string cipherAlphabet = encoder(keyword);
string cipherText;
string plainText;
outFile << Encipher(content, cipherAlphabet, cipherText);
outFile.close();
outFile1 << Decipher(cipherText, cipherAlphabet, plainText);
outFile1.close();
}
答案 0 :(得分:1)
字符串 cipherAlphabet
只包含大写字母。
另一方面,在行中
int oldCharIndex = oldAlphabet.find(tolower(input[i]));
它正在搜索小写字母或非字母字符。
因此,不会有匹配项。
添加
if (oldCharIndex < 0) oldCharIndex = oldAlphabet.find(toupper(input[i]));
在搜索大写字母的行之后将解决问题。