C ++中随机文本的解密

时间:2017-02-09 10:47:06

标签: visual-c++

您好我在C ++中查询DECRYPTION随机文本,我已加密但在解密循环中我被卡住了。我找不到解决方案。

编写一个程序,为文本消息生成随机密码。字母表中的每个字母都被替换为 另一封信是随机的。程序必须能够使用生成的密码对消息进行编码和解码。 该计划必须具备以下功能:

  1. 该程序通过用其他字母之一替换字母表中的每个拉丁字母来生成密码, 基于伪随机数发生器。两个不同的字母不能用同一个字母代替。
  2. 程序从标准输入中读取英文消息,并使用随机密码对其进行编码。 结果将打印在标准输出中。
  3. 程序从标准输入读取加密消息,并使用相同的密码对其进行解码。 同样,结果将打印在标准输出中。
  4. 代码:

    #include <iostream> //Basic standard input output library
    #include <string> //header for using of data type string
    #include <cstdlib> //Library header file, sand(), rand() prototypes
    #include <ctime> //Library header file for measuring time, time() prototype
    #include <cctype> //for character handling and classify and transform       individual characters
    
        using namespace std;
    
    int main() {
        srand(time(0)); //seed for rand, the program time (at execution of program) can make very nice seed values, because no two programs executions will occur at the same instant of the computers clock.
        static char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //static array to hold upper case letters
        static char alphabetlow[] = "abcdefghijklmnopqrstuvwxyz"; //static array to hold lower case letter
        const int LENGTH = sizeof(alphabet, alphabetlow) - 1; // length of the array
        int r;
        char temp;
        char tempb;
        for (unsigned int i = 0; i < LENGTH; i++) //loop which shuffle the array
        {
            r = rand() % LENGTH; //generate a sequance of pseudo-random numbers
            temp = alphabet[i]; //temp gets the initial value of the array element
            alphabet[i] = alphabet[r]; //randomized letters are copied into the ordered alphabet(now scrambled)
            alphabet[r] = temp; //ordered letters are copied into the scrambled alphabet
    
            tempb = alphabetlow[i]; //shuffle second array
            alphabetlow[i] = alphabetlow[r];
            alphabetlow[r] = tempb;
        }
    
        string text, textb;
        getline(cin, text);
    
        for (unsigned int i = 0; i < text.length(); i++) //loop to encrypt
        {
            if (isalpha(text[i])) //encrypt only input text which is latin letters
            {
                if (islower(text[i])) //checks if input text is lowercase letters
                {
                    text[i] = alphabetlow[text[i] - 'a']; //scrambles lowercase letters
                } else {
                    text[i] = alphabet[text[i] - 'A']; //scrambles uppercase letters
                }
            }
        }
        cout << "Encrypted: " << text << endl;
    
        getline(cin, textb);
    
        for (unsigned int i = 0; i < textb.length(); i++) //Loop for decoding
        {
            //Here i need help for decryption
        }
    
        cout << "Decrypted: " << textb << endl;
        system("Pause");
        return 0;
    }
    

2 个答案:

答案 0 :(得分:0)

#include <iostream>//Basic standard input output library
using namespace std;
#include "EnDyc.h" // Include the header file of class

//EnDyc:: is used to access the members of this particular class

char EnDyc::alphabet[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";//static array to upper case letters
char EnDyc::alphabetlow[27] = "abcdefghijklmnopqrstuvwxyz";//static array to hold lower case letter
int EnDyc::LENGTH = sizeof(alphabet, alphabetlow) - 1;// length of the array};
void EnDyc::ran()
{
    srand(time(0));//seed for rand, the program time (at execution of program) can make very nice seed values, because no two programs executions will occur at the same instant of the computers clock.
    int r;
    char temp;
    char tempb;
    for (unsigned int i = 0; i < LENGTH; i++) //loop which shuffle the array
    {
        r = rand() % LENGTH; //generate a sequance of pseudo-random numbers
        temp = alphabet[i];//temp gets the initial value of the array element
        alphabet[i] = alphabet[r];//randomized letters are copied into the ordered alphabet(now scrambled)
        alphabet[r] = temp;//ordered letters are copied into the scrambled alphabet

        tempb = alphabetlow[i];//shuffle second array
        alphabetlow[i] = alphabetlow[r];
        alphabetlow[r] = tempb;
    }

}




 string EnDyc::encyrp(string text)
{
    for (unsigned int i = 0; i < text.length(); i++)//loop to encrypt
    {
        if (isalpha(text[i]))//encrypt only input text which is latin letters
        {
            if (islower(text[i]))//checks if input text is lowercase letters
            {
                text[i] = alphabetlow[text[i] - 'a'];//scrambles lowercase letters
            }
            else
            {
                text[i] = alphabet[text[i] - 'A'];//scrambles uppercase letters
            }
        }
    }
    return text;// return the encrypted message to the main func
}



string EnDyc::decryp(string textb)
{
    //Dycription
    for (int i = 0; i < textb.length(); i++)
    {
        if (isalpha(textb[i]))//encrypt only input text which is latin letters
        {
            if (islower(textb[i]))//checks if input text is lowercase letters
            {
                for (int j = 0; j < LENGTH; j++)//Loop for the Alphabets 0-25 in alphabet array
                {
                    if (textb[i] == alphabetlow[j])// check the location of input character
                    {
                        int ind = j + (int)'a';// Add the ASCI to the location of input character
                        textb[i] = (char)ind;//Convert the interger ASCI to character ASCI
                        break;// break the current execution
                    }
                }
                //scrambles lowercase letters
            }
            else
            {
                for (int j = 0; j < LENGTH; j++)//Loop for the Alphabets 0-25 in alphabet array
                {
                    if (textb[i] == alphabet[j])// check the location of input character
                    {
                        int ind = j + (int)'A';// Add the ASCI to the location of input character
                        textb[i] = (char)ind;//Convert the interger ASCI to character ASCI
                        break;// break the current execution
                    }
                }
                //scrambles uppercase letters
            }
        }
    }
    return textb; // return the decrypted message to the main func

}

答案 1 :(得分:0)

#ifndef ENDYC_H

#include <iostream>//Basic standard input output library
#include <string>//header for using of data type string
#include <cstdlib>//Library header file, sand(), rand() prototypes
#include <ctime>//Library header file for measuring time, time() prototype
#include <cctype>//for character handling and classify and transform individual characters
using namespace std;
class EnDyc{// CLass for encryption and dycription
public:
static char alphabet[27];// An array to store lakin alphabets. In upper case
static char alphabetlow[27];// An array to store lakin alphabets. In lower case
static int LENGTH;// lenth of array for to ilplement loop for random arrangements of characters
static void ran();// method to  arrange alphabetic order randomly
static string encyrp(static string text);// method for the Encryption of message
static string decryp(static string textb);// method for the dycryption of message
};
#endif // !1