From 4de7066db43c58b4d081e2c34a5111b0aa1dac16 Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Mon, 05 Aug 2024 16:00:50 +0800
Subject: [PATCH] SM2和SM4方法

---
 src/main/java/com/dji/sample/droneairport/utils/SM2/Utils.java |   75 ++++++++++++++++++++++++++++++++++++-
 1 files changed, 72 insertions(+), 3 deletions(-)

diff --git a/src/main/java/com/dji/sample/droneairport/utils/SM2/Utils.java b/src/main/java/com/dji/sample/droneairport/utils/SM2/Utils.java
index 3cf6efe..fe47c60 100644
--- a/src/main/java/com/dji/sample/droneairport/utils/SM2/Utils.java
+++ b/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,18 +18,21 @@
 
     /**
      * 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模式
+     * @param modeType  base:标准;bc:BC模式
      */
     public static String encrypt(String plainText, String publicKey, String modeType) throws IOException, InvalidCipherTextException {
         return SecretCommon.encrypt(plainText, publicKey, ModeTypeConstant.getMode(modeType));
@@ -36,21 +40,86 @@
 
     /**
      * 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模式
+     * @param modeType   base:标准;bc:BC模式
      */
     public static String decrypt(String cipherText, String privateKey, String modeType) throws InvalidCipherTextException, UnsupportedEncodingException {
         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;
+    }
 }

--
Gitblit v1.9.3