title: Hill Cipher Encrypt
body: <p>So I am trying to use the hill cipher to encrypt my 3x3 matrix with a given key. It works correctly for the first value outputting n which it should, but then after that value I get large values and it never takes the mod of them. I added the cout statements to help me debug and see what's going wrong, but I still can't fix it. Also the second mod 26 is there because when I didn't have it there I was getting negative 13 instead of positive 13. This is a homework program, our key was given to us as numbers in case that is of any importance. </p>
  
  <p><a href="https://i.stack.imgur.com/PDNkO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PDNkO.png" alt="enter image description here"></a></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;stdio.h&gt;
  #include &lt;ctype.h&gt;
  
  using std::endl;
  using std::string;
  
  
  void inverse_matrix();
  string encryption(string x);
  
  int main()
  {
  std::string one = "paymoremoney";
  // inverse_matrix();
  
  encryption(one);
  
  system("pause");
  return 0;
  }
  
  
  
  string encryption(string x)
  {
  int encrypted[4][4];
  int key[3][3];
  key[0][0] = 4;
  key[0][1] = 9;
  key[0][2] = 15;
  key[1][0] = 15;
  key[1][1] = 17;
  key[1][2] = 6;
  key[2][0] = 24;
  key[2][1] = 0;
  key[2][2] = 17;
  
  int test = 0;
  char str[] = "";
  char c;
  
  
  int length = (int)x.length();
  for (int i = 0; i &lt; length; i++)
  {
      x[i] = tolower(x[i]);
  
  }
  /*
  
  
  while (str[test])
  {
      c = str[test];
      putchar(tolower(c));
      test++;
  
  }
  */
  
  
  int encrypt[4][4];
  encrypt[0][0] = x[0];
  encrypt[0][1] = x[1];
  encrypt[0][2] = x[2];
  encrypt[1][0] = x[3];
  encrypt[1][1] = x[4];
  encrypt[1][2] = x[5];
  encrypt[2][0] = x[6];
  encrypt[2][1] = x[7];
  encrypt[2][2] = x[8];
  encrypt[3][0] = x[9];
  encrypt[3][1] = x[10];
  encrypt[3][2] = x[11];
  
  
  
  encrypted[0][0] = (key[0][0] * encrypt[0][0]) + (key[1][0] * encrypt[0][1])              + (key[3][0] * encrypt[0][2]) % 26;
  encrypted[0][0] %= 26;
  
  
  
  encrypted[0][1] = (key[0][1] * encrypt[0][0]) + (key[1][1] * encrypt[0][1]) + (key[2][1] * encrypt[0][2])%26;
  encrypted[0][0] %= 26;
  
  encrypted[0][2] = (key[0][2] * encrypt[0][0]) + (key[1][2] * encrypt[0][1]) + (key[2][2] * encrypt[0][2]) % 26;
  encrypted[0][0] %= 26;
  
  std::cout &lt;&lt; encrypted[0][0];
  std::cout &lt;&lt; endl;
  std::cout &lt;&lt; encrypted[0][1];
  std::cout &lt;&lt; endl;
  std::cout &lt;&lt; encrypted[0][2];
  
  
  std::cout &lt;&lt; endl;
  </code></pre>
  
  <p>}</p>