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

Posted

技术标签:

【中文标题】修改程序以加密大写和小写输入【英文标题】:Modify program to encrypt both uppercase and lowercase input 【发布时间】:2015-02-02 15:29:00 【问题描述】:

我必须为随机密码问题编写代码。我已经做到了,但是程序只转换大写或小写(取决于我选择的)字母作为输入。我应该更改什么以使程序同时转换大小写字母?

代码:

   srand(time(0));     //seed for rand()
static char alphabet[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
string alph="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
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;   


string text;
getline (cin, text);
for (unsigned int i=0; i<text.length(); i++)     //loop to encrypt

    if (isalpha(text[i]))
    
        text[i]=alphabet[text[i] - 'A'];    //index alphabet with the value of text[i], adjusted to be in the range 0-25

    

【问题讨论】:

【参考方案1】:

编辑:添加完整代码(也用于解密此替换密码)

添加小写字符的第二个字母并在同一循环中修改它,该循环将第一个字母的数组打乱,如:

temp=alphabet_of_lowerCase[i];         
alphabet_of_lowerCase[i] = alphabet_of_lowerCase[r];  
alphabet_of_lowerCase[r]=temp; 

现在在您的加密方案中,只需检查符号是小写还是大写字母,islowerisupper 函数,并在 isalpha 中相应地使用所需的字母数组 if 分支:

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;

因此,通常您只需要知道用于破译加密文本的密码字母即可。其实你甚至不必知道密码字母表 解码加密文本,因为您可以使用 frequency analysis 破解几乎所有替换密码(包括 XOR 加密)。 除非 ... 替换密码中使用的密钥与原始文本的长度相同并且始终是随机的,- 在这种情况下,我们将得到牢不可破的 one-time pad。

【讨论】:

啊,解决了!谢谢,希望我早点想到。祝你有美好的一天! 另外,是否可以使用这种随机密码来解密消息?这让我很困惑,因为我没有密码密钥,所以我不知道这个程序的解密代码应该是什么样子。 我已经更新了我的答案以包括解密部分 - 看看 :-)【参考方案2】:

您可以将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); );

实时示例:http://ideone.com/PJmc1b

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

【讨论】:

以上是关于修改程序以加密大写和小写输入的主要内容,如果未能解决你的问题,请参考以下文章

从键盘输入一个小写字母输出大写程序?

恺撒加密

在 UITextView 中以大写形式显示文本并从 shouldChangeTextIn 中获取新文本,以保持原始文本和输入文本的大小写

PYTHON编写程序,功能如何把输入的字符串的大写字母变成小写字母,其中的小写字母变成大写字母?

课后作业之字符串加密

C语言编写程序,将一个字符串中的大写字母转换为对应的小写字母,小写字母转换为对应的大写字母,并统计数