【发布时间】:2021-11-27 16:25:55
【问题描述】:
我正在编写一个替代密码程序,它接收用户输入的字符串,然后使用只读数组对其进行密码。
const int ARRAY_SIZE = 26; //10
const char cipher[ARRAY_SIZE] = {'B', 'T', 'N', 'M', 'X', 'W', 'E', 'V', 'L', 'K', 'P', 'Q', 'C', 'R', 'J', 'Y', 'Z', 'D', 'A', 'F', 'H', 'I', 'G', 'S', 'U', 'O'};
如您所见,这是我正在使用的密码。 //10 只是那个字符的索引,所以我可以更容易地跟踪它。
这是我的编码函数,它根据我的密码对用户输入的字符串进行编码。这可行,并且我在代码块的注释中突出显示了逻辑。
例如'Hello'变成'VXQQJ'。
string encodeText(string plaintext, const char ciph[])
{
for (int i = 0; i <= plaintext.length(); ++i)
{
plaintext[i] = toupper(plaintext[i]);
if ((plaintext[i] >= ('A')) && (plaintext[i] <= ('Z')))
plaintext[i] = ciph[plaintext[i] - ('A')];
}
return plaintext;
/* H E L L O (NORMAL ASCII)
* V X Q Q J (OUR ENCODING)
* 0 1 2 3 4
*
* 72(H) = ciph[72 - 65] = ciph[7] ->V
* 69(E) = ciph[69 - 65] = ciph[4] ->X
* 76(L) = ciph[76 - 65] = ciph[11] ->Q
* 76(L) = ciph[76 - 65] = ciph[11] ->Q
* 79(O) = ciph[79 - 65] = ciph[14] ->J
*/
}
但是,我无法解码 VXQQJ,例如,回到 HELLO。 我试过写这个函数,它是encodeText函数的反函数
string decodeText(string encodedText, const char ciph[])
{
for (int i = 0; i <= encodedText.length(); ++i)
{
encodedText[i] = toupper(encodedText[i]);
if ((encodedText[i] >= 65) && (encodedText[i] <= 90))
encodedText[i] = ciph[encodedText[i] /* NO CLUE */];
}
string decodedPlaintext = encodedText;
return decodedPlaintext;
}
我尝试找出与 encodeText 类似的模式,但是它不起作用,我需要从我的密码中减去任意值以获得正确的索引。我还尝试实现一个解密 const 数组,它存储所有字符 'A' - 'Z' 并以某种方式使用它来解码它,但这也不正确。
这是我的整个程序,方便参考:
#include <iostream>
using namespace std;
#define endl "\n";
/*
* Substitution cipher problem
* All messages are made of uppercase, lowercase letters and punctuation.
* The original message is called the 'plaintext'
* The cipher is created by substituting each letter with another letter.
* Hard code a const array of 26 char elements for the cipher.
* Read a plaintext message and output ciphertext.
*
* Then we decipher the encoded message again to check it's validity.
*/
//B T N M X W E V L K P Q C R J Y Z D A F H I G S U O from https://www.dcode.fr/deranged-alphabet-generator
string encodeText(string plaintext, const char cyph[])
{
for (int i = 0; i <= plaintext.length(); ++i)
{
plaintext[i] = toupper(plaintext[i]);
if ((plaintext[i] >= ('A')) && (plaintext[i] <= ('Z')))
plaintext[i] = cyph[plaintext[i] - ('A')];
}
return plaintext;
/* H E L L O (NORMAL ASCII)
* V X Q Q J (OUR ENCODING)
* 0 1 2 3 4
*
* 72(H) = ciph[72 - 65] = ciph[7] ->V
* 69(E) = ciph[69 - 65] = ciph[4] ->X
* 76(L) = ciph[76 - 65] = ciph[11] ->Q
* 76(L) = ciph[76 - 65] = ciph[11] ->Q
* 79(O) = ciph[79 - 65] = ciph[14] ->J
*/
}
string decodeText(string encodedText, const char cipher[])
{
for (int i = 0; i <= encodedText.length(); ++i)
{
encodedText[i] = toupper(encodedText[i]);
if ((encodedText[i] >= 65) && (encodedText[i] <= 90))
encodedText[i] = cipher[encodedText[i] /* NO CLUE */];
}
string decodedPlaintext = encodedText;
return decodedPlaintext;
}
int main(void)
{
const int ARRAY_SIZE = 26; //10
const char cipher[ARRAY_SIZE] = {'B', 'T', 'N', 'M', 'X', 'W', 'E', 'V', 'L', 'K', 'P', 'Q', 'C', 'R', 'J', 'Y', 'Z', 'D', 'A', 'F', 'H', 'I', 'G', 'S', 'U', 'O'};
// A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
cout << "Enter plaintext string" << endl;
string plaintext;
getline(cin, plaintext);
string encoded = encodeText(plaintext, cipher);
cout << "The encoded text is:" << ' ' << encoded << endl;
cout << "Please enter the encoded text to verify if it is correct or not" << endl;
string encodedText;
getline(cin, encodedText);
string decoded = decodeText(encodedText, cipher);
cout << "The decoded text is" << ' ' << decoded << endl;
return 0;
}
【问题讨论】:
-
你没有考虑环绕。这可能是我的阅读错误,但我也看到
cipher和cyph是已定义的东西,但我没有看到ciph。 -
哎呀,这是我的许多试验和错误之一中的一个小错误,是的,它应该是密码
标签: c++ arrays string encryption