C++ 修改程序以加密大写和小写输入

C++ 修改程序以加密大写和小写输入,c++,encryption,random,shuffle,alphabet,C++,Encryption,Random,Shuffle,Alphabet,我必须为一个随机密码问题编写代码。我已经完成了,但是程序只转换大写或小写字母(取决于我选择的内容)作为输入。我应该更改什么以便程序转换大写和小写字母 代码: srand(时间(0))//兰德的种子() 静态字符字母表[]=“ABCDEFGHIJKLMNOPQRSTUVWXYZ”; 字符串alph=“abcdefghijklmnopqrstuvxyz”; 常量int LENGTH=sizeof(字母表)-1; INTR; 焦炭温度; for(unsigned int i=0;i编辑:添加完整代码(

我必须为一个随机密码问题编写代码。我已经完成了,但是程序只转换大写或小写字母(取决于我选择的内容)作为输入。我应该更改什么以便程序转换大写和小写字母

代码:

srand(时间(0))//兰德的种子()
静态字符字母表[]=“ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
字符串alph=“abcdefghijklmnopqrstuvxyz”;
常量int LENGTH=sizeof(字母表)-1;
INTR;
焦炭温度;

for(unsigned int i=0;i编辑:添加完整代码(也用于此替换密码的解密)

添加第二个小写字母,并在同一循环中对其进行修改,该循环将洗牌第一个字母的数组,如:

temp=alphabet_of_lowerCase[i];         
alphabet_of_lowerCase[i] = alphabet_of_lowerCase[r];  
alphabet_of_lowerCase[r]=temp; 
现在,在您的加密方案中,只需使用
islower
is
isupper
函数检查符号是小写还是大写字母,并在
isalpha
中相应地使用所需的字母数组,如果分支:

if (islower()) {
   text[i]= alphabet_of_lowerCase[text[i] - 'a'];
}
else
   // your previous code
完整代码:

static char alphabet[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static char text[] = "TEST";

void shuffleAlphabet() {

    srand(time(0));     //seed for rand()

    const int LENGTH = sizeof(alphabet)-1;

    int r;
    char temp;
    for (unsigned int i=0; i<LENGTH; i++)   //loop which shuffles the array
    {
        r=rand() % LENGTH;
        temp=alphabet[i];
        alphabet[i] = alphabet[r];
        alphabet[r]=temp;
    }

}

void cipher(int decrypt) {

    for (unsigned int i=0; i<strlen(text); i++)     //loop to encrypt
    {
        if (isalpha(text[i]))
        {
            if (!decrypt)
                text[i]=alphabet[text[i] - 'A'];    //index alphabet with the value of text[i], adjusted to be in the range 0-25
            else {
                int charPos = strchr(alphabet, text[i]) - alphabet; // calculate character position in cipher alphabet
                text[i]='A' + charPos; // and advance forward in standard alphabet by this position
            }
        }
    }

}

int main()
{
    printf("Text: %s\n", text);
    shuffleAlphabet();
    printf("Cipher alphabet: %s\n", alphabet);
    cipher(0);
    printf("Encrypted: %s\n", text);
    cipher(1);
    printf("Decrypted: %s\n", text);

    return 0;
}
static char alphabet[]=“abcdefghijklmnopqrstuvxyz”;
静态字符文本[]=“测试”;
void shufflealpabet(){
srand(时间(0));//rand()的种子
常量int LENGTH=sizeof(字母表)-1;
INTR;
焦炭温度;

对于(unsigned int i=0;i您可以使用
std::transform()
算法函数,使用lambda返回大写或小写字符转换

#include <algorithm>
#include <cctype>
#include <string>
//...
std::string text;
//...
std::transform(text.begin(), text.end(), text.begin(), [](char c) 
  { return isupper(c)?tolower(c):toupper(c); });
#包括


std::transform
函数之所以命名为
transform
,是有原因的,上面就是一个例子。我们取字符串,对其进行迭代,然后将每个字符从低到高进行“转换”(反之亦然)。lambda函数是应用于字符串中每个字符的规则。

ah,这就解决了它!谢谢你,希望我早点想到它。祝你愉快!还有,是否可以使用此随机密码解密消息?它让我困惑,因为我没有密码的密钥,所以我不知道解密代码应该是什么样子的此程序。我已更新我的答案以包含解密部分-请查看:-)
#include <algorithm>
#include <cctype>
#include <string>
//...
std::string text;
//...
std::transform(text.begin(), text.end(), text.begin(), [](char c) 
  { return isupper(c)?tolower(c):toupper(c); });