rain
2024-08-05 b912913c1a96c76cc2b43dd22e662c7d02b59c8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.dji.sample.droneairport.utils;
 
import org.bouncycastle.crypto.engines.SM4Engine;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.paddings.PKCS7Padding;
 
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
 
public class SM4Util {
    public static String encrypt(byte[] key, byte[] input) {
        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();
        }
        String encoded = Base64.getEncoder().encodeToString(Arrays.copyOf(output, length));
        return encoded;
    }
 
    public static byte[] decrypt(byte[] key, byte[] input) {
        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 = "{\n" +
                "\"regioncode\": \"650000\",\n" +
                "\"deviceid\": \"dh11111111111\",\n" +
                "\"brand\": \"大疆\",\n" +
                "\"model\": \"M3D\",\n" +
                "\"longitude\": 120.6670,\n" +
                "\"latitude\": 30.4567,\n" +
                "\"height\": 45,\n" +
                "\"radius\": 10000\n" +
                "}\n";
 
        String secert =encrypt(key,plaintext.getBytes());
        System.out.println(secert);
 
 
        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","hello"));
        System.out.println("封装"+decrypt("jsimjrby3wqb7dbq","MkzmRdHSy/pB4sdTjNHb4ngG0q7Z+vWzNKdJxJFkhw4Y9tI8R9sapL6ECLyVnKZwiZbmVQFtn+sT294nCVdskK8TUx7CCI3qo21d9I4ONl+XAYnlDCtEa8KSFK3CB3/D9zRt96zEDkv31Av2rdAJW9O7Le1Z13wWGmsEqHjjrXLv5jd/Cop6+BB9+lqDRlhx+ubNOFdUgJALe4wHjZPCMdE0ob44Hx+1iLg7m/WJQWc="));
        // 直接加密
        String encryptedBase64 = encrypt(key, plaintext.getBytes(StandardCharsets.UTF_8));
//        System.out.println("Encrypted (Base64): " + encryptedBase64);
 
        // 直接解密
        byte[] decoded = Base64.getDecoder().decode(encryptedBase64);
        byte[] decrypted = decrypt(key, decoded);
 
//        // 输出结果
//        System.out.println("Plaintext: " + plaintext);
//        System.out.println("Decrypted: " + new String(decrypted, StandardCharsets.UTF_8));
    }
}