title: Using rand() and srand() in an encryption code
body: <p>The question wants me to program a One-Time-Small Pad cryptosystem where the message is longer than the key. the program will use srand() and rand() to generate numbers from 0 to 4 (if the key has 5 letters) and use them on the whole sentence. the srand uses the seed which is the sum of the ASCII values of the letters of the initial key. The problem now is that the sentence encrypted is not the same as the example (attached below). </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;iomanip&gt;
  #include &lt;cmath&gt;
  #include &lt;stdlib.h&gt;
  using namespace std;
  int main ()
  {
      string message, key;
      int messagevalue, keyvalue, i=0, j=0, seed=0;
  
  
      cout&lt;&lt;"Enter the message: ";
  
      getline(cin, message);
  
      cout &lt;&lt; "Enter the key: ";
  
      getline(cin, key);
  
      messagevalue = message.length();
      keyvalue = key.length();
  
      while (key[i] != 0)
      {
          i++;
          j++;
      }
  
  
      cout &lt;&lt; "The message is: " &lt;&lt; setw(20)  &lt;&lt; message &lt;&lt; endl;
  
      for (i=0; i &lt; messagevalue; i++)
      {
         seed = seed + key[i];
      }
  
      srand(seed);
  
      for(i=0; i &lt; messagevalue; i++)
      {
  
      message[i] = 65 + (message[i] + key[rand()%j]) % 26;
  
      }
  
      cout &lt;&lt;"The cipher is: " &lt;&lt; setw(21)  &lt;&lt; message &lt;&lt;endl;
  
  
      for(i=0; i &lt; messagevalue; i++)
      {
        message[i] = 65 + 26 + (message[i] - key[rand()%j]) % 26;
  
        if ((message[i] - key[i]) &gt;= 0)
        {
            message[i] = message[i] - 26;
        }
  
      }
  
      cout &lt;&lt; "The message again is: " &lt;&lt; setw(14)  &lt;&lt; message &lt;&lt; endl;
  
  
  
      return 0;
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/OJJu1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OJJu1.png" alt="enter image description here"></a></p>