title: What's wrong with Spring's @RequestBody byte[] (length bigger than expected)?
body: <p>I tried to use <code>@RequestBody byte[] data</code> as automatic Base64 decoder, in order to receive some RSA encrypted data and afterwards decrypt it in my service:</p>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>@RequestMapping
  void doIt(@RequestBody byte[] data) {
      service.doIt(data);
  }
  </code></pre>
  
  <p><strong>Service:</strong></p>
  
  <pre><code>void doIt(byte[] encryptedData) {               
      String testDataString = "hgLGtzF4D8tlJbVivQgaXXwfI9TbQ//PrYHEez5k93sqJSi17eLCa+r/cGNGvoxDRaPYONvP4yxW0ajKDFrusQ4V4owfhkijS9KzBOTjBeAVmA+5jBsZCdoxwCA65DiP5lJ+GRbn8CjcjCr4DaEWFbWHsyvY4NGGAQLuYv+PyZipfU9pXTEDsBb15NwaHlD5m7Z4CHWdWTt1ARvRaQs56Bp63/IEmGR7w4brA1+iuKPv83FLh0rsxyoJ+F8TeqtuPhm2fHTh1FiHn0Bpaqqoyd/cBl0/utSzu4qoZhB3AiVgLjnT6Iy9p5nVoAozxQo/Es59LrpGZfjYJer073jNIg==";
      byte[] testDataBytes = Base64.decodeBase64(testDataString);
  
      System.out.println(encryptedData.length);
      System.out.println(testDataBytes.length);
  
      System.out.println(new String(encryptedData).length());
      System.out.println(Base64.encodeBase64String(testDataBytes).length());
  
      System.out.println(new String(encryptedData).equals(Base64.encodeBase64String(testDataBytes)));
  }
  </code></pre>
  
  <p><strong>Request:</strong></p>
  
  <pre><code>Content-Type: application/octet-stream
  
  Request Payload: 
  hgLGtzF4D8tlJbVivQgaXXwfI9TbQ//PrYHEez5k93sqJSi17eLCa+r/cGNGvoxDRaPYONvP4yxW0ajKDFrusQ4V4owfhkijS9KzBOTjBeAVmA+5jBsZCdoxwCA65DiP5lJ+GRbn8CjcjCr4DaEWFbWHsyvY4NGGAQLuYv+PyZipfU9pXTEDsBb15NwaHlD5m7Z4CHWdWTt1ARvRaQs56Bp63/IEmGR7w4brA1+iuKPv83FLh0rsxyoJ+F8TeqtuPhm2fHTh1FiHn0Bpaqqoyd/cBl0/utSzu4qoZhB3AiVgLjnT6Iy9p5nVoAozxQo/Es59LrpGZfjYJer073jNIg==
  </code></pre>
  
  <p><strong>The Output:</strong></p>
  
  <pre><code>(Spring) Byte length:          344
  (test)   Byte length:          256
  (Spring) Base64 String length: 344
  (test)   Base64 String length: 344
           Base64 String equals: true
  </code></pre>
  
  <p>As you can see:</p>
  
  <ul>
  <li><p>The request contains exactly the same data as the test data I put into the source code.</p></li>
  <li><p>After doing Base64 encoding both have the same length and are `equal``</p></li>
  <li><p><strong>BUT:</strong> The length of the byte arrays are different.</p></li>
  </ul>
  
  <h1>Question:</h1>
  
  <p>Why is the <code>byte[]</code> that was generated by Spring bigger? (It's useless when trying to decode it using some RSA private key, because RSA expects a length of 256.)</p>
  
  <h2>Workaround:</h2>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>@RequestMapping
  void doIt(@RequestBody String data) {
      service.doIt(Base64.decodeBase64(data));
  }
  </code></pre>