title: CryptoJS AES encryption and Java AES decryption
body: <p>I'm only asking this because I have read many posts for 2 days now about crypto AES encryption, and just when I thought I was getting it, I realized I wasn't getting it at all.  </p>
  
  <p>This post is the closest one to my issue, I have exactly the same problem but it is unanswered:</p>
  
  <p><a href="https://stackoverflow.com/questions/17780734/cryptojs-aes-encryption-and-java-aes-decryption-value-mismatch">CryptoJS AES encryption and JAVA AES decryption value mismatch</a></p>
  
  <p>I have tried doing it in many ways but I haven't gotten it right.</p>
  
  <p><strong>First Off</strong></p>
  
  <p><strong>I'm getting the already encrypted string (I only got the code to see how they were doing it), so modifying the encryption way is not an option. That's why all the similar questions aren't that useful to me.</strong></p>
  
  <p><strong>Second</strong></p>
  
  <p><strong>I do have access to the secret key and I can modify it (so adjusting length is an option if neccessary).</strong></p>
  
  <p>The encryption is done on CryptoJS and they send the encrypted string as a GET parameter.</p>
  
  <pre><code>GetParamsForAppUrl.prototype.generateUrlParams = function() {
  const self = this;
   return new Promise((resolve, reject) =&gt; {
     const currentDateInMilliseconds = new Date().getTime();
     const secret = tokenSecret.secret;
     var encrypted = CryptoJS.AES.encrypt(self.authorization, secret);
     encrypted = encrypted.toString();
     self.urlParams = {
       token: encrypted,
       time: currentDateInMilliseconds
     };
     resolve();
   });
  };
  </code></pre>
  
  <p>I can easily decrypt this on javascript using CryptoJS with:</p>
  
  <pre><code>var decrypted = CryptoJS.AES.decrypt(encrypted_string, secret);
      console.log(decrypted.toString(CryptoJS.enc.Utf8)); 
  </code></pre>
  
  <p>But I don't want to do this on Javascript, for security reasons, so I'm trying to decrypt this on Java:</p>
  
  <pre><code>String secret = "secret";
  byte[] cipherText = encrypted_string.getBytes("UTF8");
  SecretKey secKey = new SecretKeySpec(secret.getBytes(), "AES");
  Cipher aesCipher = Cipher.getInstance("AES");
  aesCipher.init(Cipher.DECRYPT_MODE, secKey);
  byte[] bytePlainText = aesCipher.doFinal(byteCipherText);
  String myDecryptedText = = new String(bytePlainText);
  </code></pre>
  
  <p>Before I had any idea of what I was doing, I tried base64 decoding, adding some IV and a lot of stuff I read, of course none of it worked. </p>
  
  <p>But after I started to understand, kinda, what I was doing, I wrote that simple script above, and got me the same error on the post: <strong>Invalid AES key length</strong></p>
  
  <p>I don't know where to go from here. After reading a lot about this, the solution seems to be hashing or padding, but I have no control on the encryption method, so I can't really hash the secret or pad it.</p>
  
  <p>But as I said, I can change the secret key so it can match some specific length, and I have tried changing it, but as I'm shooting in the dark here, I don't really know if this is the solution.</p>
  
  <p>So, my question basically is, If I got the encrypted string (<strong>in javascript like the first script</strong>) and the secret key, is there a way to decrypt it (<strong>in Java</strong>)? If so, how to do it?</p>