luxiaotao1123
2024-06-21 569d2b6bc754b037ade5b4205a4d1cdb8339b26b
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
package com.zy.asrs.framework.common;
 
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
 
 
/**
 * aes128-base64
 * @author Vincent
 */
public class AesUtils {
 
    private final static String DEFAULT_CHARSET = "utf-8";
    private final static int DEFAULT_KEY_LENGTH = 16;
 
    /**
     * aes128 - base64 加密
     * @param data  需要加密的明文
     * @param key   盐
     * @return   密文
     */
    public static String encrypt(String data, String key) {
        try {
            if (null == key || "".equals(key) || key.length() != DEFAULT_KEY_LENGTH) {
                return null;
            }
            byte[] raw = key.getBytes(DEFAULT_CHARSET);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(data.getBytes(DEFAULT_CHARSET));
            return RadixTools.bytesToHexStr(encrypted);
        } catch (Exception ex) {
            return null;
        }
 
    }
 
    /**
     * aes128 - base64 解密
     * @param data  需要解密的密文
     * @param key   盐
     * @return   明文
     */
    public static String decrypt(String data, String key) {
        try {
            if (null == key || "".equals(key) || key.length() != DEFAULT_KEY_LENGTH) {
                return null;
            }
            byte[] raw = key.getBytes(DEFAULT_CHARSET);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] original = cipher.doFinal(RadixTools.hexStringToBytes(data));
            return new String(original,DEFAULT_CHARSET);
        } catch (Exception ex) {
            return null;
        }
    }
 
    public static void main(String[] args) {
        //test
        String key = "123456";
//        String jsonData = "json={\"status\":200,\"msg\":\"success\",\"data\":\"dsadsa\"}&timestamp=" + String.valueOf(System.currentTimeMillis()+5000000L);
////        String jsonData = "status=200&msg=xltys1995==sadsadsad&timestamp=" + String.valueOf(System.currentTimeMillis()+5000000L);
//        System.out.println(System.currentTimeMillis() + 5000000L);
//        String encrypt = encrypt(key, jsonData);
//        System.out.println(encrypt);
//        String key = "QeCB1d74ab24482b";
//        String s = decrypt("bd064484343cde2d325693c0611c157d04294ae2cea03854d10a2f0aa01377cfc69cf6c700ae665c8f4c539d030bb2af"
//                , key);
//        System.out.printf(s);
 
 
        String data = "15988786205&timestamp=" + (System.currentTimeMillis() + 5000000L);
//        String jsonData = "status=200&msg=xltys1995==sadsadsad&timestamp=" + String.valueOf(System.currentTimeMillis()+5000000L);
        System.out.println(System.currentTimeMillis() + 5000000L);
        String encrypt = encrypt(data,key);
        System.out.println(encrypt);
    }
}