Synthesis of Quadrate Polibia text in C+++



  • We need a program that encrypts the word by Quadrat Polibia. First, a number of letters are proposed to contain the key for encryption. For example, 5. We'll then introduce the word from five letters. The letters are removed from their place in the alphabet and transferred to the front. I did it. Introduce the word for encryption. But we need to encrypt the word. This is done as follows. The word for encryption compares to the same letter in the alphabet and moves five letters ago, that is to the right. Help me implement.

    Code

    void main()
    {
        char alpha[]="abcdefghijklmnopqrstuvwxyz";
        int n=strlen(alpha);
        char *alph_kl=new char[n];
        int m;
        cout<<"Enter length of key word"<<endl;
        cin>>m;
        char *w=new char[m];
        int z=n-m;
        cout<<"n="<<n<<"m="<<m<<"z="<<z<<endl;
        char *alpha_bez=new char[z];
        cout<<"Enter key word"<<endl;
        gets(w);
        bool pr;
        for (int i=0,k=0;i<n;i++)
        {
            pr=false;
            for (int j=0;j<m;j++)
                if (alpha[i]==w[j])
                {
                    pr=true;
                    break;
                }
            if(pr==false) 
            {
                alpha_bez[k]=alpha[i];          
                k++;
            }
        }
        strcpy(alpha,w);
        for (i=m;i<n;i++)
            alpha[i]=alpha_bez[i-m];
        puts(alpha);
    }
    

  • QA Engineer

    There are several ways of encrypting encryption введите сюда описание изображения

    We do everything the other way.

    Implementation

    In this case, it is convenient to keep a square in the form of a single set of symbols:

    char square[] = "wordabcefghiklmnpqstuvxyz";
    

    Encryption function:

    void encrypt(char* msg, const char* square, size_t square_side) {
        for (size_t i = 0; i < strlen(msg); ++i) {
            const size_t index = strchr_get_index(square, msg[i]);
            msg[i] = square[(index + square_side) % strlen(square)];
        }
    }
    

    Decoding function:

    void decrypt(char* msg, const char* square, size_t square_side) {
        for (size_t i = 0; i < strlen(msg); ++i) {
            /* Координата может получиться отрицательной. Это значит, что нужный
               нам символ находится в конце массива. */
            const int coord = strchr_get_index(square, msg[i]) - square_side;
            const size_t index = (coord >= 0) ? coord : strlen(square) + coord;
            msg[i] = square[index];
        }
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2