如何使其小写和大写

how to make this lowercase and uppercase

本文关键字:何使其      更新时间:2023-10-16

我正在制作一个基于Vigenere密码的加密程序。

我被大小写部分卡住了。

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

"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语句我需要帮助。

以下是REDDIT的链接,该链接指向GITHUB项目:https://www.reddit.com/r/CodingHelp/comments/91c3tv/need_some_help_on_vigenere_cipher_program/

当我阅读Vigenere密码时,您只能使用一个26x26表进行编码。(至少如维基百科页面所述(。这意味着您无法在不匹配大小写的情况下将大写键与小写字符混合,反之亦然。

你可以自由地添加到密码中,以扩展"矩形表、Vigenère正方形或Vignère表",使其包含小写字符,从而生成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 */
}
}

示例使用/输出

使用维基百科页面上提供的示例,并强制大小写一致性,您可以对大写和小写字符进行编码和解码(这与原始字符串的保留大写/小写的密码相同(

$ 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

我们用一个例子来遍历ur代码。让我们取文本"a"。根据我找到的ascii表,它的值为97。假设加密[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". 

您必须背靠背地使用两组字母(上下(来形成更长版本的字母表,并在此基础上进行抽取。或者简化它,只需为你的密码使用一个大小写,它也同样有效。在解析逻辑之前,您只需要将字母设置为"toUpper"或toLower。