远方蔚蓝
一刹那情真,相逢不如不见

文章数量 126

访问次数 199825

运行天数 1437

最近活跃 2024-10-04 23:36:48

进入后台管理系统

java使用SM4工具类


  1. package wst.st.site.tools;
  2. import java.security.Key;
  3. import java.security.SecureRandom;
  4. import java.security.Security;
  5. import java.util.Arrays;
  6. import javax.crypto.Cipher;
  7. import javax.crypto.KeyGenerator;
  8. import javax.crypto.spec.SecretKeySpec;
  9. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  10. import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
  11. /**
  12. * SM4加解密工具
  13. * @author wst 2023年8月22日 下午5:31:36
  14. *
  15. */
  16. public class SM4Util {
  17. // 编码
  18. private static final String ENCODING = "UTF-8";
  19. // 加密名称
  20. public static final String ALGORIGTHM_NAME = "SM4";
  21. // 加密分组方式
  22. public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS7Padding";
  23. // KEY长度
  24. public static final int DEFAULT_KEY_SIZE = 128;
  25. public SM4Util() {
  26. }
  27. static {
  28. Security.addProvider(new BouncyCastleProvider());
  29. }
  30. // 生成ecb暗号
  31. private static Cipher generateEcbCipher(String algorithmName, int mode, byte[] key) throws Exception {
  32. Cipher cipher = Cipher.getInstance(algorithmName, BouncyCastleProvider.PROVIDER_NAME);
  33. Key sm4Key = new SecretKeySpec(key, ALGORIGTHM_NAME);
  34. cipher.init(mode, sm4Key);
  35. return cipher;
  36. }
  37. // 生成密钥
  38. public static byte[] generateKey() throws Exception {
  39. return generateKey(DEFAULT_KEY_SIZE);
  40. }
  41. public static byte[] generateKey(int keySize) throws Exception {
  42. KeyGenerator kg = KeyGenerator.getInstance(ALGORIGTHM_NAME, BouncyCastleProvider.PROVIDER_NAME);
  43. kg.init(keySize, new SecureRandom());
  44. return kg.generateKey().getEncoded();
  45. }
  46. // 加密
  47. public static String encryptEcb(String hexKey, String paramStr, String charset) throws Exception {
  48. String cipherText = "";
  49. if (null != paramStr && !"".equals(paramStr)) {
  50. byte[] keyData = ByteUtils.fromHexString(hexKey);
  51. charset = charset.trim();
  52. if (charset.length() <= 0) {
  53. charset = ENCODING;
  54. }
  55. byte[] srcData = paramStr.getBytes(charset);
  56. byte[] cipherArray = encrypt_Ecb_Padding(keyData, srcData);
  57. cipherText = ByteUtils.toHexString(cipherArray);
  58. }
  59. return cipherText;
  60. }
  61. // 加密模式之ecb
  62. public static byte[] encrypt_Ecb_Padding(byte[] key, byte[] data) throws Exception {
  63. Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.ENCRYPT_MODE, key);
  64. byte[] bs = cipher.doFinal(data);
  65. return bs;
  66. }
  67. // Sm4解密
  68. public static String decryptEcb(String hexKey, String cipherText, String charset) throws Exception {
  69. String decryptStr = "";
  70. byte[] keyData = ByteUtils.fromHexString(hexKey);
  71. byte[] cipherData = ByteUtils.fromHexString(cipherText);
  72. byte[] srcData = decrypt_Ecb_Padding(keyData, cipherData);
  73. charset = charset.trim();
  74. if (charset.length() <= 0) {
  75. charset = ENCODING;
  76. }
  77. decryptStr = new String(srcData, charset);
  78. return decryptStr;
  79. }
  80. // 解密
  81. public static byte[] decrypt_Ecb_Padding(byte[] key, byte[] cipherText) throws Exception {
  82. Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.DECRYPT_MODE, key);
  83. return cipher.doFinal(cipherText);
  84. }
  85. // 密码校验
  86. public static boolean verifyEcb(String hexKey, String cipherText, String paramStr) throws Exception {
  87. boolean flag = false;
  88. byte[] keyData = ByteUtils.fromHexString(hexKey);
  89. byte[] cipherData = ByteUtils.fromHexString(cipherText);
  90. byte[] decryptData = decrypt_Ecb_Padding(keyData, cipherData);
  91. byte[] srcData = paramStr.getBytes(ENCODING);
  92. flag = Arrays.equals(decryptData, srcData);
  93. return flag;
  94. }
  95. public static void main(String[] args) {
  96. try {
  97. String json = "{\"name\":\"Devil\"}";
  98. System.out.println("json: " + json);
  99. String key = ByteUtils.toHexString(SM4Util.generateKey());
  100. System.out.println("key: " + key);
  101. String encryptJson = SM4Util.encryptEcb(key, json, ENCODING);
  102. System.out.println("encryptJson: " + encryptJson);
  103. System.out.println(SM4Util.verifyEcb(key, encryptJson, json));
  104. String decryptJson = SM4Util.decryptEcb(key, encryptJson, ENCODING);
  105. System.out.println("decryptJson: " + decryptJson);
  106. } catch (Exception e) {
  107. e.printStackTrace();
  108. }
  109. }
  110. }