title: How to encrypt a message at client side using crypto-js library and decrypt it at the Java server
body: <p><strong>Background:</strong>
  the application that I am working on is supposed to work offline. I have an HTML5 page and the data keyed in by the user is encrypted using crypto-js library.
  And I want the encrypted message sent to java webserver and then decrypt it at the server side.</p>
  
  <p><strong>What am doing</strong> 
  I am able to encrypt the message using Crypto-js    </p>
  
  <pre><code>&lt;code&gt;
  var message = "my message text";
  var password = "user password";
  var encrypted = CryptoJS.AES.encrypt( message ,password );
  console.log(encrypted.toString());
  // this prints an encrypted text "D0GBMGzxKXU757RKI8hDuQ=="
  &lt;/code&gt;
  </code></pre>
  
  <p>What I would like to do is pass the encrypted text "D0GBMGzxKXU757RKI8hDuQ==
  " to a java server side code and get the necrypted message decrypted.</p>
  
  <p>I tried many options to decrypt the crypto-js encrypted message at the java server side.
  Please find below my code at the server side that is supposed to do the decryption of the encrypted text.</p>
  
  <pre><code>&lt;code&gt;
  public static String decrypt(String keyText,String encryptedText) 
  {
  // generate key 
  Key key = new SecretKeySpec(keyText.getBytes(), "AES");
  Cipher chiper = Cipher.getInstance("AES");
  chiper.init(Cipher.DECRYPT_MODE, key);
  byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedText);
  byte[] decValue = chiper.doFinal(decordedValue);
  String decryptedValue = new String(decValue);
  return decryptedValue;
  }  
  &lt;/code&gt;
  </code></pre>
  
  <p>I call the java method decrypt from below code</p>
  
  <pre><code>&lt;code&gt;
  // performs decryption 
  public static void main(String[] args) throws Exception 
  {
  String decryptedText = CrypterUtil.decrypt("user password","D0GBMGzxKXU757RKI8hDuQ==");
  }
  &lt;/code&gt;
  </code></pre>
  
  <p>But i get the following exception when i run the java decrypt code </p>
  
  <pre><code>&lt;code&gt;
  Exception in thread "main" java.security.InvalidKeyException: Invalid AES key length: 13 bytes
  at com.sun.crypto.provider.AESCipher.engineGetKeySize(AESCipher.java:372)
  at javax.crypto.Cipher.passCryptoPermCheck(Cipher.java:1052)
  at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1010)
  at javax.crypto.Cipher.implInit(Cipher.java:786)
  at javax.crypto.Cipher.chooseProvider(Cipher.java:849)
  at javax.crypto.Cipher.init(Cipher.java:1213)
  at javax.crypto.Cipher.init(Cipher.java:1153)
  at au.gov.daff.pems.model.utils.CrypterUtil.decrypt(CrypterUtil.java:34)
  at au.gov.daff.pems.model.utils.CrypterUtil.main(CrypterUtil.java:47)
  Process exited with exit code 1.
  &lt;/code&gt;
  </code></pre>
  
  <p>Am not sure what am I doing wrong ?... What is the best way to encrypt a message using the crypto-js library so that it can be decripted else where using user keyed in password.</p>