title: Java method to decrypt an AES string from Angular-Cryptography
body: <p>I'm trying to do a login method that keeps the client password secure and encrypted in my server.</p>
  
  <p>The library I'm using from Angular, is <a href="https://github.com/middleout/angular-cryptography" rel="nofollow">https://github.com/middleout/angular-cryptography</a></p>
  
  <p>The idea is, to follow these steps:</p>
  
  <ol>
  <li><p>I set a salt in my module.config: </p>
  
  <pre><code>app.config(['$cryptoProvider', function($cryptoProvider){
      $cryptoProvider.setCryptographyKey('thisismysalt');
  }]);
  </code></pre></li>
  <li><p>I encrypt the password with itself:</p>
  
  <pre><code>user.pw = $crypto.encrypt(user.pw, user.pw);
  </code></pre></li>
  <li><p>If I'm registering an user, I re-encrypt the password with itself (repeat step 2) and save it in the DB. If I'm logging, I just send the result from the last step, to the server.</p></li>
  <li><p>When you decypher the double-encrypted string with the single-encrypted one, you get the single encrypted string again. So if your password was correct, you just compare the result with the single-encrypted string, and you validate the user.</p></li>
  </ol>
  
  <hr>
  
  <p>Ok, this method should work (I already did it in Node some time ago), works great with SSL to protect user's passwords even in your server!</p>
  
  <p>But I can't find any library or snippet in Java that can do it. I tried many of them, but they are hard to understand and when I adapt them to my procedure, they just won't work. I tried the following method:</p>
  
  <pre><code>static String IV = "AAAAAAAAAAAAAAAA";
  static String plaintext = "test text 123\0\0\0"; /*Note null padding*/
  static String encryptionKey = "0123456789abcdef";
  
  public static String decrypt(byte[] cipherText, String encryptionKey) throws Exception{
      Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
      SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
      cipher.init(Cipher.DECRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
      return new String(cipher.doFinal(cipherText),"UTF-8");
  }
  </code></pre>
  
  <p>I passed it as first argument, the double-encrypted password from the DB, and as second argument, the single-encrypted password from the frontend:</p>
  
  <blockquote>
    <p>java.security.InvalidKeyException: Invalid AES key length: 44 bytes</p>
  </blockquote>
  
  <p>Am I doing something wrong? Should I use a different algorithm? </p>