title: Encrypt Spring Boot Rest Api Query Param
body: <p>I know this question has been asked multiple times, but none of them are answering my question, So please don't mark this as duplicate.</p>
  
  <p>I need to encrypt my parameters in rest API using AES.
  I have created 2 APIs, for encryption and decryption.</p>
  
  <pre><code>import AesTest;
      import com.google.zxing.BarcodeFormat;
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.web.bind.annotation.*;  
      import javax.servlet.http.HttpServletResponse;
      import java.net.URLDecoder;
      import java.net.URLEncoder;
  
  @RestController
  @SpringBootApplication
  public class BarcodeApplication {
  
  public static void main(String[] args) {
      SpringApplication.run(BarcodeApplication.class, args);
  }
  
  @GetMapping(value = "encrypt")
  public String encrypt(@RequestParam BarcodeFormat barcodeFormat, @RequestParam(defaultValue = "this is test") String text, @RequestParam(defaultValue = "200") String width, @RequestParam(defaultValue = "200") String height, HttpServletResponse response) throws Exception {
      System.out.println("barcodeFormat = " + barcodeFormat);
      System.out.println("text = " + text);
      System.out.println("width = " + width);
      System.out.println("height = " + height);
      response.setContentType("image/png");
      String encryptQuery = AesTest.encrypt(barcodeFormat.toString() + "&amp;" + text + "&amp;" + width + "&amp;" + height);
      String urlEncodedData = URLEncoder.encode(encryptQuery, "UTF-8");
      return urlEncodedData;
  }
  
  @RequestMapping(path = "barcode/{query}")
  public String getMessage(@PathVariable("query") String query, HttpServletResponse response) throws Exception {
      String decryptQuery = AesTest.decrypt(query);
      String urlEncodedData = URLDecoder.decode(decryptQuery, "UTF-8");
      String[] queryArr = urlEncodedData.split("&amp;");
      BarcodeFormat barcodeFormat = BarcodeFormat.valueOf(queryArr[0]);
      String text = queryArr[1];
      String width = queryArr[2];
      String height = queryArr[3];
      System.out.println("barcodeFormat = " + barcodeFormat);
      System.out.println("text = " + text);
      System.out.println("width = " + width);
      System.out.println("height = " + height);
      return null;
  }
  </code></pre>
  
  <p>}`</p>
  
  <p>Below is my AesTest File</p>
  
  <pre><code>import org.apache.tomcat.util.codec.binary.Base64;
  import javax.crypto.Cipher;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.SecretKeySpec;
  import java.nio.charset.StandardCharsets;
  
  public class AesTest {
      private static final String secretKey = "aesEncryptionKeyasdfghjk";
      private static final String initVector = "encryptionIntVec";
  
  public static String decrypt(String encrypted) {
      try {
          IvParameterSpec iv = new IvParameterSpec(initVector.getBytes(StandardCharsets.UTF_8));
          SecretKeySpec skeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "AES");
          Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
          cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
          byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
          return new String(original);
      } catch (Exception ex) {
          ex.printStackTrace();
      }
      return null;
  }
  
  public static String encrypt(String value) {
      try {
          IvParameterSpec ivParameterSpec = new IvParameterSpec(initVector.getBytes(StandardCharsets.UTF_8));
          SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "AES");
          Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
          cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
          byte[] encrypted = cipher.doFinal(value.getBytes());
          return Base64.encodeBase64String(encrypted);
      } catch (Exception ex) {
          ex.printStackTrace();
      }
      return null;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>if I am using <a href="http://localhost:8080/encrypt?text=" rel="nofollow noreferrer">http://localhost:8080/encrypt?text=</a><strong>test</strong>&amp;height=50&amp;barcodeFormat=CODE_128&amp;width=200</p>
  
  <p>it returns me 9mXpav2uRPRsNufSRpj1aXiD5sBVgmjFoyst8cQp%2Bms%3D.</p>
  
  <p>passing this value in 2nd API <a href="http://localhost:8080/barcode/9mXpav2uRPRsNufSRpj1aXiD5sBVgmjFoyst8cQp%2Bms%3D" rel="nofollow noreferrer">http://localhost:8080/barcode/9mXpav2uRPRsNufSRpj1aXiD5sBVgmjFoyst8cQp%2Bms%3D</a> is working perfectly fine.</p>
  
  <p>but if I change the value of 1st API ie, <a href="http://localhost:8080/encrypt?text=" rel="nofollow noreferrer">http://localhost:8080/encrypt?text=</a><strong>testshri</strong>&amp;height=50&amp;barcodeFormat=CODE_128&amp;width=200</p>
  
  <p>it returns Js%2Butdm9mS9jxSNBbvHeCJaKv7TqrtImTU%2FqcDWaSo8%3D.</p>
  
  <p>passing this value in 2nd API returns HTTP Status 400 – Bad Request.</p>
  
  <p>Changing <strong>text</strong> in query param is giving different results.
  What am I doing wrong???</p>