C++ 加密/解密程序C++;

C++ 加密/解密程序C++;,c++,encryption,codeblocks,C++,Encryption,Codeblocks,我的家庭作业是制作一个加密/解密程序,用户在其中输入消息和具有相同字母数的密钥。 程序加密消息并显示它,然后解密密码并再次显示消息。 我在解密密码时遇到了一个问题,当我写HELLO时,密钥是JHBZA,它会显示HELPO而不是HELLO。代码中有什么问题 #include <iostream> #include <string> #include <iomanip> using namespace std; int main() { c

我的家庭作业是制作一个加密/解密程序,用户在其中输入消息和具有相同字母数的密钥。 程序加密消息并显示它,然后解密密码并再次显示消息。 我在解密密码时遇到了一个问题,当我写HELLO时,密钥是JHBZA,它会显示HELPO而不是HELLO。代码中有什么问题

       #include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
    char message[100], key[100], encryption[100], decryption[100];
    int msgvalue [100], msgvalue2 [100], keyvalue [100], keyvalue2 [100], sum[100], decryptvalue[100], decryptvalue2[100];
    int i=0;

cout << "Enter the message: ";
cin.getline(message, 100);
while(message[i] != '\0')
{

    msgvalue[i] = message[i];
    msgvalue2[i] = msgvalue[i] - 65;
    i++;
}

i=0;

cout << "Enter the key: ";
cin.getline(key, 100);
while(key[i] != '\0')
{
    keyvalue[i] = key[i];
    keyvalue2[i] = keyvalue[i] -65;
    i++;
}



cout << "The message is: " << setw(15) << message << endl;
for(int i = 0; msgvalue[i] > 1; i++)
{
sum [i] = msgvalue2[i] + keyvalue2[i];
sum[i] = sum[i] % 26;
}



cout << "The cipher is: " << setw(12);

for(int i = 0; msgvalue[i] >= 65 && msgvalue[i] <= 90; i++)
{
 encryption[i] = sum[i] + 65;

 cout << encryption[i];
}


cout << endl << "The message again is: " << setw(12);

for(int i = 0; msgvalue[i] >= 65 && msgvalue[i] <= 90; i++)
{
decryptvalue[i] =(sum[i] - keyvalue2[i]) % 26;

if (decryptvalue[i] < 0)
{
    decryptvalue[i] = -decryptvalue[i] ;
}


decryptvalue2[i]  = decryptvalue[i] + 65;

decryption[i] = decryptvalue2[i];

cout << decryption[i];

}


    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符消息[100],密钥[100],加密[100],解密[100];
int-msgvalue[100],msgvalue 2[100],keyvalue[100],keyvalue2[100],sum[100],decryptvalue[100],decryptvalue2[100];
int i=0;
cout'Z'=25
L=11

因此(25+11)%26=10

当你去解密它时,(10-25)%26=-15


然后取这个值的绝对值,将其设置为15。相反,当解密值小于0时,正确的做法是加26。这将把-15变成11。11='L'。

所有反斜杠是什么?不知道,但我修复了它。很抱歉!听起来您可能需要学习如何使用调试器逐步完成代码。使用良好的debugger,你可以一行一行地执行你的程序,看看它偏离了你的预期。如果你要做任何编程,这是一个必不可少的工具。进一步阅读:@nathanoliver谢谢你,我会仔细看看!现在我知道问题是这个等式DecryptValue[i]=(sum[i]-keyvalue2[i])%26;当我将H写为消息时,它不适用于从s到z的键