0

我正在尝试弄清楚如何实现这个自动密钥密码,并认为我已经解决了大部分问题。密码应该使用使用字母表中字符位置的子密钥样式系统。

目前我被困在如何处理一些符号“;:,”上。当它们作为加密或解密字符串的一部分输入并且由于我是该语言的新手而不确定如何处理它时。任何指导或方向都会很棒。在下面提供了代码和密码应如何工作的示例。

密码说明: 在此处输入图像描述

#include <iostream>
#include <string>
#include <assert.h>

using namespace std;

//Declares
char autokeyE(int, int);
char autokeyD(char);
char numToLetter(int);
int letterToNum(char);

int main()
{
    //Declares
    string inputText, finalText;
    int firstAlpha = 0;
    int key = 0;
    int option = 0;


    //First Values
    do
    {
        cout << "What operation would you like to do? Press '1' for Encrypt and '2' for Decrypt." << endl ;
        cin >> option;

        if (option == 1)
        {
            cout << "Please input your plain text to encrypt." << endl ;
            cin >> inputText;
            cout << "Please input your key to encrypt with." << endl;
            cin >> key;

            string finalText = "";

            firstAlpha = letterToNum(inputText[0]);
            finalText = numToLetter((firstAlpha + key) %26);
            //inputText[0] = finalText[0];

            for (int x = 1; x < inputText.length(); x++)
            {

                finalText += autokeyE(letterToNum(inputText[x-1]), letterToNum(inputText[x]));
            }
            cout << finalText << endl;
        }

        if (option == 2)
        {
            cout << "Please input your encrypted text to decrypt." << endl ;
            cin >> inputText;
            string finalText = "";

            firstAlpha = letterToNum(inputText[0]);
            finalText = numToLetter((firstAlpha + key) %26);

            for (int x = 1; x < inputText.length(); x++)
            {
                //cout << inputText[x]; Testing output
                finalText += autokeyD(inputText[x]);
            }
            cout << finalText << endl;
        }
    }
    while (!inputText.length() == 0);
}

char autokeyE(int c, int n)
{
    cout << "Keystream: " << n << " | Current Subkey: " << c << endl;

        int result = 0;
        //c = toupper(c);
        result = ((c + n) +26 )%26;
        cout << "C as a numtoletter: " << numToLetter(result) << " Result: " << result << endl;
        return numToLetter(result); 
    return c;
}

char autokeyD(char c)
{
    //Decrypting Shift -1
    if (isalpha(c))
    {
        c = toupper(c); 
        c = (((c - 65) - 1) % 26) + 65;
    }
    return c;
}

char numToLetter(int n)
{
    assert(n >= 1 && n <= 32);
    return "ABCDEFGHIJKLMNOPQRSTUVWXYZ ;:,."[n];  
}

int letterToNum(char n)
{
    if (isalpha(n))
    {
        n = toupper(n);
        return(int) n - 65; 
    }
    else 
    {
        cout << "REUTRNING A NON ALPHA CHARACTER AS: " << n << endl;
        return(int) n -30;
    }

}
4

2 回答 2

0

我不明白你的问题,但我认为答案是在每个不起作用的字符之前做一个反斜杠,就像这样:("\;\:\,\."其中一些可能起作用,所以只对那些不起作用的字符执行)

于 2017-11-21T05:47:14.313 回答
0

您可以使用 C++ 处理符号isPunct,例如:

if (isPunct(inputText[x]) {
    //do whatever it is you need to do
}
于 2017-11-21T07:20:30.590 回答