001package top.cenze.utils.crypt.sm.sm4; 002 003/** 004 * Created by $(USER) on $(DATE) 005 */ 006 007import org.apache.commons.codec.binary.Base64; 008import top.cenze.utils.ConvertUtil; 009 010import java.util.regex.Matcher; 011import java.util.regex.Pattern; 012 013public class SM4Utils { 014// private String secretKey = ""; 015// private String iv = ""; 016// private boolean hexString = false; 017 018 public String secretKey = ""; 019 public String iv = ""; 020 public boolean hexString = false; 021 022 public SM4Utils() { 023 } 024 025 026 public String encryptData_ECB(String plainText) { 027 try { 028 SM4_Context ctx = new SM4_Context(); 029 ctx.isPadding = true; 030 ctx.mode = SM4.SM4_ENCRYPT; 031 032 byte[] keyBytes; 033 if (hexString) { 034 keyBytes = ConvertUtil.hexStringToBytes(secretKey); 035 } else { 036 //keyBytes = secretKey.getBytes(); 037 keyBytes = ConvertUtil.hexStringToBytes(secretKey); 038 } 039 040 SM4 sm4 = new SM4(); 041 sm4.sm4_setkey_enc(ctx, keyBytes); 042 byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainText.getBytes("UTF-8")); 043 return ConvertUtil.byteToHex(encrypted); 044 } catch (Exception e) { 045 e.printStackTrace(); 046 return null; 047 } 048 } 049 050 public String decryptData_ECB(String cipherText) { 051 try { 052 byte[] encrypted = ConvertUtil.hexToByte(cipherText); 053 cipherText= Base64.encodeBase64String(encrypted);; 054 //cipherText = new BASE64Encoder().encode(encrypted); 055 if (cipherText != null && cipherText.trim().length() > 0) { 056 Pattern p = Pattern.compile("\\s*|\t|\r|\n"); 057 Matcher m = p.matcher(cipherText); 058 cipherText = m.replaceAll(""); 059 } 060 061 SM4_Context ctx = new SM4_Context(); 062 ctx.isPadding = true; 063 ctx.mode = SM4.SM4_DECRYPT; 064 065 byte[] keyBytes; 066 if (hexString) { 067 keyBytes = ConvertUtil.hexStringToBytes(secretKey); 068 } else { 069 keyBytes = secretKey.getBytes(); 070 } 071 072 SM4 sm4 = new SM4(); 073 sm4.sm4_setkey_dec(ctx, keyBytes); 074 byte[] decrypted = sm4.sm4_crypt_ecb(ctx, Base64.decodeBase64(cipherText)); 075 //byte[] decrypted = sm4.sm4_crypt_ecb(ctx, new BASE64Decoder().decodeBuffer(cipherText)); 076 return new String(decrypted, "UTF-8"); 077 } catch (Exception e) { 078 e.printStackTrace(); 079 return null; 080 } 081 } 082 083 public String encryptData_CBC(String plainText) { 084 try { 085 SM4_Context ctx = new SM4_Context(); 086 ctx.isPadding = true; 087 ctx.mode = SM4.SM4_ENCRYPT; 088 089 byte[] keyBytes; 090 byte[] ivBytes; 091 if (hexString) { 092 keyBytes = ConvertUtil.hexStringToBytes(secretKey); 093 ivBytes = ConvertUtil.hexStringToBytes(iv); 094 } else { 095 keyBytes = secretKey.getBytes(); 096 ivBytes = iv.getBytes(); 097 } 098 099 SM4 sm4 = new SM4(); 100 sm4.sm4_setkey_enc(ctx, keyBytes); 101 byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, plainText.getBytes("UTF-8")); 102 return ConvertUtil.byteToHex(encrypted); 103 } catch (Exception e) { 104 e.printStackTrace(); 105 return null; 106 } 107 } 108 109 public String decryptData_CBC(String cipherText) { 110 try { 111 byte[] encrypted = ConvertUtil.hexToByte(cipherText); 112 cipherText= Base64.encodeBase64String(encrypted);; 113 //cipherText = new BASE64Encoder().encode(encrypted); 114 if (cipherText != null && cipherText.trim().length() > 0) { 115 Pattern p = Pattern.compile("\\s*|\t|\r|\n"); 116 Matcher m = p.matcher(cipherText); 117 cipherText = m.replaceAll(""); 118 } 119 SM4_Context ctx = new SM4_Context(); 120 ctx.isPadding = true; 121 ctx.mode = SM4.SM4_DECRYPT; 122 123 byte[] keyBytes; 124 byte[] ivBytes; 125 if (hexString) { 126 keyBytes = ConvertUtil.hexStringToBytes(secretKey); 127 ivBytes = ConvertUtil.hexStringToBytes(iv); 128 } else { 129 keyBytes = secretKey.getBytes(); 130 ivBytes = iv.getBytes(); 131 } 132 133 SM4 sm4 = new SM4(); 134 sm4.sm4_setkey_dec(ctx, keyBytes); 135 //byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, new BASE64Decoder().decodeBuffer(cipherText)); 136 byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, Base64.decodeBase64(cipherText)); 137 /*String text = new String(decrypted, "UTF-8"); 138 return text.substring(0,text.length()-1);*/ 139 return new String(decrypted, "UTF-8"); 140 } catch (Exception e) { 141 e.printStackTrace(); 142 return null; 143 } 144 } 145 146// public static void main(String[] args) throws IOException { 147// String plainText = "I Love You Every Day"; 148// String s = ConvertUtil.byteToHex(plainText.getBytes()); 149// System.out.println("原文" + s); 150// SM4Utils sm4 = new SM4Utils(); 151// //sm4.secretKey = "JeF8U9wHFOMfs2Y8"; 152// sm4.secretKey = "64EC7C763AB7BF64E2D75FF83A319918"; 153// sm4.hexString = true; 154// 155// System.out.println("ECB模式加密"); 156// String cipherText = sm4.encryptData_ECB(plainText); 157// System.out.println("密文: " + cipherText); 158// System.out.println(""); 159// 160// String plainText2 = sm4.decryptData_ECB(cipherText); 161// System.out.println("明文: " + plainText2); 162// System.out.println(""); 163// 164// System.out.println("CBC模式加密"); 165// sm4.iv = "31313131313131313131313131313131"; 166// String cipherText2 = sm4.encryptData_CBC(plainText); 167// System.out.println("加密密文: " + cipherText2); 168// System.out.println(""); 169// 170// String plainText3 = sm4.decryptData_CBC(cipherText2); 171// System.out.println("解密明文: " + plainText3); 172// 173// } 174}