国密2工具类

使用了Bouncy Castle库来实现SM4算法的加密和解密。其中,generateKey方法用于生成随机密钥,encrypt方法用于加密数据,decrypt方法用于解密数据,generateIV方法用于生成随机IV

import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.engines.SM4Engine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.util.encoders.Hex;
 import java.security.SecureRandom;
 public class SM2Util {
     private static final int KEY_SIZE = 16;
    private static final int IV_SIZE = 16;
     /**
     * 生成随机密钥
     *
     * @return 随机密钥
     */
    public static byte[] generateKey() {
        byte[] key = new byte[KEY_SIZE];
        SecureRandom random = new SecureRandom();
        random.nextBytes(key);
        return key;
    }
     /**
     * 加密
     *
     * @param key 密钥
     * @param data 待加密数据
     * @return 加密后的数据
     */
    public static byte[] encrypt(byte[] key, byte[] data) {
        byte[] iv = generateIV();
        CipherParameters params = new ParametersWithIV(new KeyParameter(key), iv);
        CBCBlockCipher cipher = new CBCBlockCipher(new SM4Engine());
        cipher.init(true, params);
        byte[] encrypted = new byte[cipher.getOutputSize(data.length)];
        int len = cipher.processBytes(data, 0, data.length, encrypted, 0);
        try {
            cipher.doFinal(encrypted, len);
        } catch (Exception e) {
            throw new RuntimeException("Failed to encrypt data: " + e.getMessage());
        }
        return Hex.encode(encrypted);
    }
     /**
     * 解密
     *
     * @param key 密钥
     * @param data 待解密数据
     * @return 解密后的数据
     */
    public static byte[] decrypt(byte[] key, byte[] data) {
        byte[] iv = generateIV();
        CipherParameters params = new ParametersWithIV(new KeyParameter(key), iv);
        CBCBlockCipher cipher = new CBCBlockCipher(new SM4Engine());
        cipher.init(false, params);
        byte[] decrypted = new byte[cipher.getOutputSize(data.length)];
        int len = cipher.processBytes(data, 0, data.length, decrypted, 0);
        try {
            cipher.doFinal(decrypted, len);
        } catch (Exception e) {
            throw new RuntimeException("Failed to decrypt data: " + e.getMessage());
        }
        return decrypted;
    }
     /**
     * 生成随机IV
     *
     * @return 随机IV
     */
    private static byte[] generateIV() {
        byte[] iv = new byte[IV_SIZE];
        SecureRandom random = new SecureRandom();
        random.nextBytes(iv);
        return iv;
    }
}```

	/**
		测试main
	*/
  public static void main(String[] args) {
        byte[] key = SM2Util.generateKey();
		// 加密数据
        byte[] data = "Hello, world!".getBytes();
        byte[] encrypted = SM2Util.encrypt(key, data);
		// 解密数据
        byte[] decrypted = SM2Util.decrypt(key, encrypted);
        String message = new String(decrypted);
        System.out.println(message);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

郭优秀的笔记

你的支持就是我最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值