Techknow Study

transposition cipher method.

1:08:00 AM vikas 0 Comments Category :



                  Encrypt and decrypt a plain text using
                 
transposition cipher method.




Transposition ciphers are rarely encountered nowadays. They differ from both code systems and substitution ciphers; in a transposition cipher the letters of the plaintext are shifted about to form the cryptogram. This can be done in a number of ways and some systems exist where even whole words are transposed, rather than individual letters. To encrypt Chinese, for instance, one can use a transposition cipher operating on the individual signs of written Chinese (using a substitution cipher for a language like Chinese would be awkward if not impossible).


METHOD OF ENCRYPTION-:  One of the easiest ways to achieve transposition is the Single Columnar Transposition Cipher. To use it, one needs a keyword or phrase, whose letters are numbered according to their presence in the alphabet. The keyword Ozymandias is numbered in the following way:
 
O  Z  Y  M  A  N  D  I  A  S
7 10  9  5  1  6  3  4  2  8
That is, the first occurance of the letter A is numbered 1, the second 2. There are no B:s or C:s so the next letter to be numbered are the D followed by I, and so on.
 Next the plaintext is written in rows under the numbered keyword, one letter under each letter of the keyword. Let's say that the plaintext to be encrypted is Company has reached primary goal. It will look like this:


O  Z  Y  M  A  N  D  I  A  S
7 10  9  5  1  6  3  4  2  8
c  o  m  p  a  n  y  h  a  s
r  e  a  c  h  e  d  p  r  i
m  a  r  y  g  o  a  l      
Now the letters of the plaintext are copied down by reading them off columnwise in the order stated by the enumeration of the keyword. The result is the finished cryptogram, which - of course - are put into groups of five letters, like this:
 
AHGAR YDAHP LPCYN EOCRM SIMAR OEA
To decrypt a received message enciphered by this method, one first must calculate the number of letters present in the cryptogram. This is done to see how many letters there originally were in the last row. As can be seen above, the two last columns - the ones numbered 2 and 8 - only contains two letters and this is important. Now the cryptogram above contains 28 letters and as a legitimate user of the crypto system, one knows that the keyword is ten letters wide. Therefore the last row must consist of eight letters only, the two final positions being empty. Keeping that in mind - or better still, marking the two final position of row three in some way to indicate that they shouldn't be used - one numbers the keyword letters (just as when encrypting) and then start by writing the first three letters of the cryptogram under keyword letter number one, thus:  
 
 

O  Z  Y  M  A  N  D  I  A  S
7 10  9  5  1  6  3  4  2  8
.  .  .  .  a  .  .  .  .  .
.  .  .  .  h  .  .  .  .  .
.  .  .  .  g  .  .  .  *  *
Next comes column number two. Since the last position in column two is marked by a star and shouldn't be used, one only writes the next two letters, instead of three. Continue in the same way by writing the next three letters under keyword letter number three, and so on up to keyword letter eight, it will look like this:
 O  Z  Y  M  A  N  D  I  A  S
7 10  9  5  1  6  3  4  2  8
c  .  .  p  a  n  y  h  a  .
r  .  .  c  h  e  d  p  r  .
m  .  .  y  g  o  a  l  *  *
Now column eight follows, and there only two letters should be written as stated above (the position marked by a star being left empty). This leaves six letters of the cryptogram, and these - of course - are written in column nine and ten, and then the cleartext can be read in the normal way, row by row.
 Usually when employing a transposition cipher like the above, one adds dummy letters to make the final group five letters long if it isn't already full. It is important to do this before transposing the letters, otherwise the receiver can't calculate the columns that haven't a full number of letters if the last row isn't complete. In some cases the last row is always made complete by adding dummy letters, but that reduces the security of the cipher and isn't recommended (now, this cipher is quite easy to break anyway...).



Program-:


#include <iostream>
void main(void)
    {
          char strOriginalIput[100], strPass[35], strENCR[100], outpass[100];
          int istrLen = 0;
          int iArray[20] =
          {
          1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
        };
    cout << "\n Enter String(without space) : ";
    cin >> strOriginalIput;
    istrLen = strlen(strOriginalIput);
    strOriginalIput[istrLen + 1] = '\0';
    cout << "\n Enter Crypt Pass: ";
    cin >> strPass;
    int iLen = strlen(strPass);
    strPass[iLen + 1] = '\0';
    strcpy(outpass, strPass);
    outpass[iLen + 1] = '\0';
    int cnt = 0;
    //cout << strOriginalIput << "\n";
    for (int i = 0; i < iLen - 1; i++)
                   {
        for (int j = 0; j < iLen - 1 - i; j++)
                             {
                             if (strPass[j + 1] < strPass[j])
                                      {
                /* compare the two neighbors */
                char tmp = strPass[j];       /* swap a[j] and a[j+1]   */
                strPass[j] = strPass[j + 1];
                strPass[j + 1] = tmp;
                int t = iArray[j];
                iArray[j] = iArray[j + 1];
                iArray[j + 1] = t;
                }
                             }
                   }
          cnt = 0;
    for (int z = 0; z < iLen; z++)
                   {
        for (int x = 0; x <= iLen; x++)
                             {
            if ((iArray[z] + iLen * x) <= istrLen)
                                      {
                strENCR[cnt++] = strOriginalIput[(iArray[z] + iLen * x) - 1];
                }
            }
        }
          strENCR[istrLen] = '\0';    //cout << strENCR << "\n\n"         ;
    // Output
          int nl = 1;
    for (i = 0; i < iLen; i++)
                   {
                   cout << outpass[i] << " ";
        cout << "\n-------------------------------";
        cout << "\n";
        for (i = 0; i < istrLen; i++)
                             {
            if (i == iLen * nl)
                                      {
                cout << "\n" << strOriginalIput[i] << " ";
                nl++;
                }
            else
                                      cout << strOriginalIput[i] << " ";
            }
        cout << "\n\n" << "Encrypted String : " << strENCR;
        // Encryption is over, now going for decryption
        cout << "\n";
        char strtmp[100];
        cnt = 0;
        for (z = 0; z < iLen; z++)
                             {
            for (int x = 0; x <= iLen; x++)
                                      {
                if ((iArray[z] + iLen * x) <= (istrLen))
                                                strtmp[iArray[z] + (iLen * x) - 1] = strENCR[cnt++];
                }
            }
        strtmp[istrLen] = '\0';
        cout << "Decrypted String :" << strtmp << "\n\n";
        }
    }

RELATED POSTS

0 comments