title: Vigenere Cipher java UTF-8
body: <p>Hello evrybody who reads this!</p>
  
  <p>I need to realize Vigenere cipher on Java.
  I have a .txt document, which I'm going to read, encode and decode. Here it is:</p>
  
  <pre><code>ASCII     abcde xyz
  German    äöü ÄÖÜ ß
  Polish    ąęźżńł
  Russian   абвгдеж эюя
  CJK       你好 
  </code></pre>
  
  <p>My problem is that I don't know how to shift chars correctly, According to <a href="http://unicode-table.com/ru/" rel="nofollow">this table</a> latin letters have codes from 0061 to 007A. German ones that I need: 00C0 - 00FF, polish: 0100-017F, russian 0430-044F and I didn't gind chineese.</p>
  
  <p>How can I specify <code>unshiftChar</code> and <code>shiftChar</code> to make it correst?
  Now my input looks like this:</p>
  
  <pre><code>The original text from file is: 
  ﻿ASCII     abcde xyz
  German    äöü ÄÖÜ ß
  Polish    ąęźżńł
  Russian   абвгдеж эюя
  CJK       你好 
  
  String that will be encoded is: 
  asciiabcdexyzgermanäöüäöüßpolishąęźżńłrussianабвгдежэюяcjk你好
  
  The encrypted string is: 
  äckkwdfaqmzjökcbucäbdslhwfssjvåjoxfbsltfvwgnvboegbrnboeghxöb
  
  The decrypted phrase is: 
  asciiab￥dexyzg￧rmanäö￷äöuupo￮ibmjcå￷äldhtc￲iwmtdå￶awmtdd￼pw
  </code></pre>
  
  <p>Here is a Java code:</p>
  
  <pre><code>    public class VigenereCipher 
      {
      public static void main(String[] args) throws IOException
      {
          String key = "Unicode";
  
          File file = new File("G:\\unicode.txt");
  
          FileInputStream fis = new FileInputStream(file);
          byte[] fileBArray = new byte[fis.available()];
          fis.read(fileBArray);
  
          String text = new String(fileBArray, "UTF-8");
  
          //String text = "Some simple text to check the decoding algorythm";
          System.out.println("The original text from file is: \n" + text);
          String enc = encrypt(text, key);
          System.out.println(enc + "\n");
          System.out.println("The decrypted phrase is: ");
          System.out.println(decrypt(enc, key));
      }
  
      // Encrypts a string
      public static String encrypt(String message, String key) 
      {
          message = StringToLowerCaseWithAllSymbols(message);
          System.out.println("String that will be encoded is: \n" + message);
          char messageChar, keyChar;
          String encryptedMessage = "";
          for (int i = 0; i &lt; message.length(); i++) 
          {
              messageChar = shiftChar(message.charAt(i));
              keyChar = shiftChar(key.charAt(i % key.length()));
              messageChar = (char) ((keyChar + messageChar) % 29);
              messageChar = unshiftChar(messageChar);
              encryptedMessage += messageChar;
          }
          System.out.println("\nThe encrypted string is: ");
          return encryptedMessage;
      }
  
      // Decrypts a string
       public static String decrypt(String cipher,String key)
       {
              char cipherChar, keyChar;
              cipher = StringToLowerCaseWithAllSymbols(cipher);
              String decryptedMessage = "";
              cipher = cipher.toLowerCase();
              for (int i = 0; i &lt; cipher.length(); i++) 
              {
                  cipherChar = shiftChar(cipher.charAt(i));
                  keyChar = shiftChar(key.charAt(i % key.length()));
                  cipherChar = (char) ((29 + cipherChar - keyChar) %  29);
                  cipherChar = unshiftChar(cipherChar);
                  decryptedMessage += cipherChar;
              }
              return decryptedMessage;
       }
  
       // Prunes all characters not in the alphabet {A-Öa-ö} from a string and changes it to all lower     case.
       public static String StringToLowerCaseWithAllSymbols(String s) 
       {
              //s = s.replaceAll("[^A-Za-zåäöÅÄÖ]", "");
              // 's' contains all the symbols from my text
              s = s.replaceAll("[^A-Za-zäöüÄÖÜßąęźżńłабвгдежэюя你好]", "");
              return s.toLowerCase();
          }
  
      // Assigns characters a,b,c...å,ä,ö the values 1,2,3...,26,28,29.
       private static char shiftChar(char c) 
       {
              if (96 &lt; c &amp;&amp; c &lt; 123) 
              {
                  c -= 97;
              } 
              else if (c == 229) 
              {
                  c = 26;
              } 
              else if (c == 228) 
              {
                  c = 27;
              } 
              else if (c == 246) 
              {
                  c = 28;
              }
              return c;
          }
  
      // Undoes the assignment in shiftChar and gives the characters back their UTF-8 values. 
       private static char unshiftChar(char c) 
       {
              if (0 &lt;= c &amp;&amp; c &lt;= 25) 
              {
                  c += 97;
              } 
              else if (c == 26) 
              {
                  c = 229;
              } 
              else if (c == 27) 
              {
                  c = 228;
              } 
              else if (c == 28) 
              {
                  c = 246;
              }
              return c;
          }
  }
  </code></pre>