| | |
| | | import org.bouncycastle.crypto.paddings.PKCS7Padding; |
| | | |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.security.SecureRandom; |
| | | import java.util.Arrays; |
| | | import java.util.Base64; |
| | | |
| | | public class SM4Util { |
| | | |
| | | |
| | | /** |
| | | * 生成一个随机的SM4加密密钥。 |
| | | * |
| | | * SM4算法是一种对称加密算法,其密钥长度为128位(16字节)。 |
| | | * 本方法使用安全随机数生成器生成一个16字节的随机值作为SM4密钥, |
| | | * 并将其转换为十六进制字符串格式以便于使用和存储。 |
| | | * |
| | | * @return 返回一个表示SM4密钥的十六进制字符串。 |
| | | */ |
| | | public static String generateSM4Key() { |
| | | SecureRandom secureRandom = new SecureRandom(); |
| | | byte[] sm4Key = new byte[8]; // SM4 密钥长度为 128 位(16 字节) |
| | | secureRandom.nextBytes(sm4Key); |
| | | String key = bytesToHex(sm4Key); |
| | | return key; |
| | | } |
| | | |
| | | |
| | | private static String bytesToHex(byte[] bytes) { |
| | | StringBuilder sb = new StringBuilder(); |
| | | for (byte b : bytes) { |
| | | sb.append(String.format("%02x", b)); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | /** |
| | | * 使用SM4算法加密输入数据。 |
| | | * |
| | | * @param key 加密密钥,应符合SM4算法的密钥长度要求。 |
| | | * @param input 待加密的输入数据。 |
| | | * @return 加密后的数据,以Base64编码形式表示。 |
| | | */ |
| | | public static String encrypt(byte[] key, byte[] input) { |
| | | // 创建并初始化SM4加密器 |
| | | PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new SM4Engine(), new PKCS7Padding()); |
| | | cipher.init(true, new KeyParameter(key)); // ECB模式下不需要IV |
| | | |
| | | // 用于存储加密后的数据 |
| | | byte[] output = new byte[cipher.getOutputSize(input.length)]; |
| | | // 处理输入数据,进行加密 |
| | | int length = cipher.processBytes(input, 0, input.length, output, 0); |
| | | |
| | | // 完成加密过程,处理可能剩余的数据 |
| | | try { |
| | | length += cipher.doFinal(output, length); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | // 使用Base64编码加密后的字节数组,并返回 |
| | | String encoded = Base64.getEncoder().encodeToString(Arrays.copyOf(output, length)); |
| | | return encoded; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 使用SM4算法对输入数据进行解密 |
| | | * |
| | | * @param key 密钥字节数组,用于解密数据 |
| | | * @param input 待解密的字节数组 |
| | | * @return 解密后的字节数组 |
| | | */ |
| | | public static byte[] decrypt(byte[] key, byte[] input) { |
| | | // 创建基于SM4算法的缓冲分组密码对象 |
| | | PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new SM4Engine(), new PKCS7Padding()); |
| | | // 初始化密码对象为解密模式,并设置密钥参数 |
| | | cipher.init(false, new KeyParameter(key)); // ECB模式下不需要IV |
| | | // 根据输入数据长度计算解密后的字节数组长度 |
| | | byte[] output = new byte[cipher.getOutputSize(input.length)]; |
| | | // 处理字节数组,将输入数据解密到输出字节数组中 |
| | | int length = cipher.processBytes(input, 0, input.length, output, 0); |
| | | try { |
| | | // 完成最终的解密操作,并将长度累加到已处理字节数组中 |
| | | length += cipher.doFinal(output, length); |
| | | } catch (Exception e) { |
| | | // 捕获并处理解密过程中的异常 |
| | | e.printStackTrace(); |
| | | } |
| | | // 返回解密后的字节数组 |
| | | return Arrays.copyOf(output, length); |
| | | } |
| | | |
| | | public static String decrypt(String keys,String secert){ |
| | | byte[] key = keys.getBytes(StandardCharsets.UTF_8); |
| | | byte[] ming= Base64.getDecoder().decode(secert); |
| | | byte[] text=decrypt(key,ming); |
| | | return new String(text, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | public static String encrypt(String keys, String input){ |
| | | byte[] key = keys.getBytes(StandardCharsets.UTF_8); |
| | | return encrypt(key,input.getBytes()); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | byte[] key = "jsimjrby3wqb7dbq".getBytes(StandardCharsets.UTF_8); // 16字节密钥 |
| | | |
| | | // 原始明文 |
| | | String plaintext = "hello"; |
| | | String plaintext = "{\n" + |
| | | " \"deviceid\": [\n" + |
| | | " \"1581F6QAD241800B6V95\",\n" + |
| | | " \"1581F5BMD22CK0014H2U\"\n" + |
| | | " ],\n" + |
| | | " \"bizidname\": \"南昌市西湖区图斑下发\",\n" + |
| | | " \"tasklist\": [\n" + |
| | | " {\n" + |
| | | " \"bsm\": \"2028107171717717107171017\",\n" + |
| | | " \"xzqdm\": \"310000\",\n" + |
| | | " \"dkbh\": \"ndbg201102U0172710711\",\n" + |
| | | " \"dklx\": \"ndbg2024\",\n" + |
| | | " \"dkmc\": \"小桥头\",\n" + |
| | | " \"dkmj\": 20.5,\n" + |
| | | " \"dkfw\": \"MULTIPOLYGON(((115.86528871951153 28.625287925196325,115.86561708513025 28.625787612007546,115.86815834948467 28.624602640426623,115.86773004650362 28.62425999804172,115.86528871951153 28.625287925196325)))\",\n" + |
| | | " \"bz\": null\n" + |
| | | " },\n" + |
| | | " {\n" + |
| | | " \"bsm\": \"207\",\n" + |
| | | " \"xzqdm\": \"31000\",\n" + |
| | | " \"dkbh\": \"n1\",\n" + |
| | | " \"dklx\": \"n4\",\n" + |
| | | " \"dkmc\": \"小桥\",\n" + |
| | | " \"dkmj\": 20.5,\n" + |
| | | " \"dkfw\": \"MULTIPOLYGON(((115.86262525992014 28.634623244731678,115.86214341906647 28.63383445340825,115.86164016306374 28.63418423417594,115.86204348170406 28.63485524217979,115.86262525992014 28.634623244731678)))\",\n" + |
| | | " \"bz\": null\n" + |
| | | " },\n" + |
| | | " {\n" + |
| | | " \"bsm\": \"20281071717177177\",\n" + |
| | | " \"xzqdm\": \"31000\",\n" + |
| | | " \"dkbh\": \"ndbg20110210711\",\n" + |
| | | " \"dklx\": \"ndbg2774\",\n" + |
| | | " \"dkmc\": \"桥头\",\n" + |
| | | " \"dkmj\": 20.574,\n" + |
| | | " \"dkfw\": \"MULTIPOLYGON(((115.86528871951153 28.625287925196325,115.86561708513025 28.625787612007546,115.86815834948467 28.624602640426623,115.86773004650362 28.62425999804172,115.86528871951153 28.625287925196325)))\",\n" + |
| | | " \"bz\": null\n" + |
| | | " },\n" + |
| | | " {\n" + |
| | | " \"bsm\": \"177177\",\n" + |
| | | " \"xzqdm\": \"3\",\n" + |
| | | " \"dkbh\": \"n1\",\n" + |
| | | " \"dklx\": \"ndbg2774\",\n" + |
| | | " \"dkmc\": \"大桥头\",\n" + |
| | | " \"dkmj\": 20.54,\n" + |
| | | " \"dkfw\": \"MULTIPOLYGON(((115.84891670917342 28.62884019080562,115.84861068074487 28.628310237250474,115.84801105657137 28.628543424429186,115.84836943314201 28.629060290948733,115.84891670917342 28.62884019080562)))\",\n" + |
| | | " \"bz\": null\n" + |
| | | " },\n" + |
| | | " {\n" + |
| | | " \"bsm\": \"177177\",\n" + |
| | | " \"xzqdm\": \"3\",\n" + |
| | | " \"dkbh\": \"n1\",\n" + |
| | | " \"dklx\": \"ndbg2774\",\n" + |
| | | " \"dkmc\": \"大桥头\",\n" + |
| | | " \"dkmj\": 20.54,\n" + |
| | | " \"dkfw\": \"MULTIPOLYGON(((115.85261028414699 28.628065910491646,115.85234952636368 28.627469892700983,115.85043542990357 28.628240439727712,115.85050295853954 28.62842805760428,115.85071376127738 28.628739229859185,115.85080329411801 28.628869938164858,115.85261028414699 28.628065910491646)))\",\n" + |
| | | " \"bz\": null\n" + |
| | | " }\n" + |
| | | ",\n" + |
| | | " {\n" + |
| | | " \"bsm\": \"177177\",\n" + |
| | | " \"xzqdm\": \"3\",\n" + |
| | | " \"dkbh\": \"n1\",\n" + |
| | | " \"dklx\": \"ndbg2774\",\n" + |
| | | " \"dkmc\": \"大桥头\",\n" + |
| | | " \"dkmj\": 20.54,\n" + |
| | | " \"dkfw\": \"MULTIPOLYGON(((115.85063001998248 28.61939504315339,115.85047227189142 28.619025124342414,115.8490255596 28.6190394011085,115.8491588094162 28.61949625762165,115.84966325514971 28.61989600707048,115.84993419451101 28.620007388652688,115.85063001998248 28.61939504315339)))\",\n" + |
| | | " \"bz\": null\n" + |
| | | " }\n" + |
| | | " ]\n" + |
| | | "}\n"; |
| | | String secert =encrypt(key,plaintext.getBytes()); |
| | | // System.out.println(secert); |
| | | |
| | | System.out.println("Encoded: " + encrypt(key, plaintext.getBytes(StandardCharsets.UTF_8))); |
| | | |
| | | // 解码并解密 |
| | | byte[] decoded = Base64.getDecoder().decode(encrypt(key, plaintext.getBytes(StandardCharsets.UTF_8))); |
| | | byte[] ming= Base64.getDecoder().decode(secert); |
| | | byte[] text=decrypt(key,ming); |
| | | // System.out.println(new String(text, StandardCharsets.UTF_8)); |
| | | System.out.println(encrypt("jsimjrby3wqb7dbq",plaintext)); |
| | | // System.out.println(generateSM4Key()); |
| | | // System.out.println("封装"+decrypt("8a217699714b0fc0","N0KrVFiFdPlpoRF/pugsgOFjDSymxnhUHe9xbuF4XhO3/uVYC83dCixg9estZC67YdP3R/H9oGQ3uDV1OuSV/dYTnli8nb6hMBCcMhcGMeY0UOoWL00tMOqJMaQQ903C9BaozQ1AjdUu1zaA3uisNdkj3O0jMBZRv7JVWuOYpjaPAElRehLFf25wbDxjAtz8p13OWoUZuCfawcfL2B45H2OOUFzr/a91X26NJvkkRrr4dEngeubTldC53JXF+poD92xfw0f9e+7weLcB4QR01F2mpCNTW4nEdj/aBQxoLtt+zVYOBWFSEBsqu5ExdpRxpRTyN7Sh1inEUVYN0f9OT3mzeF8oaRe/AyI2I50dQrh1pBvNH7uCzjPtSrl8HvhKzv5l+G++3VFb2fDWqconrmt9AOCyxLhOkso6yC8MH9ZtEAVhB20LLVYN0qQrdZHIzJNXgWTDXlTVcPcGUjkTYbKYDk8q/+ToLg7BNCsMVKjwCE/ehbS4aU/3/M3rdXlRxXSuNylT1MZYKy1giRbkVnVHHl4Dw4ZYe5ydm6y1VRS1fcAkEhlcKrmgHGcWDf0i9mBjUqXGRs0ZmD7gsTXDf5/I4Pwhwk7VAJAIQqYElOWnopE4uzjIsY/NzZ6PnBsmLwBuwSvOL+ooZbRTenfmWK7r42dxJN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XJlrli3gMIpTR8lYgfOVGFBi5lIIFAakKMEL4cu/odxCCLEoqYQAkO53hyi9zJ5fO/aaKH7k7CTUgviLLofJUgh9aaHdR1MNpIx+dfO4g3TPVnofCb7/PPOa9U0U87wjb7kTGRx5yK494qfslNofwpRZa9bKT3aZl04XVVQ/v1/zTs49VqxWnv2C0uwg+KT4HWcA5+UyXy2uC8LLBmdYI3h8qmzG57K50wL/AJ7ZMN/MFeKD8ulg1CPupAJxlduH/Zu40F6y1fFqinQfqeVOhXX7lNJoWEycdo0XGsVGq3kdEay0mpLVHUPQVW+wDxN/O7tWdJqS1R1D0FVvsA8Tfzu7VmQyL9Eq3f7OvmqAdFHzQkFQFyd4KpMAlWySab4tCbL+4YVOYfaRysmmui2NZdUW2GcFtceaROR6KNL0KeROxrehgxZeKBn25gneLP9G40kiXhkGRk0ufZ2DGBoHQ7/y9DTwVYGonvJBsJcvM4Kg5Abhy25CzIH75dRVThNHSiWaVAao9yMrQEQFz4PJwysIJnu1jYkrCvQ32t+NkBMN5nZOj8XEyOqtaYh9mVl0oeAfz/XdFozHlCljAH4yzLwtyMQOaZqc7IoYnHbejZdI0chNiZteN46775aU0MC+weqGLYUq4ehBXxq3Yd5ds2Tx+RcxI+TvCDmADKf+19KK5owV2APRlFTvPv6tyO8vu/c0EvSEPMPotb7wtVEOuBGd7+iJ5aKPkiG62ikXLqYAjbDe238+pAN1hCVRcUEkdmzhgN7MdMdB1OSJJ+CC4Om7GZv2xRIYYE3cEOrHCIYlRCT7d5TCatHmJN5Y7HEg2AgSl2/6CoCZbGQaCj8qsqnS7I=")); |
| | | String encryptedBase64 = encrypt(key, plaintext.getBytes(StandardCharsets.UTF_8)); |
| | | // System.out.println("Encrypted (Base64): " + encryptedBase64); |
| | | // System.out.println(generateSM4Key()); |
| | | // 直接解密 |
| | | byte[] decoded = Base64.getDecoder().decode(encryptedBase64); |
| | | byte[] decrypted = decrypt(key, decoded); |
| | | |
| | | // 输出结果 |
| | | System.out.println("Plaintext: " + plaintext); |
| | | System.out.println("Encrypted (Base64): " + encrypt(key, plaintext.getBytes(StandardCharsets.UTF_8))); |
| | | System.out.println("Decrypted: " + new String(decrypted, StandardCharsets.UTF_8)); |
| | | // // 输出结果 |
| | | // System.out.println("Plaintext: " + plaintext); |
| | | // System.out.println("Decrypted: " + new String(decrypted, StandardCharsets.UTF_8)); |
| | | } |
| | | } |