SM4加解密实现

对称加密用于敏感个人数据加密

package com.example.demo.service;

import org.bouncycastle.crypto.engines.SM4Engine;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.paddings.PKCS7Padding;
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.modes.CBCBlockCipher;

import java.util.Arrays;

public class SM4CBCFixedKey {

    // 固定密钥(16字节)
    private static final byte[] FIXED_KEY = hexToBytes("11223344556677889900AABBCCDDEEFF");

    // 固定 IV(16字节)
    private static final byte[] FIXED_IV = hexToBytes("A1A2A3A4A5A6A7A8A9AAABACADAEAFB0");

    // CBC 加密
    public static byte[] encrypt(byte[] data) throws Exception {
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new SM4Engine()), new PKCS7Padding());
        cipher.init(true, new ParametersWithIV(new KeyParameter(FIXED_KEY), FIXED_IV));
        return process(cipher, data);
    }

    // CBC 解密
    public static byte[] decrypt(byte[] encrypted) throws Exception {
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new SM4Engine()), new PKCS7Padding());
        cipher.init(false, new ParametersWithIV(new KeyParameter(FIXED_KEY), FIXED_IV));
        return process(cipher, encrypted);
    }

    // 加/解密公共逻辑
    private static byte[] process(BufferedBlockCipher cipher, byte[] input) throws Exception {
        byte[] output = new byte[cipher.getOutputSize(input.length)];
        int len = cipher.processBytes(input, 0, input.length, output, 0);
        len += cipher.doFinal(output, len);
        return Arrays.copyOf(output, len);
    }

    // HEX 工具方法
    public static byte[] hexToBytes(String hex) {
        int len = hex.length();
        byte[] result = new byte[len / 2];
        for (int i = 0; i < len; i += 2)
            result[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
                    + Character.digit(hex.charAt(i+1), 16));
        return result;
    }

    public static String bytesToHex(byte[] data) {
        StringBuilder sb = new StringBuilder();
        for (byte b : data)
            sb.append(String.format("%02X", b));
        return sb.toString();
    }

    public static void main(String[] args) throws Exception {
        String plaintext = "这是 CBC 模式下的 SM4 加密测试。";

        System.out.println("原文: " + plaintext);

        byte[] encrypted = encrypt(plaintext.getBytes("UTF-8"));
        System.out.println("密文 : " + bytesToHex(encrypted));

        byte[] decrypted = decrypt(encrypted);
        System.out.println("解密结果: " + new String(decrypted, "UTF-8"));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暴走的YH

非常感谢大家的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值