title: AES javascript encryption and Java decryption
body: <p>I've already implemented RSA encryption in javascrypt and RSA decryption in java which is just a simple process. But the problem I've to encrypt a large amount of data which is not possible for RSA at a single go, either I should have to split the data to be encrypted (which will complicate the process) or use AES with RSA encryption and decryption. So I opted to go for AES with RSA encryption and decryption.</p>
  
  <p>Here is my javascript code which use Crypto-js</p>
  
  <pre><code>&lt;script src="rollups/aes.js"&gt;&lt;/script&gt;
  &lt;script src="components/enc-base64-min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="rollups/jquery-min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
      var secretPass = CryptoJS.lib.WordArray.random(16);
      var message = "&lt;username&gt;user&lt;/username&gt;&lt;password&gt;password&lt;/password&gt;";
      var encrypted = CryptoJS.AES.encrypt(message, CryptoJS.enc.Hex.stringify(secretPass));
      var encode = encrypted.ciphertext.toString(CryptoJS.enc.Base64);
      var secretPasses = CryptoJS.enc.Hex.stringify(secretPass);
      console.log('encrypted: ',encrypted);
      console.log('secretPasses: ',secretPasses);
      console.log('encode: ',encode);
      $.ajax({
              url: 'encryption',
              type: 'POST',
              data: {
                  encode: encode,
                  secretPasses: secretPasses
              },
              success: function(data) {
                  console.log('success');
              },
              failure: function(data) {
                  console.log('failure');
              }
          });
      &lt;/script&gt;
  </code></pre>
  
  <p>Output in Jsp</p>
  
  <pre><code>encrypted: U2FsdGVkX192e9xprFPyuWu3Rxv2+CDMXiu2/TtNDwExvo4Dstx1mbqCHgds27Ng7zhYayVLjifeG15cuHI7hHfmEWvVeo7DDmOUsZmQAEM=
  secretPasses: 23f96d28ae9f9c1c8c37050f79acdb37
  encode: a7dHG/b4IMxeK7b9O00PATG+jgOy3HWZuoIeB2zbs2DvOFhrJUuOJ94bXly4cjuEd+YRa9V6jsMOY5SxmZAAQw==
  </code></pre>
  
  <p>In my post method of servlet, I used sysout to check the received data is same or not. The secret pass I'm getting is the same, encoded data is also the same. The problem is, encode data changed its form in the jsp itself while doing the conversion from encrypted to encode. I tried to pass "encrypted" directly via ajax, but it's pointing error, if I put "alert(typepof encrypted);", it alerts as "Object". How can I pass the original encrypted data to servlet?</p>
  
  <pre><code>System.out.println("secretpasses: "+request.getParameter("secretPasses"));
  System.out.println("encode: "+request.getParameter("encode"));
  </code></pre>
  
  <p>Output in Java :</p>
  
  <pre><code>secretpasses: 23f96d28ae9f9c1c8c37050f79acdb37
  encode: a7dHG/b4IMxeK7b9O00PATG+jgOy3HWZuoIeB2zbs2DvOFhrJUuOJ94bXly4cjuEd+YRa9V6jsMOY5SxmZAAQw==
  </code></pre>
  
  <p>Also it would be welcome if I can get some examples for AES encryption in Javascript and decryption in Java. I notified that it's AES with RSA encryption and decryption, but it's not inserted into current code. If I can get the AES part working, I can do RSA appropriately by encrypting the AEs key.</p>