国密sm4加解密算法工具类,可用于生产环境
package com.example.demo.endecryption.utils;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.Security;
public class Sm4Util {
public enum Algorithm {
SM4("SM4","SM4","key长度:16 byte");
private String keyAlgorithm;
private String transformation;
private String description;
Algorithm(String keyAlgorithm, String transformation, String description) {
this.keyAlgorithm = keyAlgorithm;
this.transformation = transformation;
this.description = description;
}
public String getKeyAlgorithm() {
return this.keyAlgorithm;
}
public String getTransformation() {
return this.transformation;
}
public String getDescription() {
return this.description;
}
}
private static final String PROVIDER_NAME = "BC";
static {
Security.addProvider(new BouncyCastleProvider());
}
public static SecretKey genKeyByStr(Algorithm algorithm, String keyStr, Charset charset) {
return readKeyFromBytes(algorithm, keyStr.getBytes(charset));
}
public static SecretKey readKeyFromBytes(Sm4Util.Algorithm algorithm, byte[] keyBytes) {
return new SecretKeySpec(keyBytes, algorithm.getKeyAlgorithm());
}
public static String encryptBase64(Sm4Util.Algorithm algorithm, SecretKey key, String data, Charset charset) throws InvalidKeyException {
return Base64.encodeBase64String(encrypt(algorithm, key, data.getBytes(charset)));
}
public static byte[] encrypt(Sm4Util.Algorithm algorithm, SecretKey key, byte[] data) throws InvalidKeyException {
try {
return cipherDoFinal(algorithm, Cipher.ENCRYPT_MODE, key, data);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
}
}
private static byte[] cipherDoFinal(Sm4Util.Algorithm algorithm, int opmode, SecretKey key, byte[] data) throws InvalidKeyException, BadPaddingException {
Cipher cipher;
try {
cipher = Cipher.getInstance(algorithm.getTransformation(), PROVIDER_NAME);
} catch (Exception e) {
throw new RuntimeException(e);
}
cipher.init(opmode, key);
try {
return cipher.doFinal(data);
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
}
}
public static String decryptBase64(Sm4Util.Algorithm algorithm, SecretKey key, String data, Charset charset)
throws InvalidKeyException, BadPaddingException {
return new String(decrypt(algorithm, key, Base64.decodeBase64(data)), charset);
}
public static byte[] decrypt(Sm4Util.Algorithm algorithm, SecretKey key, byte[] data) throws InvalidKeyException, BadPaddingException {
return cipherDoFinal(algorithm, Cipher.DECRYPT_MODE, key, data);
}
}
测试
@Test
public void sm4Test() throws InvalidKeyException, BadPaddingException {
SymEncUtil.Algorithm algorithm = SymEncUtil.Algorithm.SM4;
String encryptKey = "0123456789ABCDEF";
Charset encryptCharset = StandardCharsets.UTF_8;
SecretKey key = SymEncUtil.genKeyByStr(algorithm, encryptKey, encryptCharset);
String encryptBase64 = SymEncUtil.encryptBase64(algorithm, key, "123456", encryptCharset);
System.out.println("encryptBase64 = " + encryptBase64);
String decryptBase64 = SymEncUtil.decryptBase64(algorithm, key, "QtrH8m/aR9x/cySEoUb+Nw==", encryptCharset);
System.out.println("decryptBase64 = " + decryptBase64);
}