如何用c++程序解密????????急!!!!!!

来源:百度知道 编辑:UC知道 时间:2024/05/24 00:44:33
8. Your country is at war and your enemies are using a secret code to communicate with each other. Your have managed to interpret a message that reads as follows:
:mmZ\dxZmx]Zpgy
The message is obviously encrypted using the enemy’s secret code. You have just learned that their encryption method is based upon the ASCII code. For example: the letter ‘A’ is encoded using the number 65 and ‘B’ is encoded using the number 66.
Your enemy’s secret code takes each letter of the message and encrypts it as follows:
If (OriginalChar +Key>126) then
EncryptedChar =32+((OriginalChar+Key)-127)
else
EncryptedChar=(OriginalChar+Key)
Write a program that decrypts the interpreted message. You only know that the key used is a number between 1 and 100. When you try the valid key, the message will make sense. For all other keys, message will appear as gibberish.
如果KEY= 7,OriginalChar= ‘A’,’A’的ASCII是65

EncryptedChar= 65+7=72

72

#include "stdafx.h"
#include "string"
#include "iostream"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
string code=":mmZ\\dxZmx]Zpgy";
string s=code;
for (int key=0;key<100;key++)
{
for (int i=0;i<(int)s.size();i++)
{
if (code[i]+key<126)
s[i]=code[i]-key;
else
s[i]=code[i]+95-key;
}
cout<<"key="<<key<<": "<<s<<endl;
};
return 0;
}

/*
观察输出,当key=88时,输出为"Attack at dawn!"其他无意义.
*/