SM4Utils.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package com.security.cipher.sm;
  2. import java.io.IOException;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. import Decoder.BASE64Decoder;
  6. import Decoder.BASE64Encoder;
  7. public class SM4Utils
  8. {
  9. private String secretKey = "";
  10. private String iv = "";
  11. private boolean hexString = true;
  12. public SM4Utils()
  13. {
  14. }
  15. public void SetSecretKey(String Key)
  16. {
  17. this.secretKey = Key;
  18. }
  19. public String encryptData_ECB(String plainText)
  20. {
  21. try
  22. {
  23. SM4_Context ctx = new SM4_Context();
  24. ctx.isPadding = false;
  25. ctx.mode = SM4.SM4_ENCRYPT;
  26. byte[] keyBytes;
  27. if (hexString)
  28. {
  29. keyBytes = Util.hexStringToBytes(secretKey);
  30. }
  31. else
  32. {
  33. keyBytes = secretKey.getBytes();
  34. }
  35. SM4 sm4 = new SM4();
  36. sm4.sm4_setkey_enc(ctx, keyBytes);
  37. byte[] encrypted = sm4.sm4_crypt_ecb(ctx, Util.hexStringToBytes(plainText));
  38. String cipherText = Util.encodeHexString(encrypted);
  39. //String cipherText = new String(encrypted);
  40. //Log.e("test1",cipherText);
  41. /*
  42. String cipherText = new BASE64Encoder().encode(encrypted);
  43. if (cipherText != null && cipherText.trim().length() > 0)
  44. {
  45. Pattern p = Pattern.compile("\\s*|\t|\r|\n");
  46. Matcher m = p.matcher(cipherText);
  47. cipherText = m.replaceAll("");
  48. }*/
  49. return cipherText;
  50. }
  51. catch (Exception e)
  52. {
  53. e.printStackTrace();
  54. return null;
  55. }
  56. }
  57. public String decryptData_ECB(String cipherText)
  58. {
  59. try
  60. {
  61. SM4_Context ctx = new SM4_Context();
  62. ctx.isPadding = false;
  63. ctx.mode = SM4.SM4_DECRYPT;
  64. byte[] keyBytes;
  65. if (hexString)
  66. {
  67. keyBytes = Util.hexStringToBytes(secretKey);
  68. }
  69. else
  70. {
  71. keyBytes = secretKey.getBytes();
  72. }
  73. SM4 sm4 = new SM4();
  74. sm4.sm4_setkey_dec(ctx, keyBytes);
  75. byte[] decrypted = sm4.sm4_crypt_ecb(ctx, Util.hexStringToBytes(cipherText));
  76. return Util.encodeHexString(decrypted);
  77. }
  78. catch (Exception e)
  79. {
  80. e.printStackTrace();
  81. return null;
  82. }
  83. }
  84. public String encryptData_CBC(String plainText)
  85. {
  86. try
  87. {
  88. SM4_Context ctx = new SM4_Context();
  89. ctx.isPadding = false;
  90. ctx.mode = SM4.SM4_ENCRYPT;
  91. byte[] keyBytes;
  92. byte[] ivBytes;
  93. if (hexString)
  94. {
  95. keyBytes = Util.hexStringToBytes(secretKey);
  96. ivBytes = Util.hexStringToBytes(iv);
  97. }
  98. else
  99. {
  100. keyBytes = secretKey.getBytes();
  101. ivBytes = iv.getBytes();
  102. }
  103. SM4 sm4 = new SM4();
  104. sm4.sm4_setkey_enc(ctx, keyBytes);
  105. byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, plainText.getBytes("GBK"));
  106. String cipherText = new BASE64Encoder().encode(encrypted);
  107. if (cipherText != null && cipherText.trim().length() > 0)
  108. {
  109. Pattern p = Pattern.compile("\\s*|\t|\r|\n");
  110. Matcher m = p.matcher(cipherText);
  111. cipherText = m.replaceAll("");
  112. }
  113. return cipherText;
  114. }
  115. catch (Exception e)
  116. {
  117. e.printStackTrace();
  118. return null;
  119. }
  120. }
  121. public String decryptData_CBC(String cipherText)
  122. {
  123. try
  124. {
  125. SM4_Context ctx = new SM4_Context();
  126. ctx.isPadding = false;
  127. ctx.mode = SM4.SM4_DECRYPT;
  128. byte[] keyBytes;
  129. byte[] ivBytes;
  130. if (hexString)
  131. {
  132. keyBytes = Util.hexStringToBytes(secretKey);
  133. ivBytes = Util.hexStringToBytes(iv);
  134. }
  135. else
  136. {
  137. keyBytes = secretKey.getBytes();
  138. ivBytes = iv.getBytes();
  139. }
  140. SM4 sm4 = new SM4();
  141. sm4.sm4_setkey_dec(ctx, keyBytes);
  142. byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, new BASE64Decoder().decodeBuffer(cipherText));
  143. return new String(decrypted, "GBK");
  144. }
  145. catch (Exception e)
  146. {
  147. e.printStackTrace();
  148. return null;
  149. }
  150. }
  151. /*
  152. public static void main(String[] args) throws IOException
  153. {
  154. String plainText = "abcd";
  155. SM4Utils sm4 = new SM4Utils();
  156. sm4.secretKey = "JeF8U9wHFOMfs2Y8";
  157. sm4.hexString = false;
  158. System.out.println("ECB模式");
  159. String cipherText = sm4.encryptData_ECB(plainText);
  160. System.out.println("密文: " + cipherText);
  161. System.out.println("");
  162. plainText = sm4.decryptData_ECB(cipherText);
  163. System.out.println("明文: " + plainText);
  164. System.out.println("");
  165. System.out.println("CBC模式");
  166. sm4.iv = "UISwD9fW6cFh9SNS";
  167. cipherText = sm4.encryptData_CBC(plainText);
  168. System.out.println("密文: " + cipherText);
  169. System.out.println("");
  170. plainText = sm4.decryptData_CBC(cipherText);
  171. System.out.println("明文: " + plainText);
  172. }*/
  173. public static void SM4_ECB_TEST() throws IOException {
  174. String plainText = "11223344556677888877665544332211";
  175. String sm4_Key = "00000000000000008888888888888888";
  176. SM4Utils sm4Utils = new SM4Utils();
  177. sm4Utils.SetSecretKey(sm4_Key);
  178. String encryptdata = sm4Utils.encryptData_ECB(plainText);
  179. //txt.setText(encryptdata);
  180. System.out.println("密文: " + encryptdata);
  181. String decryptdata = sm4Utils.decryptData_ECB(encryptdata);
  182. System.out.println("密文: " + decryptdata);
  183. }
  184. }