- 核心新增: ICON_CLICK(PNG图标方案,预渲染资源)/SCRATCH/JIGSAW/CURVE_SLIDER(V1-V3)/ANGLE/CURVE_DRAW/WORD_ORDER_CLICK/PROOF_OF_WORK 等生成器 - 图标点选: classpath PNG 加载替代运行时字体渲染(Linux容器无emoji字体),提示条图作为 templateImage 返回前端 - 新增模块: crypto(AES+RSA)/obfuscator(背景乱序/噪声/扭曲)/risk(风控/IP黑名单/限流)/site(站点管理)/ml(轨迹规则引擎) - 平台后端: 站点管理/验证码API(generate/verify/secondary-verify)/统计/ML轨迹学习 - 前端SDK: TPCaptcha兼容,支持全部新型号渲染与交互 - 工具: tools/icon-render 图标预渲染工具
116 lines
4.1 KiB
Java
116 lines
4.1 KiB
Java
package cloud.tianai.captcha.crypto;
|
|
|
|
import java.security.KeyPair;
|
|
import java.util.Base64;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public class CryptoService {
|
|
|
|
private final AesEncryptor aesEncryptor;
|
|
private final RsaEncryptor rsaEncryptor;
|
|
private final Signer signer;
|
|
private final String secretKey;
|
|
|
|
public CryptoService(byte[] aesKey, KeyPair rsaKeyPair, String secretKey) {
|
|
this.aesEncryptor = new AesEncryptor(aesKey);
|
|
try {
|
|
this.rsaEncryptor = new RsaEncryptor(
|
|
RsaEncryptor.publicKeyToBase64(rsaKeyPair.getPublic()),
|
|
RsaEncryptor.privateKeyToBase64(rsaKeyPair.getPrivate())
|
|
);
|
|
} catch (Exception e) {
|
|
throw new CryptoException("Failed to initialize RSA encryptor", e);
|
|
}
|
|
this.signer = new Signer();
|
|
this.secretKey = secretKey;
|
|
}
|
|
|
|
public CryptoService(String base64AesKey, String base64PublicKey, String base64PrivateKey, String secretKey) {
|
|
this.aesEncryptor = new AesEncryptor(base64AesKey);
|
|
try {
|
|
this.rsaEncryptor = new RsaEncryptor(base64PublicKey, base64PrivateKey);
|
|
} catch (Exception e) {
|
|
throw new CryptoException("Failed to initialize RSA encryptor", e);
|
|
}
|
|
this.signer = new Signer();
|
|
this.secretKey = secretKey;
|
|
}
|
|
|
|
public EncryptedPayload encrypt(String plaintext) {
|
|
String nonce = signer.generateNonce();
|
|
long timestamp = System.currentTimeMillis();
|
|
|
|
String aesEncrypted = aesEncryptor.encrypt(plaintext);
|
|
String signature = signer.signWithTimestamp(aesEncrypted + "|" + nonce, secretKey, timestamp);
|
|
|
|
EncryptedPayload payload = new EncryptedPayload();
|
|
payload.data = aesEncrypted;
|
|
payload.nonce = nonce;
|
|
payload.timestamp = timestamp;
|
|
payload.signature = signature;
|
|
return payload;
|
|
}
|
|
|
|
public String decrypt(EncryptedPayload payload) {
|
|
if (!signer.verifyWithTimestamp(
|
|
payload.data + "|" + payload.nonce,
|
|
secretKey,
|
|
payload.signature,
|
|
payload.timestamp,
|
|
300000
|
|
)) {
|
|
throw new CryptoException("Signature verification failed or timestamp expired");
|
|
}
|
|
return aesEncryptor.decrypt(payload.data);
|
|
}
|
|
|
|
public String encryptAesKeyForTransport(byte[] aesKey) {
|
|
return rsaEncryptor.encrypt(Base64.getEncoder().encodeToString(aesKey));
|
|
}
|
|
|
|
public String decryptAesKeyFromTransport(String encryptedAesKey) {
|
|
return rsaEncryptor.decrypt(encryptedAesKey);
|
|
}
|
|
|
|
public String getPublicKeyBase64() {
|
|
return RsaEncryptor.publicKeyToBase64(rsaEncryptor.getPublicKey());
|
|
}
|
|
|
|
public static CryptoService generate() throws Exception {
|
|
byte[] aesKey = AesEncryptor.generateKey();
|
|
KeyPair rsaKeyPair = RsaEncryptor.generateKeyPair();
|
|
byte[] secretKeyBytes = new byte[32];
|
|
new java.security.SecureRandom().nextBytes(secretKeyBytes);
|
|
String secretKey = Base64.getEncoder().encodeToString(secretKeyBytes);
|
|
return new CryptoService(aesKey, rsaKeyPair, secretKey);
|
|
}
|
|
|
|
public static class EncryptedPayload {
|
|
public String data;
|
|
public String nonce;
|
|
public long timestamp;
|
|
public String signature;
|
|
|
|
public Map<String, Object> toMap() {
|
|
Map<String, Object> map = new HashMap<>();
|
|
map.put("data", data);
|
|
map.put("nonce", nonce);
|
|
map.put("ts", timestamp);
|
|
map.put("sig", signature);
|
|
return map;
|
|
}
|
|
|
|
public static EncryptedPayload fromMap(Map<String, Object> map) {
|
|
EncryptedPayload payload = new EncryptedPayload();
|
|
payload.data = (String) map.get("data");
|
|
payload.nonce = (String) map.get("nonce");
|
|
payload.timestamp = map.get("ts") instanceof Number
|
|
? ((Number) map.get("ts")).longValue()
|
|
: Long.parseLong(String.valueOf(map.get("ts")));
|
|
payload.signature = (String) map.get("sig");
|
|
return payload;
|
|
}
|
|
}
|
|
}
|