本文主要是介绍【加密算法】有关国密4加密算法的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
今天在对接乐企的时候,需要用到国密4算法对入参进行加密 之后对出参进行解密,乐企平台提供了算法实现,具体如下:
import org.apache.commons.lang3.ObjectUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.bouncycastle.util.encoders.Hex;import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Base64;/*** User: yanjun.hou* Date: 2024/8/27 09:51* Description:*/
public class SM4Util {static {Security.addProvider(new BouncyCastleProvider());}private static final Charset ENCODING = StandardCharsets.UTF_8;public static final String ALGORITHM_NAME = "SM4";// 加密算法/分组加密模式/分组填充方式// PKCS5Padding-以8个字节为一组进行分组加密// 定义分组加密模式使用:PKCS5Paddingpublic static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";/*** 生成ECB暗号** @param algorithmName 算法名称* @param mode 模式* @param key 密码* @explain ECB模式(电子密码本模式:Electronic codebook)*/private static Cipher generateEcbCipher(String algorithmName, int mode, byte[] key) throws Exception {Cipher cipher = Cipher.getInstance(algorithmName, BouncyCastleProvider.PROVIDER_NAME);Key sm4Key = new SecretKeySpec(key, ALGORITHM_NAME);cipher.init(mode, sm4Key);return cipher;}/*** sm4加密** @param hexKey 16进制密钥(忽略大小写)* @param paramStr 待加密字符串* @return 返回Base64后加密字符串* @explain 加密模式:ECB*/public static String encryptEcb(String hexKey, String paramStr) throws Exception {// 16进制字符串-->byte[]byte[] keyData = ByteUtils.fromHexString(hexKey);// String-->byte[]byte[] srcData = paramStr.getBytes(ENCODING);// 加密后的数组byte[] cipherArray = encrypt_Ecb_Padding(keyData, srcData);// byte[]-->hexStringString cipherText = Base64.getEncoder().encodeToString(cipherArray);return cipherText;}/*** 加密模式为ECB** @param key 2进制密钥* @param data 2进制原文* @return 二进制密文*/public static byte[] encrypt_Ecb_Padding(byte[] key, byte[] data) throws Exception {Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.ENCRYPT_MODE, key);return cipher.doFinal(data);}/*** sm4解密** @param hexKey 16进制密钥* @param cipherText 16进制的加密字符串(忽略大小写)* @return 解密后的字符串* @explain 解密模式:采用ECB*/@SuppressWarnings("UnnecessaryLocalVariable")public static String decryptEcb(String hexKey, String cipherText) throws Exception {// hexString-->byte[]byte[] keyData = ByteUtils.fromHexString(hexKey);// hexString-->byte[]byte[] cipherData = Base64.getDecoder().decode(cipherText);// 解密byte[] srcData = decrypt_Ecb_Padding(keyData, cipherData);// 接收解密后的字符串 byte[]-->StringString decryptStr = new String(srcData, ENCODING);return decryptStr;}/*** sm4解密** @param key 2进制密钥* @param cipherText 2进制密文* @return 解密后的2进制原文*/public static byte[] decrypt_Ecb_Padding(byte[] key, byte[] cipherText) throws Exception {Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.DECRYPT_MODE, key);return cipher.doFinal(cipherText);}
}
这篇关于【加密算法】有关国密4加密算法的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!