在 Spring Boot 中集成自定义 Base64 密钥,以及使用 SM2 和 SM4 加密算法,您可以按照以下步骤进行操作:

  1. 添加相关依赖:在您的 Spring Boot 项目的 pom.xml 文件中添加相关的依赖,以引入加密算法和 Base64 编解码的支持。例如,对于 SM2 和 SM4 加密算法,您可以使用 Bouncy Castle 加密库的依赖:
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.68</version>
</dependency>
  1. 创建密钥:您可以在配置文件中配置您的 Base64 密钥字符串,然后在代码中进行解码,生成相应的密钥。以下是一个示例代码:
import org.bouncycastle.util.encoders.Base64;

public class KeyUtils {

    public static byte[] decodeBase64Key(String base64Key) {
        return Base64.decode(base64Key);
    }
}
  1. 实现加密解密逻辑:编写代码使用 SM2 和 SM4 加密算法进行加密和解密操作。以下是一个示例代码:
import org.bouncycastle.crypto.engines.SM2Engine;
import org.bouncycastle.crypto.engines.SM4Engine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PKCS7Padding;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;
import java.util.Base64;

public class EncryptionUtils {

    public static String encryptWithSM2(String plainText, byte[] publicKey) throws Exception {
        Security.addProvider(new BouncyCastleProvider());

        SM2Engine sm2Engine = new SM2Engine();
        sm2Engine.init(true, null); // 使用默认公钥加密

        byte[] encrypted = sm2Engine.processBlock(plainText.getBytes(), 0, plainText.getBytes().length);

        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static String decryptWithSM2(String encryptedText, byte[] privateKey) throws Exception {
        Security.addProvider(new BouncyCastleProvider());

        SM2Engine sm2Engine = new SM2Engine();
        sm2Engine.init(false, null); // 使用默认私钥解密

        byte[] encryptedData = Base64.getDecoder().decode(encryptedText);
        byte[] decrypted = sm2Engine.processBlock(encryptedData, 0, encryptedData.length);

        return new String(decrypted);
    }

    public static String encryptWithSM4(String plainText, byte[] key) throws Exception {
        Security.addProvider(new BouncyCastleProvider());

        byte[] iv = new byte[16]; // 初始化向量为 16 字节长度的随机数

        SM4Engine sm4Engine = new SM4Engine();
        CBCBlockCipher cipher = new CBCBlockCipher(sm4Engine);
        PKCS7Padding padding = new PKCS7Padding();
        cipher = new CBCBlockCipher(cipher); // 使用 CBC 模式

        ParametersWithIV parametersWithIV = new ParametersWithIV(new KeyParameter(key), iv);
        cipher.init(true, parametersWithIV); // 加密模式

        byte[] plaintextBytes = plainText.getBytes();
        byte[] ciphertext = new byte[cipher.getOutputSize(plaintextBytes.length)];
        int outputSize = cipher.processBytes(plaintextBytes, 0, plaintextBytes.length, ciphertext, 0);
        cipher.doFinal(ciphertext, outputSize);

        return Base64.getEncoder().encodeToString(ciphertext);
    }

    public static String decryptWithSM4(String encryptedText, byte[] key) throws Exception {
        Security.addProvider(new BouncyCastleProvider());

        byte[] iv = new byte[16]; // 初始化向量为 16 字节长度的随机数

        SM4Engine sm4Engine = new SM4Engine();
        CBCBlockCipher cipher = new CBCBlockCipher(sm4Engine);
        PKCS7Padding padding = new PKCS7Padding();
        cipher = new CBCBlockCipher(cipher); // 使用 CBC 模式

        ParametersWithIV parametersWithIV = new ParametersWithIV(new KeyParameter(key), iv);
        cipher.init(false, parametersWithIV); // 解密模式

        byte[] ciphertextBytes = Base64.getDecoder().decode(encryptedText);
        byte[] decrypted = new byte[cipher.getOutputSize(ciphertextBytes.length)];
        int outputSize = cipher.processBytes(ciphertextBytes, 0, ciphertextBytes.length, decrypted, 0);
        cipher.doFinal(decrypted, outputSize);

        return new String(decrypted);
    }
}

这样,您就可以使用自定义的 Base64 密钥和 SM2/SM4 加密算法进行加密和解密操作了。确保在实际使用中处理异常、密钥的存储和管理、合理配置算法参数等。请注意,以上示例代码仅供参考,您需要根据实际情况进行调整和扩展。

SpringBoot 集成自定义 Base64 密钥,SM2 和 SM4 加密算法

原文地址: https://www.cveoy.top/t/topic/APq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录