Я знаю, что этот вопрос задавался несколько раз, но никто из них не отвечает на мой вопрос, поэтому, пожалуйста, не отмечайте его как дубликат.
Мне нужно зашифровать мои параметры в API остальных с помощью AES. Я создал 2 API для шифрования и дешифрования.
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() + "&" + text + "&" + width + "&" + 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("&");
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;
}
} `
Ниже приведен мой файл AesTest
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;
}
}
if Я использую http://localhost: 8080 / encrypt? Text = test & height = 50 & barcodeFormat = CODE_128 & width = 200
возвращает мне 9mXpav2uRPRsNufSRpj1aXiD5sBVgmjFoystBmsm% 2 2 * 20ms%. *
передача этого значения во 2-й API http://localhost: 8080 / barcode / 9mXpav2uRPRsNufSRpj1aXiD5sBVgmjFoyst8cQp% 2Bms% 3D работает отлично.
, но если я изменю значение 1-го API ie, http://localhost: 8080 / зашифровать? Text = testshri & height = 50 & barcodeFormat = CODE_128 & width = 200
возвращает Js% 2Butdm9mS9jxSNBbvHeCJaKv7TDW2% UUCFMM 1034 *
Передача этого значения во 2-й API возвращает HTTP Status 400 - Bad Request.
Изменение text в параметре запроса дает разные результаты. Что я делаю не так ???