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 toMap() { Map 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 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; } } }