字母加密程序

alphabet encryption program

本文关键字:程序 加密      更新时间:2023-10-16

嗨,我正在开发一个加密程序。程序应该获取用户输入的字母,并将其替换为字母表中第 11 位的相应字母。例如,如果用户输入"joe",程序应该输出"uzp"。

我当前的代码做得很好,但它不能识别空格,程序应该环绕字母表。因此,"Y"变为"J","Z"将变为"K"等。有人知道我该如何解决这个问题吗?

void encrypt(std::string &e);
int main() {
    string nameAttempt;
    cout << "Enter your name to be Encrypted: ";
         cin >> nameAttempt;

    cout << "Original string is: " << nameAttempt << endl;
         encrypt( nameAttempt );
    cout << "Encrypted string is: " << nameAttempt << endl;

system("pause");
return 0;
}

void encrypt (std::string &e) {
     const char* tempCharArray = e.c_str();
     for( int i=0; i<e.size(); ++i )
     e[i] = tempCharArray[i]+11;  
     } // 

假设您要:

    将小写字母
  • 替换为小写字母
  • 将大写字母
  • 替换为大写字母
  • 保留空格和任何其他非字母字符

void encrypt (std::string &e)
{
    int size = e.size();
    for (int i=0; i<size; i++)
    {
        char c = e[i];
        if (('A' <= c && c <= 'Z'-11) || ('a' <= c && c <= 'z'-11))
            e[i] = c+11;
        else if ('Z'-11 < c && c <= 'Z')
            e[i] = c+11-'Z'+'A';
        else if ('z'-11 < c && c <= 'z')
            e[i] = c+11-'z'+'a';
    }
}

你可以做这样的事情:

char _character='X';
int _value=static_cast<int>(_character);
if(_value!=32)//not space
{
    int _newValue=((_value+11)%90);
    (_newValue<65)?_newValue+=65:_newValue+=0;
    char _newCharacter=static_cast<char>(_newValue);
}