mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
package com.panzhihua.applets.umf;
 
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
 
public class MyAESUtil {
 
    // 加密
    public static String Encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        // 判断Key是否为16位
        if (sKey.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        byte[] raw = sKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
        return new BASE64Encoder().encode(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。
    }
 
    // 解密
    public static String Decrypt(String sSrc, String sKey) throws Exception {
        try {
 
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }
 
    public static void main(String[] args) throws Exception{
        String ryo7M3n8loC5Abcd = MyAESUtil.Encrypt("{\"order_status\":\"6\",\"wash_name\":null,\"wash_mobile\":null,\"phone\":\"13699017236\"}", "Ryo7M3n8loC5Abcd");
//        String ryo7M3n8loC5Abcd = MyAESUtil.Decrypt("Ixp8WMuJ9bnKhiBKmKWq\\/rQXQ7SaWc3fHLB1e\\/PzAvIEN0CH1nXtMCBzMegT49on31S+3Itop388Yc0EGVx30GGVlG81p7ssiqHlLRna2i903Rid9hqbjZdrBomiy2jp", "Ryo7M3n8loC5Abcd");
//        String ryo7M3n8loC5Abcd = MyAESUtil.Decrypt("0H4upa36EW9AFSNcoF9rVX6D5enB9T/9P1hWxLf/NgiGCueg159vz7Ex6+tPAR83aQLPyGeuF74Y5C9VpzQjL7brj8aWdMmnWJp/W1i5lpM=", "Ryo7M3n8loC5Abcd");
//        订单完成13980596944 {"aesString":"0H4upa36EW9AFSNcoF9rVX6D5enB9T\/9P1hWxLf\/NgiGCueg159vz7Ex6+tPAR83aQLPyGeuF74Y5C9VpzQjL2IYlgC9A6hhT82p\/kQTjTk="}
//        取消订单13980596944 {"aesString":"0H4upa36EW9AFSNcoF9rVfYjNQ0z81u6AWDcCU9O9DqGCueg159vz7Ex6+tPAR83aQLPyGeuF74Y5C9VpzQjL2IYlgC9A6hhT82p\/kQTjTk="}
        System.out.println(ryo7M3n8loC5Abcd);
    }
}