main
黄海 1 year ago
parent 7dc81e6873
commit 8521a9b42a

@ -125,7 +125,24 @@
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<!--加载jwt-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.2</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.2</version>
<scope>runtime</scope>
</dependency>
<!--引用json库-->
<dependency>
<groupId>net.sf.json-lib</groupId>

@ -0,0 +1,59 @@
package UnitTest;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AesUtil {
static final int KEY_LENGTH_BYTE = 32;
static final int TAG_LENGTH_BIT = 128;
private final byte[] aesKey;
public AesUtil(byte[] key) {
if (key.length != KEY_LENGTH_BYTE) {
throw new IllegalArgumentException("无效的ApiV3Key长度必须为32个字节");
}
this.aesKey = key;
}
public String decryptToString(byte[] associatedData, byte[] nonce, String ciphertext)
throws GeneralSecurityException, IOException {
try {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
SecretKeySpec key = new SecretKeySpec(aesKey, "AES");
GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce);
cipher.init(Cipher.DECRYPT_MODE, key, spec);
cipher.updateAAD(associatedData);
return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), "utf-8");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new IllegalStateException(e);
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
throw new IllegalArgumentException(e);
}
}
public static String decryptToString(String enData) throws GeneralSecurityException, IOException {
String key = "1d35eefc2b8207d615028d056ce5296c";
String associatedData = "12345";
String nonce = "3229172322917278";
AesUtil aesUtils = new AesUtil(key.getBytes());
String deData = aesUtils.decryptToString(associatedData.getBytes(), nonce.getBytes(), enData);
return deData;
}
public static void main(String[] args) throws Exception {
String enData = "Bs4h+MXFkIsmtacWqM2ah808n53ru/eAcIwN/rGLNaIOKEBaysqG6g==";
System.out.println(decryptToString(enData));
}
}

@ -1,53 +0,0 @@
package UnitTest;
import java.util.Base64;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;
public class KeyValidator {
public static boolean validateKey(String key, String sharedSecret) {
try {
String[] parts = key.split(":");
if (parts.length != 3) {
return false; // Invalid key format
}
String secret = parts[0];
String timestampStr = parts[1];
String expirationStr = parts[2];
long timestamp = Long.parseLong(timestampStr);
long expiration = Long.parseLong(expirationStr);
// Check if the key has expired
if (System.currentTimeMillis() > expiration * 1000) {
return false; // Key has expired
}
// Recreate the key for verification
String keyData = String.join(":", secret, timestampStr, expirationStr);
String generatedKey = generateKey(sharedSecret, keyData);
return key.equals(generatedKey); // Validate the key
} catch (Exception e) {
return false; // Error in processing, consider the key invalid
}
}
private static String generateKey(String secret, String keyData) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update((keyData + secret).getBytes());
byte[] digest = md.digest();
return Base64.getUrlEncoder().encodeToString(digest);
}
public static void main(String[] args) throws Exception {
String sharedSecret = "DsideaL4r5t6y7u@123"; // This should match the one used in Python
String key = "0ede740c2c81ea362dcdeee568716eb06706d96dda55be6109bb8cf2c83344c1"; // Example key to validate
boolean isValid = validateKey(key, sharedSecret);
System.out.println("Is the key valid? " + isValid);
}
}

@ -744,7 +744,11 @@ public class ZbdcModel {
else record.set(key, Double.parseDouble(value.toString()));
}
}
int lb_sb_id = jsonObject.getInteger("lb_sb_id");
int lb_sb_id = 1;
if (jsonObject.containsKey("lb_sb_id")) {
lb_sb_id = jsonObject.getInteger("lb_sb_id");
}
Record lbSbRecord = Db.findById("t_zbdc_table_lbsb", "lb_sb_id", lb_sb_id);
int lb_id = lbSbRecord.getInt("lb_id");//类别ID
String lb_name = lbSbRecord.getStr("lb_name");//类别名称

Loading…
Cancel
Save