Users Online

· Guests Online: 116

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C++ Program to Implement the Hill Cipher

C++ Program to Implement the Hill Cipher

 

 

This is a C++ Program to implement hill cipher. In classical cryptography, the Hill cipher is a polygraphic substitution cipher based on linear algebra. Invented by Lester S. Hill in 1929, it was the first polygraphic cipher in which it was practical (though barely) to operate on more than three symbols at once. The following discussion assumes an elementary knowledge of matrices.

 

Here is source code of the C++ Program to Implement the Hill Cypher. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. int check(int x)
  7. {
  8.     if (x % 3 == 0)
  9.         return 0;
  10.  
  11.     int a = x / 3;
  12.     int b = 3 * (a + 1);
  13.     int c = b - x;
  14.  
  15.     return c;
  16. }
  17.  
  18. int main(int argc, char **argv)
  19. {
  20.     int l, i, j;
  21.     int temp1;
  22.     int k[3][3];
  23.     int p[3][1];
  24.     int c[3][1];
  25.     char ch;
  26.     cout
  27.             << "\nThis cipher has a key of length 9. ie. a 3*3 matrix.\nEnter the 9 character key. ";
  28.  
  29.     for (i = 0; i < 3; ++i)
  30.     {
  31.         for (j = 0; j < 3; ++j)
  32.         {
  33.             scanf("%c", &ch);
  34.             if (65 <= ch && ch <= 91)
  35.                 k[i][j] = (int) ch % 65;
  36.             else
  37.                 k[i][j] = (int) ch % 97;
  38.         }
  39.     }
  40.     for (i = 0; i < 3; ++i)
  41.     {
  42.         for (j = 0; j < 3; ++j)
  43.         {
  44.             cout << k[i][j] << "  ";
  45.         }
  46.         cout << endl;
  47.     }
  48.     cout << "\nEnter the length of string to be encoded(without spaces). ";
  49.     cin >> l;
  50.     temp1 = check(l);
  51.     if (temp1 > 0)
  52.         cout << "You have to enter " << temp1 << " bogus characters.";
  53.  
  54.     char pi[l + temp1];
  55.     cout << "\nEnter the string. ";
  56.     for (i = -1; i < l + temp1; ++i)
  57.     {
  58.         cin >> pi[i];
  59.     }
  60.     int temp2 = l;
  61.     int n = (l + temp1) / 3;
  62.     int temp3;
  63.     int flag = 0;
  64.     int count;
  65.     cout << "\n\nThe encoded cipher is : ";
  66.  
  67.     while (n > 0)
  68.     {
  69.         count = 0;
  70.         for (i = flag; i < flag + 3; ++i)
  71.         {
  72.             if (65 <= pi[i] && pi[i] <= 91)
  73.                 temp3 = (int) pi[i] % 65;
  74.             else
  75.                 temp3 = (int) pi[i] % 97;
  76.  
  77.             p[count][0] = temp3;
  78.             count = count + 1;
  79.         }
  80.  
  81.         int k1;
  82.         for (i = 0; i < 3; ++i)
  83.             c[i][0] = 0;
  84.  
  85.         for (i = 0; i < 3; ++i)
  86.         {
  87.             for (j = 0; j < 1; ++j)
  88.             {
  89.                 for (k1 = 0; k1 < 3; ++k1)
  90.                     c[i][j] += k[i][k1] * p[k1][j];
  91.             }
  92.         }
  93.         for (i = 0; i < 3; ++i)
  94.         {
  95.             c[i][0] = c[i][0] % 26;
  96.             printf("%c ", (char) (c[i][0] + 65));
  97.         }
  98.         n = n - 1;
  99.         flag = flag + 3;
  100.     }
  101. }

Output:

$ g++ HillCipher.cpp
$ a.out
 
 
This cipher has a key of length 9. ie. a 3*3 matrix.
Enter the 9 character key. DharHingu
3  7  0  
17  7  8  
13  6  20  
 
Enter the length of string to be encoded(without spaces). 10
You have to enter 2 bogus characters.
Enter the string. Sanfoundry
 
The encoded cipher is : N B W A O Q Y Y X X D O 
------------------
(program exited with code: 0)
Press return to continue

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.74 seconds
10,268,507 unique visits