Air
2024-11-04 a1f06d31b7b4cac569c34bdfbb68de77f2858ffe
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.nova.sankuai.infra.utils;
 
 
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Random;
 
/**
 * @Description AES对称加密算法工具类
 * @Author CWR
 * @Date 2022/3/21 20:26
 */
public final class AesSecretUtils {
    /**
     * 私有构造方法
     */
    private AesSecretUtils() {
 
    }
 
    private static final String AES = "AES";
    private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
    private static final String SHA_1 = "SHA-1";
 
    /**
     * base64编码、解码对象
     */
    private static final Base64.Encoder ENCODER = Base64.getEncoder();
    private static final Base64.Decoder DECODER = Base64.getDecoder();
 
    /**
     * 加密
     *
     * @param plaintext 明文
     * @param secretKey 密钥
     * @return 密文base64
     */
    public static String encrypt(String plaintext, String secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        // 获取密钥对象
        SecretKey key = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), AES);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] plaintextBytes = plaintext.getBytes(StandardCharsets.UTF_8);
        // 加密
        byte[] ciphertextBytes = cipher.doFinal(plaintextBytes);
        return new String(ENCODER.encode(ciphertextBytes), StandardCharsets.UTF_8);
    }
 
    /**
     * 解密
     *
     * @param ciphertext 密文
     * @param secretKey  密钥
     * @return 密文base64
     */
    public static String decrypt(String ciphertext, String secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        // 获取密钥对象
        SecretKey key = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), AES);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] ciphertextBytes = ciphertext.getBytes(StandardCharsets.UTF_8);
        // 解密
        byte[] plaintextBytes = cipher.doFinal(DECODER.decode(ciphertextBytes));
        return new String(plaintextBytes, StandardCharsets.UTF_8);
    }
 
    /**
     * SHA-1加密
     *
     * @param plaintext
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String sha1(String plaintext) throws NoSuchAlgorithmException {
        MessageDigest digest = MessageDigest.getInstance(SHA_1);
        byte[] plaintextBytes = plaintext.getBytes(StandardCharsets.UTF_8);
        digest.update(plaintextBytes);
        // 获取字节数组
        byte[] messageDigest = digest.digest();
        StringBuilder hexString = new StringBuilder();
        // 字节数组转换为 ⼗六进制 数
        for (byte b : messageDigest) {
            String shaHex = Integer.toHexString(b & 0xFF);
            if (shaHex.length() < 2) {
                hexString.append(0);
            }
            hexString.append(shaHex);
        }
        return hexString.toString().toUpperCase();
    }
 
    /**
     * 随机生成字符串.
     *
     * @param length 字符串长度
     */
    public static String getRandomString(int length) {
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
 
    /**
     * 获取当前时间戳
     *
     * @return
     */
    public static Long getTimeStamp() {
        return System.currentTimeMillis() / 1000L;
    }
 
    public static void main(String[] args) throws NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
        String plaintext = "this is aes";
        String ciphertext = encrypt(plaintext, "1234123412341234");
        System.out.println("明文:" + plaintext);
        System.out.println("密文:" + ciphertext);
        String decryptString = decrypt(ciphertext, "1234123412341234");
        System.out.println("解密后:" + decryptString);
 
        System.out.println("SHA-1:" + sha1(plaintext));
    }
}