我目前正試圖做一個替換加密/解密程序。該程序從文件中讀取並且基於用戶給定密鑰逐個字符地加密。例如,如果該文件讀取「ATTACK AT DAWN」並且給出的關鍵字是「LEMON」,則產生的密文將是「LXFOPV EF RNHR」。替換加密不返回字符
我做了一個加密功能,但是,它將值更改爲除字母數字字母以外的其他字符。任何幫助,將不勝感激。我粘貼了以下功能:
void encrypt(ifstream& in_s, ofstream& out_s, char next, char& cypher, int& i, char key[])
{
in_s.get(next); // get character from file
while (!in_s.eof())
{
check_i(i); // check i value is within the constraint for key[4]
if(isupper(next)) // if uppercase run cypher
{
cypher = ((next - 65) + (key[i] - 65) % 26) + 65;
}
else if (islower(next))
{
next = toupper(next); // change to upper
cypher = ((next - 65) + (key[i] - 65) % 26) + 65;
cypher = tolower(cypher); // change to lower
}
else // if blank do nothing
{
cypher = next;
}
cout << cypher;
in_s.get(next); // get next char
i++; // add 1 to i for key[i]
}
}
我明白了!感謝您的意見。我已經解決了這個問題,但是「加密」字符仍然不是全部字母數字。我想這可能是我的關鍵功能有問題。 –
很難評論,因爲你從未展示過你的程序的_actual_輸出,並且你沒有顯示'check_i'函數的定義。 – paddy