title: BadPaddingException when decrypting JSON data with encrypted values
body: <p>I have a third party API where I have to send data in JSON in encrypted form. The data in JSON looks like this:</p>
  <pre><code>{
      &quot;encryptedKey&quot;: &quot;rltP+oNBMx26wSpmvKZ91iL=&quot;,
      &quot;encryptedData&quot;: &quot;u5o+ON08CNGLwvt8OUmHXFPAzfk3uPILANA=&quot;
  }
  </code></pre>
  <p><strong>Note:</strong> The data in json values is trimmed to just show example.</p>
  <p>I am using Java11HttpClient to send this request to the given endpoint. The request with all encrypted values get processed well and I get a desired result that looks like same as request(Response is again in key value paired JSON and values are encrypted).</p>
  <pre><code>{
      &quot;encryptedKey&quot;: &quot;rltP+oNmvKZ91iL=&quot;,
      &quot;encryptedData&quot;: &quot;u5ot8OUmHXFPAzfk3uPILANA=&quot;
  }
  </code></pre>
  <p>I have to decrypt this JSON &quot;encryptedKey&quot; again to get actual content. The decryption is done using &quot;RSA/ECB/PKCS1Padding&quot;. So when I pass this String of encryptedKey to decrypt code it gives me BadPaddingException.</p>
  <pre><code>javax.crypto.BadPaddingException: Decryption error
          at java.base/sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:378)
          at java.base/sun.security.rsa.RSAPadding.unpad(RSAPadding.java:290)
          at java.base/com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:366)
          at java.base/com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:392)
          at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2202)
  </code></pre>
  <p>I am using this code:</p>
  <pre><code>HttpRequest request = HttpRequest.newBuilder().POST(BodyPublishers.ofString(requestPayload))
                  .uri(URI.create(url))
                  .header(&quot;Content-Type&quot;, &quot;application/json&quot;)
                  .header(&quot;Accept&quot;, &quot;application/json&quot;)
                  .headers(buildHeaders(headers).toArray(String[]::new)).build();
  </code></pre>
  <p>HttpResponse response = httpClient.send(request,
  HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));</p>
  <p>This above code works well for request and give me a response in structure same as expected. But the problem seems to be lying in the process of deserialization of response in String.</p>
  <p>Option 2: When I pick the encrypted request generated from code, same request if added as a payload in Postman, I get similar response. Now if I decrypt that encrypted key from Postman response, using same codebase it gives me a valid data without padding Exception.</p>
  <p>Need suggestion on the ways to send such request and process the response data with some library in Spring or Java11 etc.</p>
  <p>I have tried Unirest, Java11HttpClient and OkHttp so far but same results.</p>
  <p>Thanks in advance.</p>