【问题标题】:Handling symbols in a string while encrypting加密时处理字符串中的符号
【发布时间】:2017-11-21 05:42:21
【问题描述】:

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

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

密码说明:

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

}

【问题讨论】:

  • 你有多确定这个算法可以处理非 alpha 输入?
  • 语言不高的人可以肯定!我相信问题确实出在我创建的 letterToNum 函数中,因为我不确定如何将这些值转换为下一个字符可以用来加密并在非字母字符之后继续该过程。
  • 注意 P 的值行。 A 是 0。T 是 19。这是有道理的,因为 A 是第一个字母,T 是第 20 个字母,Z 是第 26 个字母,所以我认为它的值是 25。那么你给一个不是的字符分配什么数字t 一个字母并且仍然遵守给定的算法?您可以选择值,例如“。” = 16, '!' = 27,但这会破坏用于产生 C 值的数学:P+Key mod 26。如果任务是定义一个新算法来处理非字母输入,很酷,但如果不是,那么最好的选择是丢弃所有内容那不是一封信。请注意给定文本中没有空格。
  • 这可能是最好的解决方案,也是我试图用 letterToNum 中的 If 语句做的事情,但我认为它搞砸了生成其余加密的数学。不过不确定。我会试试的。谢谢。

标签: c++ vigenere autokey


【解决方案1】:

我不明白你的问题,但我认为答案是在每个不起作用的字符前加一个反斜杠,例如:"\;\:\,\."(其中一些可能有效,所以只能在那些不)

【讨论】:

  • 对不起,如果它有点混乱。当前,当您运行该程序时,它将为这些对应于它们在字母表中的位置(a = 0、b=1 等)生成值。问题在于密码的工作方式是它使用前面的字符位置来生成加密文本,如我在原始帖子中链接的图片所示。我的问题是试图找到一种方法来加密并能够解密这些,而不会像现在尝试将“攻击:”这样的字符串作为字符串时发生的事情那样乱扔扳手。它生成一个值,但没有用。
  • 好吧,但我建议你不要包含它们
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 2022-01-10
    • 2010-10-03
    • 1970-01-01
    相关资源
    最近更新 更多