Sm4Util.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package com.xunmei.system.util;
  2. import org.apache.commons.codec.binary.Hex;
  3. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  4. import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
  5. import javax.crypto.Cipher;
  6. import javax.crypto.KeyGenerator;
  7. import javax.crypto.spec.SecretKeySpec;
  8. import java.io.UnsupportedEncodingException;
  9. import java.net.URLDecoder;
  10. import java.net.URLEncoder;
  11. import java.nio.charset.StandardCharsets;
  12. import java.security.Key;
  13. import java.security.SecureRandom;
  14. import java.security.Security;
  15. public class Sm4Util {
  16. static {
  17. Security.addProvider(new BouncyCastleProvider());
  18. }
  19. private static final String ENCODING = "UTF-8";
  20. public static final String ALGORITHM_NAME = "SM4";
  21. // 加密算法/分组加密模式/分组填充方式
  22. // PKCS5Padding-以8个字节为一组进行分组加密
  23. // 定义分组加密模式使用:PKCS5Padding
  24. public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";
  25. // 128-32位16进制;256-64位16进制
  26. public static final int DEFAULT_KEY_SIZE = 128;
  27. /**
  28. * 自动生成密钥
  29. *
  30. * @return
  31. * @explain
  32. */
  33. public static String generateKey() throws Exception {
  34. return new String(Hex.encodeHex(generateKey(DEFAULT_KEY_SIZE),false));
  35. }
  36. /**
  37. * @param keySize
  38. * @return
  39. * @throws Exception
  40. * @explain
  41. */
  42. public static byte[] generateKey(int keySize) throws Exception {
  43. KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME);
  44. kg.init(keySize, new SecureRandom());
  45. return kg.generateKey().getEncoded();
  46. }
  47. /**
  48. * 生成ECB暗号
  49. *
  50. * @param algorithmName 算法名称
  51. * @param mode 模式
  52. * @param key
  53. * @return
  54. * @throws Exception
  55. * @explain ECB模式(电子密码本模式:Electronic codebook)
  56. */
  57. private static Cipher generateEcbCipher(String algorithmName, int mode, byte[] key) throws Exception {
  58. Cipher cipher = Cipher.getInstance(algorithmName, BouncyCastleProvider.PROVIDER_NAME);
  59. Key sm4Key = new SecretKeySpec(key, ALGORITHM_NAME);
  60. cipher.init(mode, sm4Key);
  61. return cipher;
  62. }
  63. /**
  64. * sm4加密
  65. *
  66. * @param hexKey 16进制密钥(忽略大小写)
  67. * @param paramStr 待加密字符串
  68. * @return 返回16进制的加密字符串
  69. * @explain 加密模式:ECB
  70. * 密文长度不固定,会随着被加密字符串长度的变化而变化
  71. */
  72. public static String encryptEcb(String hexKey, String paramStr) {
  73. try {
  74. String cipherText = "";
  75. // 16进制字符串-->byte[]
  76. byte[] keyData = ByteUtils.fromHexString(hexKey);
  77. // String-->byte[]
  78. byte[] srcData = paramStr.getBytes(ENCODING);
  79. // 加密后的数组
  80. byte[] cipherArray = encrypt_Ecb_Padding(keyData, srcData);
  81. // byte[]-->hexString
  82. cipherText = ByteUtils.toHexString(cipherArray);
  83. URLEncoder.encode(cipherText, StandardCharsets.UTF_8.toString());
  84. return cipherText;
  85. } catch (Exception e) {
  86. return paramStr;
  87. }
  88. }
  89. /**
  90. * sm4加密
  91. *
  92. * @param hexKey 16进制密钥(忽略大小写)
  93. * @param paramStr 待加密字符串
  94. * @return 返回16进制的加密字符串
  95. * @explain 加密模式:ECB
  96. * 密文长度不固定,会随着被加密字符串长度的变化而变化
  97. */
  98. public static String encryptEcb(byte[] hexKey, String paramStr) {
  99. try {
  100. String cipherText = "";
  101. // 16进制字符串-->byte[]
  102. byte[] keyData = hexKey;
  103. // String-->byte[]
  104. byte[] srcData = paramStr.getBytes(ENCODING);
  105. // 加密后的数组
  106. byte[] cipherArray = encrypt_Ecb_Padding(keyData, srcData);
  107. // byte[]-->hexString
  108. cipherText = ByteUtils.toHexString(cipherArray);
  109. return cipherText;
  110. } catch (Exception e) {
  111. return paramStr;
  112. }
  113. }
  114. /**
  115. * 加密模式之Ecb
  116. *
  117. * @param key
  118. * @param data
  119. * @return
  120. * @throws Exception
  121. * @explain
  122. */
  123. public static byte[] encrypt_Ecb_Padding(byte[] key, byte[] data) throws Exception {
  124. Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.ENCRYPT_MODE, key);
  125. return cipher.doFinal(data);
  126. }
  127. /**
  128. * sm4解密
  129. *
  130. * @param hexKey 16进制密钥
  131. * @param cipherText 16进制的加密字符串(忽略大小写)
  132. * @return 解密后的字符串
  133. * @throws Exception
  134. * @explain 解密模式:采用ECB
  135. */
  136. public static String decryptEcb(String hexKey, String cipherText) {
  137. try {
  138. cipherText = URLDecoder.decode(cipherText,StandardCharsets.UTF_8.toString());
  139. } catch (UnsupportedEncodingException e) {
  140. e.printStackTrace();
  141. }
  142. // 用于接收解密后的字符串
  143. String decryptStr = "";
  144. // hexString-->byte[]
  145. byte[] keyData = ByteUtils.fromHexString(hexKey);
  146. // hexString-->byte[]
  147. byte[] cipherData = ByteUtils.fromHexString(cipherText);
  148. // 解密
  149. byte[] srcData = new byte[0];
  150. try {
  151. srcData = decrypt_Ecb_Padding(keyData, cipherData);
  152. // byte[]-->String
  153. decryptStr = new String(srcData, ENCODING);
  154. } catch (Exception e) {
  155. e.printStackTrace();
  156. }
  157. return decryptStr;
  158. }
  159. /**
  160. * 解密
  161. *
  162. * @param key
  163. * @param cipherText
  164. * @return
  165. * @throws Exception
  166. * @explain
  167. */
  168. public static byte[] decrypt_Ecb_Padding(byte[] key, byte[] cipherText) throws Exception {
  169. Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.DECRYPT_MODE, key);
  170. return cipher.doFinal(cipherText);
  171. }
  172. public static void main(String[] args) {
  173. String s = encryptEcb("51d95b1dc43a9faaad0570f81c755fcf", "update core_edu_training_task set status=2 where id='1812463465882353673';");
  174. System.out.println(s);
  175. String s1 = decryptEcb("51d95b1dc43a9faaad0570f81c755fcf", s);
  176. System.out.println(s1);
  177. }
  178. }