如何使这个小写和大写

时间:2018-07-23 03:32:07

标签: c++ string encryption

我正在制作基于Vigenere cipher的加密程序。

我卡在小写字母和大写字母上。

例如,如果我想加密/解密文本:

"This is a STRING"

大写字母可以很好地加密/解密,但是小写字母不能正确解密。另外,我使用的密钥全部为大写。以下代码不适合大写和小写吗?

if (isupper(OriginalText[i])) {
    int x = (OriginalText[i] + key[i]) % 26;
    x = x + 'A';

    EncryptText.push_back((x));
} else if (islower(OriginalText[i])) {
    int y = (OriginalText[i] + key[i]) % 26;

    y = y + 'a';

    EncryptText.push_back((y));
}

以下是用于解密:

if (EncryptedText[i] != space[j]) {

    if (isupper(EncryptedText[i])) {
        int x = (EncryptedText[i] - key[i] + 26) % 26;
        x += 'A';

        DecryptedText.push_back((x));
    } else if (islower(EncryptedText[i])) {
        int y = (EncryptedText[i] - key[i] + 26) % 26;
        y += 'a';

        DecryptedText.push_back((y));
    }
}

编辑:为了覆盖所有方面,我尝试使用下面的代码代替上面的代码。

  • 带大写字母的大写字母
  • 带小写字母键的大写字母
  • 带大写字母的小写文本
  • 带小写字母的小写字母

我能够满足以下两个条件:

  • 带大写字母的大写字母
  • 带小写字母键的大写字母

因此,我知道一个小写的密码密钥将起作用。 现在,我需要使这些条件起作用:

  • 带大写字母的小写文本
  • 带小写字母的小写字母

这是下面的代码:

        if (EncryptedText[i] != space[j]) {

        if (isupper(EncryptedText[i]) && isupper(key[i])) {
            x = ((((EncryptedText[i] + 'A') - (key[i] - 'A')) % 26) + 'A');
            DecryptedText.push_back(x);
        }
        if (isupper(EncryptedText[i]) && islower(key[i])) {
            x = ((((EncryptedText[i] + 'A') - (key[i] - 'a')) % 26) + 'A');
            DecryptedText.push_back(x);
        }
        if (islower(EncryptedText[i]) && isupper(key[i])) {
            x = ((((EncryptedText[i] + 'a') - (key[i] + 'A')) % 26) + 'a');
            DecryptedText.push_back(x);
        }
        if (islower(EncryptedText[i]) && islower(key[i])) {
            x = ((((EncryptedText[i] + 'a') - (key[i] + 'a')) % 26) + 'a');
            DecryptedText.push_back(x);
        }

我需要最后两个if语句的帮助。

以下是与GITHUB项目有关联的REDDIT的链接: https://www.reddit.com/r/CodingHelp/comments/91c3tv/need_some_help_on_vigenere_cipher_program/

3 个答案:

答案 0 :(得分:1)

在我阅读Vigenere cipher时,您只能使用一个26x26的表格进行编码。 (至少如Wikipedia页面中所述)。这意味着在不区分大小写的情况下,您无法混用大写键和小写字符,反之亦然。

您可以随意添加密码,以扩展“表格直肠,维格纳尔广场或维格纳尔表”,使其包含小写字符,从而得到52x52,并在密码中构建偏移量体操-但随后不会Vigenere密码。

这并不意味着您不能同时处理大写和小写字符,只需要使键/字符大小写一致即可进行编码和解码。您可以通过在编码/解码过程中简单地使用tolowertoupper转换来做到这一点,例如

#include <iostream>
#include <string>

using namespace std;

int main (int argc, char **argv) {

    string  key = argc > 1 ? argv[1] : "LEMON",
            line,       /* input to encode */
            cipher,     /* encoded input  */
            decode;     /* decoded cipher */
    auto& k = key;
    size_t kdx = 0;        /* key index */

    if (!k[kdx]) {  /* validate key has at least one char */
        cerr << "invalid key.\n";
        return 1;
    }

    while (getline (cin, line)) {   /* read each line of input */
        /* encode line into cipher */
        for (auto& c : line) {      /* for each char in input */
            if (islower(c))         /* if lower, force key lower */
                cipher.push_back((tolower(k[kdx]) - 'a' + c - 'a') % 26 + 'a');
            else if (isupper (c))   /* if upper, force key upper */
                cipher.push_back((toupper(k[kdx]) - 'A' + c - 'A') % 26 + 'A');
            else {  /* otherwise -- character not supported */
                cerr << "error: unsupported char '" << c << "' removing.\n";
                continue;
            }
            kdx++;  /* increment key index */
            if (kdx == key.length())    /* if end of key, reset key index */
                kdx = 0;
        }
        /* decode cipher into decode */
        kdx = 0;                    /* reset key index */
        for (auto& c : cipher) {    /* for each char in cipher */
            if (islower (c)) {      /* if lower, force key lower */
                int off = c - tolower (k[kdx]);
                if (off >= 0)       /* if offset >= 0, mod 26 */
                    decode.push_back (off % 26 + 'a');
                else                /* if offset < 0, + 26 */
                    decode.push_back (off + 26 + 'a');
            }
            else if (isupper (c)) { /* do the same for upper case */
                int off = c - toupper (k[kdx]);
                if (off >= 0)
                    decode.push_back (off % 26 + 'A');
                else
                    decode.push_back (off + 26 + 'A');        
            }
            else {  
                cerr << "error: invalid char in cipher '" << c << "'.\n";
                return 1;
            }
            kdx++;  /* increment key index */
            if (kdx == key.length())    /* if end of key, reset key index */
                kdx = 0;
        }
        cout << "input : " << line << '\n' <<
                "key   : " << key <<  '\n' <<
                "cipher: " << cipher << '\n' <<
                "decode: " << decode << '\n';

        cipher.clear();     /* clear both cipher and decode */
        decode.clear();
        kdx = 0;            /* reset key index */
    }
}

使用/输出示例

使用Wikipedia页面上提供的示例,并强制区分大小写,您可以对大写和小写字符进行编码和解码(这是相同的密码,保留了原始字符串的大写/小写字母)

$ echo "ATTACKATDAWN" | ./bin/vigenere
input : ATTACKATDAWN
key   : LEMON
cipher: LXFOPVEFRNHR

$ echo "AttackAtDawn" | ./bin/vigenere lEMoN
input : AttackAtDawn
key   : lEMoN
cipher: LxfopvEfRnhr
decode: AttackAtDawn

$ echo "Attack At Dawn" | ./bin/vigenere lEMoN
error: unsupported char ' ' removing.
error: unsupported char ' ' removing.
input : Attack At Dawn
key   : lEMoN
cipher: LxfopvEfRnhr
decode: AttackAtDawn

答案 1 :(得分:0)

通过示例详细介绍我们的代码。让我们取文字“ a”。根据我发现的ascii表,它的值为97。 假设Encrypted [i] =='a'

我还要假设您的DecryptedText是char类型。

int y = (97 - key[i] + 26) % 26
y = y + 97;

我不知道您的钥匙[i]是什么。用您的密钥[i]进行数学运算,然后查看行为是否正常。

初学者的一种非常简单的方法是在其中插入一些打印语句

int y = (97 - key[i] + 26) % 26
std::cout << key[i] << " <-- is the key value;
y = y + 97;
std::cout << y << " <-- resulting y value". 

答案 2 :(得分:0)

您必须背对背使用两组字母(大写和小写)以形成较长版本的字母并以此为基准进行抽取。或者,也可以简化它,并为密码使用一个单独的大小写,同样有效。在解析逻辑之前,您只需将字母设为“ toUpper”或“ toLower”即可。