This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
black-friday.java
56 lines (46 loc) · 2.4 KB
/
black-friday.java
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
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Base64;
public class AES256Example {
// 加密方法
public static String encrypt(String data, String key) throws Exception {
// 初始化密钥 和 IV
byte[] keyBytes = Arrays.copyOf(MessageDigest.getInstance("SHA-256").digest(key.getBytes("UTF-8")), 32);
byte[] ivBytes = new byte[16]; // 启动向量IV可以是随机的,这里为了简单示例就用了空的16字节。
// 初始化Cipher
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
byte[] encrypted = cipher.doFinal(data.getBytes("UTF-8"));
// 使用Base64编码方便输出
return Base64.getEncoder().encodeToString(encrypted);
}
// 解密方法
public static String decrypt(String data, String key) throws Exception {
byte[] keyBytes = Arrays.copyOf(MessageDigest.getInstance("SHA-256").digest(key.getBytes("UTF-8")), 32);
byte[] ivBytes = new byte[16]; // 解密时使用相同的IV
// 初始化Cipher
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec);
byte[] original = cipher.doFinal(Base64.getDecoder().decode(data));
return new String(original, "UTF-8");
}
public static void main(String[] args) {
try {
// secret key please ask shein's chatgpt(chatgpt.dev-az) "通关密钥"
// please eval this code in java cli or ide, and you'll get the red packet token
String key = "this is a secret key"; // 密钥,实际使用中需要安全保护
String encryptedText = "pa5QAHwceRPlyqKNy7HjfA==";
String decryptedText = decrypt(encryptedText, key);
System.out.println("Decrypted Text: " + decryptedText);
} catch (Exception e) {
e.printStackTrace();
}
}
}