加密时处理字符串中的符号

时间:2017-11-21 05:42:21

标签: c++ vigenere autokey

我正在尝试弄清楚如何能够实现这个自动密钥密码,并认为我已经完成了大部分工作。密码应该使用字母中字符位置的子键样式系统。

目前我仍然坚持如何处理一些符号" ;:"。当它们作为加密或解密字符串的一部分输入时,并且由于我不熟悉该语言而不确定如何处理它。任何指导或指导都会很精彩。提供了代码以及密码如何在下面工作的示例。

密码描述: enter image description here

#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;
    }

}

2 个答案:

答案 0 :(得分:0)

我不明白你的问题,但我认为答案是在每个不起作用的角色之前做一个反斜杠,如下所示:"\;\:\,\."(其中一些可能有效,所以只能这样做在没有的那些)

答案 1 :(得分:0)

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

if (isPunct(inputText[x]) {
    //do whatever it is you need to do
}