BouncyCastle

0. 在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务

1. 参考附件内容完成SM2加解密的内容,提交运行结果截图

2 完成SM3,SM4算法的调用,提交运行结果截图和代码

SM2

import org.bouncycastle.crypto.engines.SM2Engine;
import org.bouncycastle.crypto.params.*;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import org.bouncycastle.jce.spec.ECParameterSpec;
import org.bouncycastle.util.encoders.Hex;

import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;

public class SM2Util {
	/**
	 * SM2加密算法
	 * 
	 * @param publicKey 公钥
	 * @param data      明文数据
	 * @return
	 **/
	public String encrypt(PublicKey publicKey, String data) {
		ECPublicKeyParameters ecPublicKeyParameters = null;
		if (publicKey instanceof BCECPublicKey) {
			BCECPublicKey bcecPublicKey = (BCECPublicKey) publicKey;
			ECParameterSpec ecParameterSpec = bcecPublicKey.getParameters();
			ECDomainParameters ecDomainParameters = new ECDomainParameters(ecParameterSpec.getCurve(),
					ecParameterSpec.getG(), ecParameterSpec.getN());
			ecPublicKeyParameters = new ECPublicKeyParameters(bcecPublicKey.getQ(), ecDomainParameters);
		}
		SM2Engine sm2Engine = new SM2Engine();
		sm2Engine.init(true, new ParametersWithRandom(ecPublicKeyParameters, new SecureRandom()));

		byte[] arrayOfBytes = null;
		try {
			byte[] in = data.getBytes(StandardCharsets.UTF_8);
			arrayOfBytes = sm2Engine.processBlock(in, 0, in.length);
		} catch (Exception e) {
			System.out.println("SM2加密时出现异常:");
		}
		return Hex.toHexString(arrayOfBytes);
	}

	/**
	 * SM2解密算法
	 * 
	 * @param privateKey 私钥
	 * @param cipherData 密文数据
	 * @return
	 **/
	public String decrypt(PrivateKey privateKey, String cipherData) {
		byte[] cipherDataByte = Hex.decode(cipherData);
		BCECPrivateKey bcecPrivateKey = (BCECPrivateKey) privateKey;
		ECParameterSpec ecParameterSpec = bcecPrivateKey.getParameters();
		ECDomainParameters ecDomainParameters = new ECDomainParameters(ecParameterSpec.getCurve(),
				ecParameterSpec.getG(), ecParameterSpec.getN());
		ECPrivateKeyParameters ecPrivateKeyParameters = new ECPrivateKeyParameters(bcecPrivateKey.getD(),
				ecDomainParameters);
		SM2Engine sm2Engine = new SM2Engine();
		sm2Engine.init(false, ecPrivateKeyParameters);
		String result = null;
		try {
			byte[] arrayOfBytes = sm2Engine.processBlock(cipherDataByte, 0, cipherDataByte.length);
			return new String(arrayOfBytes, StandardCharsets.UTF_8);
		} catch (Exception e) {
			System.out.println("SM2解密时出现异常");
		}
		return result;
	}
}

SM3

import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.Security;

/**
 * @Description: 国密SM3摘要算法帮助类
 * @Author: wucheng
 * @CreateDate: 2020/2/16 16:36
 */
public class SM3Util {
	static {
		Security.addProvider(new BouncyCastleProvider());
	}

	public byte[] digest(String input) {
		SM3Digest sm3Digest = new SM3Digest();
		int length = input.length();
		byte[] data = input.getBytes();
		sm3Digest.update(data, 0, length);
		byte[] ret = new byte[sm3Digest.getDigestSize()];
		sm3Digest.doFinal(ret, 0);
		return ret;
	}
}

SM4

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;

/**
 * @Description: 国密SM4对称加解密算法帮助类
 * @Author: wucheng
 * @CreateDate: 2020/2/16 16:38
 */
public class SM4Util {
	static {
		Security.addProvider(new BouncyCastleProvider());
	}

	/**
	 * SM4 加密
	 * 
	 * @param input                 明文数据
	 * @param key                   密钥
	 * @param sm4ModeAndPaddingEnum 加密模式和padding模式
	 * @param iv                    初始向量(ECB模式下传NULL)
	 * @return
	 * @throws Exception
	 */
	public byte[] encrypt(byte[] input, byte[] key, SM4ModeAndPaddingEnum sm4ModeAndPaddingEnum, byte[] iv)
			throws Exception {
		return sm4(input, key, sm4ModeAndPaddingEnum, iv, Cipher.ENCRYPT_MODE);
	}

	/**
	 * SM4 解密
	 * 
	 * @param input                 密文数据
	 * @param key                   密钥
	 * @param sm4ModeAndPaddingEnum 加密模式和padding模式
	 * @param iv                    初始向量(ECB模式下传NULL)
	 * @return
	 * @throws Exception
	 */
	public byte[] decrypt(byte[] input, byte[] key, SM4ModeAndPaddingEnum sm4ModeAndPaddingEnum, byte[] iv)
			throws Exception {
		return sm4(input, key, sm4ModeAndPaddingEnum, iv, Cipher.DECRYPT_MODE);
	}

	private static byte[] sm4(byte[] input, byte[] key, SM4ModeAndPaddingEnum sm4ModeAndPaddingEnum, byte[] iv,
			int mode) throws Exception {
		IvParameterSpec ivParameterSpec = null;
		if (iv != null) {
			ivParameterSpec = new IvParameterSpec(iv);
		}
		SecretKeySpec sm4Key = new SecretKeySpec(key, "SM4");
		Cipher cipher = Cipher.getInstance(sm4ModeAndPaddingEnum.getName(), BouncyCastleProvider.PROVIDER_NAME);
		if (ivParameterSpec == null) {
			cipher.init(mode, sm4Key);
		} else {
			cipher.init(mode, sm4Key, ivParameterSpec);
		}
		return cipher.doFinal(input);
	}
}

posted @ 2023-06-08 10:10  20201224吴卓航  阅读(30)  评论(0编辑  收藏  举报