| src/main/java/com/dji/sample/droneairport/controller/RegistController.java | ●●●●● patch | view | raw | blame | history | |
| src/main/java/com/dji/sample/droneairport/service/impl/RegistServiceImpl.java | ●●●●● patch | view | raw | blame | history | |
| src/main/java/com/dji/sample/droneairport/utils/SM2/Test.java | ●●●●● patch | view | raw | blame | history | |
| src/main/java/com/dji/sample/droneairport/utils/SM2/Utils.java | ●●●●● patch | view | raw | blame | history | |
| src/main/java/com/dji/sample/droneairport/utils/SM4Util.java | ●●●●● patch | view | raw | blame | history |
src/main/java/com/dji/sample/droneairport/controller/RegistController.java
@@ -20,6 +20,14 @@ @PostMapping("/addDevice") public ResponseResult registeDrone(@RequestBody AddDeviceParam param) { param.setBrand("大疆"); param.setDeviceid("1581F6QAD241500BDZ3J"); param.setModel("M3D"); param.setHeight(15); param.setLatitude(25.8917271933111); param.setLongitude(116.02094292569765); param.setRadius(7000); param.setRegioncode("360781"); return ResponseResult.success(registService.addDrone(param)); } @PostMapping("/test") src/main/java/com/dji/sample/droneairport/service/impl/RegistServiceImpl.java
@@ -4,13 +4,21 @@ import com.dji.sample.droneairport.model.param.AddDeviceParam; import com.dji.sample.droneairport.model.param.RegistParam; import com.dji.sample.droneairport.service.RegistService; import com.dji.sample.droneairport.utils.AuthUtil; import com.dji.sample.droneairport.utils.SM2.Utils; import com.dji.sample.droneairport.utils.SM4Util; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.bouncycastle.crypto.InvalidCipherTextException; import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import java.io.IOException; import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class RegistServiceImpl implements RegistService { @@ -22,18 +30,20 @@ // 构建请求体 String jsonBody = buildRequestBody(param); // 设置请求头 String base64=SM4Util.encrypt("jsimjrby3wqb7dbq",jsonBody); // 设置请求头 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.setAccept(List.of(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.ALL)); headers.set("Accept-Language", "zh-CN,zh;q=0.9"); headers.setConnection("keep-alive"); headers.set("x-auth-token", ""); headers.set("x-lc-token",""); headers.set("x-auth-token", AuthUtil.getToken()); headers.set("x-lc-token",Utils.encrypt("jsimjrby3wqb7dbq","044D6061FC08A19D3F32CEAA8CF6679B40500008FD741FC26DE7E50AEBF3A9115D47274437730EADEDAEF0CCC4853C5F0B35B30C6AEA83A5F6FBCA4ABEAC9E3B98")); // 构建请求实体 HttpEntity<String> requestEntity = new HttpEntity<>(jsonBody, headers); HttpEntity<String> requestEntity = new HttpEntity<>(base64, headers); // 发送请求 ResponseEntity<String> response = restTemplate.exchange( "https://wrj.shuixiongit.com/drone-api/droneAirport/test", "https://xcx.geoway.com.cn:8033/v1/droneAirport/RegistService", HttpMethod.POST, requestEntity, String.class); @@ -58,23 +68,41 @@ try { // 构建请求体 String jsonBody = buildRequestBody(param); String base64 = SM4Util.encrypt("jsimjrby3wqb7dbq", jsonBody); System.out.println(AuthUtil.getToken()); System.out.println(AuthUtil.getToken()); System.out.println(AuthUtil.getToken()); System.out.println(AuthUtil.getToken()); System.out.println(AuthUtil.getToken()); System.out.println(AuthUtil.getToken()); System.out.println(AuthUtil.getToken()); System.out.println(AuthUtil.getToken()); System.out.println(AuthUtil.getToken()); System.out.println(AuthUtil.getToken()); System.out.println(AuthUtil.getToken()); System.out.println(AuthUtil.getToken()); System.out.println(AuthUtil.getToken()); System.out.println(AuthUtil.getToken()); System.out.println(AuthUtil.getToken()); System.out.println(AuthUtil.getToken()); System.out.println(AuthUtil.getToken()); // 设置请求头 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.setAccept(List.of(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.ALL)); headers.set("Accept-Language", "zh-CN,zh;q=0.9"); headers.setConnection("keep-alive"); headers.set("x-auth-token", ""); headers.set("x-lc-token",""); headers.set("x-lc-token", AuthUtil.getToken()); headers.set("x-lc-secret", Utils.encrypt("jsimjrby3wqb7dbq", "044D6061FC08A19D3F32CEAA8CF6679B40500008FD741FC26DE7E50AEBF3A9115D47274437730EADEDAEF0CCC4853C5F0B35B30C6AEA83A5F6FBCA4ABEAC9E3B98")); // 构建请求实体 HttpEntity<String> requestEntity = new HttpEntity<>(jsonBody, headers); HttpEntity<byte[]> requestEntity = new HttpEntity<>(base64.getBytes(), headers); // 发送请求 ResponseEntity<String> response = restTemplate.exchange( "https://wrj.shuixiongit.com/drone-api/droneAirport/test", HttpMethod.POST, requestEntity, String.class); return response.getBody(); RestTemplate restTemplate = new RestTemplate(); String response = restTemplate.postForObject("https://xcx.geoway.com.cn:8033/v1/droneAirport/addDevice", requestEntity, String.class); return response; } catch (Exception e) { // 异常处理 @@ -94,4 +122,31 @@ throw new RuntimeException("数据有误", e); } } public static String getSM2(String data, String key, String pubKey) throws InvalidCipherTextException, IOException { String text = SM4Util.encrypt(key, data); String encrypt = Utils.encrypt(text, pubKey); encrypt = Utils.hexToBase64(encrypt); return encrypt; } public static String deSM2(String Base64, String priKey, String serect) throws Exception { String hex = Utils.convertBase64ToHex(Base64); String decrypt = Utils.decrypt(hex, priKey); return SM4Util.decrypt(serect, decrypt); } public static void main(String[] args) throws Exception { System.out.println(getSM2("jsimjrby3wqb7dbq", "jsimjrby3wqb7dbq", "044c4e29d89bc0389409e204b8c072af927d2bfbc62265472463f6fa2642e565411925c58ca72b8da87d743afffa9f94aeaee4351a0511692e06ad1567ba32d236")); System.out.println(deSM2("BN2taHcWvZhysmUbAHjPAWo/vAgW3pCTv4CK0dHJoUpM62k1oUeYnMvK4twfcBs/qkbygkAt+4pUaBa6iomM9MSKVCtOSb1QAaAQ9milAvkDPSBE9yrYHzLQeLkT9HwENBJ5NanF6vl2nPgpC1d1q+XeJEBrEvJtpQkSHHsh/me/1+h9zPy0MpMkH89S", "00e9ced7f847e5b414ef4f4f164ec5f83172b3183826c075f0e37d3f8a1e1f6c2c", "jsimjrby3wqb7dbq")); System.out.println(createKey()); } public static Map<String, String> createKey() throws NoSuchAlgorithmException { Map<String, String> keyPair = Utils.createKeyPair(); Map<String, String> newKey = new HashMap<>(); newKey.put("publicKey", keyPair.get("pbcky")); newKey.put("privateKey", keyPair.get("pveky")); return newKey; } } src/main/java/com/dji/sample/droneairport/utils/SM2/Test.java
File was deleted src/main/java/com/dji/sample/droneairport/utils/SM2/Utils.java
@@ -1,11 +1,12 @@ package com.dji.sample.droneairport.utils.SM2; import org.bouncycastle.crypto.InvalidCipherTextException; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.NoSuchAlgorithmException; import java.util.Base64; import java.util.Map; public class Utils { /** @@ -17,15 +18,18 @@ /** * encrypt * * @param plainText 需加密的明文字符串 * @param publicKey 公钥 */ public static String encrypt(String plainText, String publicKey) throws IOException, InvalidCipherTextException { publicKey=ensurePrefix(publicKey); return encrypt(plainText, publicKey, ModeTypeConstant.BASE); } /** * encrypt * * @param plainText 需加密的明文字符串 * @param publicKey 公钥 * @param modeType base:标准;bc:BC模式 @@ -36,15 +40,18 @@ /** * decrypt * * @param cipherText 需加密的字符串 * @param privateKey 私钥 */ public static String decrypt(String cipherText, String privateKey) throws InvalidCipherTextException, UnsupportedEncodingException { cipherText = ensurePrefix(cipherText); return decrypt(cipherText, privateKey, ModeTypeConstant.BASE); } /** * decrypt * * @param cipherText 需加密的字符串 * @param privateKey 私钥 * @param modeType base:标准;bc:BC模式 @@ -53,4 +60,66 @@ return SecretCommon.decrypt(cipherText, privateKey, ModeTypeConstant.getMode(modeType)); } public static String ensurePrefix(String input) { // 检查字符串是否以 "04" 开头 if (!input.startsWith("04")) { return "04" + input; // 添加前缀 } return input; // 原样返回 } // 将 Hex 格式的字符串转换为字节数组 public static byte[] hexToBytes(String hex) { int length = hex.length(); byte[] data = new byte[length / 2]; for (int i = 0; i < length; i += 2) { data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16)); } return data; } // 将 DER 格式的字节数组转换为 Base64 编码的字符串 public static String convertDerToBase64(byte[] derKey) { return Base64.getEncoder().encodeToString(derKey); } // 将 Base64 编码的字符串转换为 Hex 格式的字符串 public static String convertBase64ToHex(String base64Key) throws Exception { // 将 Base64 解码为 DER 编码的字节数组 byte[] derBytes = Base64.getDecoder().decode(base64Key); // 将 DER 编码的字节数组转换为 Hex 格式的字符串 return bytesToHex(derBytes); } // 将字节数组转换为 Hex 格式的字符串 private static String bytesToHex(byte[] bytes) { StringBuilder hexString = new StringBuilder(); for (byte b : bytes) { String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) { hexString.append('0'); } hexString.append(hex); } return hexString.toString().toUpperCase(); // Convert to uppercase for consistency } public static String hexToBase64(String hexString) { // 将 Hex 字符串转换为字节数组 byte[] bytes = hexStringToByteArray(hexString); // 将字节数组转换为 Base64 字符串 return Base64.getEncoder().encodeToString(bytes); } private static byte[] hexStringToByteArray(String hexString) { int len = hexString.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i+1), 16)); } return data; } } src/main/java/com/dji/sample/droneairport/utils/SM4Util.java
@@ -37,21 +37,52 @@ return Arrays.copyOf(output, length); } public static String decrypt(String keys,String secert){ byte[] key = keys.getBytes(StandardCharsets.UTF_8); byte[] ming= Base64.getDecoder().decode(secert); byte[] text=decrypt(key,ming); return new String(text, StandardCharsets.UTF_8); } public static String encrypt(String keys, String input){ byte[] key = keys.getBytes(StandardCharsets.UTF_8); return encrypt(key,input.getBytes()); } public static void main(String[] args) { byte[] key = "jsimjrby3wqb7dbq".getBytes(StandardCharsets.UTF_8); // 16字节密钥 // 原始明文 String plaintext = "hello"; String plaintext = "{\n" + "\"regioncode\": \"650000\",\n" + "\"deviceid\": \"dh11111111111\",\n" + "\"brand\": \"大疆\",\n" + "\"model\": \"M3D\",\n" + "\"longitude\": 120.6670,\n" + "\"latitude\": 30.4567,\n" + "\"height\": 45,\n" + "\"radius\": 10000\n" + "}\n"; System.out.println("Encoded: " + encrypt(key, plaintext.getBytes(StandardCharsets.UTF_8))); String secert =encrypt(key,plaintext.getBytes()); System.out.println(secert); // 解码并解密 byte[] decoded = Base64.getDecoder().decode(encrypt(key, plaintext.getBytes(StandardCharsets.UTF_8))); byte[] ming= Base64.getDecoder().decode(secert); byte[] text=decrypt(key,ming); System.out.println(new String(text, StandardCharsets.UTF_8)); System.out.println("封装"+encrypt("jsimjrby3wqb7dbq","hello")); System.out.println("封装"+decrypt("jsimjrby3wqb7dbq","MkzmRdHSy/pB4sdTjNHb4ngG0q7Z+vWzNKdJxJFkhw4Y9tI8R9sapL6ECLyVnKZwiZbmVQFtn+sT294nCVdskK8TUx7CCI3qo21d9I4ONl+XAYnlDCtEa8KSFK3CB3/D9zRt96zEDkv31Av2rdAJW9O7Le1Z13wWGmsEqHjjrXLv5jd/Cop6+BB9+lqDRlhx+ubNOFdUgJALe4wHjZPCMdE0ob44Hx+1iLg7m/WJQWc=")); // 直接加密 String encryptedBase64 = encrypt(key, plaintext.getBytes(StandardCharsets.UTF_8)); // System.out.println("Encrypted (Base64): " + encryptedBase64); // 直接解密 byte[] decoded = Base64.getDecoder().decode(encryptedBase64); byte[] decrypted = decrypt(key, decoded); // 输出结果 System.out.println("Plaintext: " + plaintext); System.out.println("Encrypted (Base64): " + encrypt(key, plaintext.getBytes(StandardCharsets.UTF_8))); System.out.println("Decrypted: " + new String(decrypted, StandardCharsets.UTF_8)); // // 输出结果 // System.out.println("Plaintext: " + plaintext); // System.out.println("Decrypted: " + new String(decrypted, StandardCharsets.UTF_8)); } }