title: Vignere Cipher encryption/decryption using arrays
body: <p>I need to first encrypt and then decrypt a message using the Vigenere cypher. This is how it should work</p>
  <pre><code>example message:                                 &quot;c  a  t  s  _  d  o  g  s&quot;
  keyword &quot;rats&quot;:                                   r  a  t  s  r  a  t  s  r
  order of the letter in the message (start at a=0):2  0  19 18 () 3  14 6  18
  order of the letter in the keyword:               17 0  19 18 17 0  19 18 17
  sum of the two orders:                            19 0  38 36 17 3  39 24 35
  new letter for the message*                       t  a  m  k     d  h  y  j
  encrypted message = &quot;tamk uoyk&quot;
  </code></pre>
  <p><em>Note:</em> if the <code>sum &gt; 26</code> then we subtract <code>26</code> from the <code>sum</code> to get a cyclical alphabet. Example:</p>
  <pre><code>z + b = 25 + 1 = 26; 26 - 26 = 0 --&gt; a
  </code></pre>
  <p>I have written the methods to obtain the numerical value of the keyword, and also two methods that &quot;add&quot; or &quot;subtract&quot; individual letters and two methods that perform the caesar encoding/decoding (simply shifting the entire message by an int to the right in the alphabet, or to the left to decrypt).</p>
  <p>The piece that I really need help for is to how to create a for loop that's going to repeat the keyword the appropriate amount of times (to have the same length as the message) and proceed to the obtainKeys method to get the numerical values of the repeated key.</p>
  <p>Here is my entire program; the part I am struggling with is at the end (Q2f)</p>
  <pre class="lang-java prettyprint-override"><code>import java.util.Arrays;
  public class Cypher {
      public static void main(String[] args) {
          System.out.println(charRightShift('z', 3));
          System.out.println(charLeftShift('z', 3));
  
          String test = caesarEncode(&quot;cats and dogs&quot;, 5);
          System.out.println(test);
          System.out.println(caesarDecode(test, 5));
          obtainKeys(&quot;abcxyz&quot;);
          System.out.println(vigenereEncode(&quot;elephants&quot;, &quot;rats&quot;));
      }
  
      //Q2a-b
      //Generalized method for char shifts
      public static char charShift(char c, int n) {
          //value of n should be between 0 and 25
          if (Math.abs(n) &lt; 0 || 25 &lt; Math.abs(n)) {
              //returning the ascii value of '0' which
              //is nul &amp; adding error message
              int zero = 0;
              c = (char) zero;
              throw new IllegalArgumentException(&quot;n has to be 0&lt;=|n|&lt;=25&quot;);
          }
          //character c should be a lower case latin letter
          //if not, we simply return c, the original character,
          //skipping this else if
          else if (c &gt;= 'a' &amp;&amp; c &lt;= 'z') {
              c = (char) (c + n);
              if (c &gt; 'z') {
                  c = (char) (c - 26);
              } else if (c &lt; 'a') {
                  c = (char) (c + 26);
              }
          }
          return c;
      }
  
      //method that shifts the value of the character to the right
      public static char charRightShift(char c, int n) {
          c = charShift(c, n);
          return c;
      }
  
      //method that shifts the value of the character to the left
      public static char charLeftShift(char c, int n) {
          n = -n;
          c = charShift(c, n);
          return c;
      }
  
      //Q2c
      //method that shifts the message to the right by int 'key' characters
      public static String caesarEncode(String message, int key) {
          //transform string into char array
          char[] messageEncrypt = message.toCharArray();
          //for each char, we shift it by 'key' ints,
          //using charRightShift method
          for (int i = 0; i &lt; messageEncrypt.length; i++) {
              char c = messageEncrypt[i];
              c = charRightShift(c, key);
              messageEncrypt[i] = c;
          }
          return new String(messageEncrypt);
      }
  
      //Q2d
      //method that shifts the message to the left by int 'key' characters
      public static String caesarDecode(String message, int key) {
          //transform string into char array
          char[] messageDecrypt = message.toCharArray();
          //for each char, we shift it by 'key' ints using charLeftShift
          for (int i = 0; i &lt; messageDecrypt.length; i++) {
              char c = messageDecrypt[i];
              c = charLeftShift(c, key);
              messageDecrypt[i] = c;
          }
          return new String(messageDecrypt);
      }
  
      //Q2e
      //method to obtain the int array storing the numerical value of the String
      public static int[] obtainKeys(String s) {
          //creating int array where we're going to
          //store the numerical value of the String
          int[] keys = new int[s.length()];
          int j;
          //for each ascii value of the char in string s, we substract 97 to
          //get the lower case english alphabet character corresponding to it
          for (int i = 0; i &lt; s.length(); i++) {
              char c = s.charAt(i);
              j = c - 97;
              //now store every int in the int array
              keys[i] = j;
          }
          String keysString = Arrays.toString(keys);
          return keys;
      }
  
      //Q2f
      public static String vigenereEncode(String message, String keyword) {
          //for loop check if there are any 'illegal' characters in the keyword
          char[] kword = keyword.toCharArray();
          for (int i = 0; i &lt; kword.length; i++) {
              char c = kword[i];
              if (c &lt; 'a' || c &gt; 'z') {
                  throw new IllegalArgumentException(
                          &quot;The keyword must only contain characters &quot; +
                                  &quot;from the lower case English alphabet.&quot;);
              }
          }
          int[] numMessage = obtainKeys(message);
          int[] numKeyword = obtainKeys(keyword);
          for (int i = 0; i &lt; message.length(); i++) {
              for (int j = 0; j &lt; keyword.length(); i++) {
                  //NOT SURE IF I NEED A NESTED LOOP HERE
                  //WHAT TO DO HERE?
              }
          }
          return messageVigenere;
      }
  }
  </code></pre>