Information Security 22 min read

Combining RSA and AES for Secure API Parameter Transmission: A Practical Guide

This article explains a real‑world incident where insecure API parameters led to leaderboard manipulation, then details how to securely combine RSA asymmetric encryption with AES symmetric encryption, covering key concepts, padding modes, implementation steps, and server‑side decryption using Java.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Combining RSA and AES for Secure API Parameter Transmission: A Practical Guide

1. Incident Overview

During the development of an H5 airplane‑battle game, the unlimited mode scores were transmitted via Body parameters, which are visible and vulnerable. To improve security, the developer and frontend agreed to send the score together with a secret number and user ID, then Base64‑encode the payload.

However, after the Chinese New Year, the operations team reported abnormal leaderboard scores, with the top player showing 400,000+ points while the second place had only around 10,000. Investigation revealed that a malicious user altered the Base64‑encoded request parameters, breaking the decryption logic.

2. RSA and AES Fundamentals

2.1 Asymmetric and Symmetric Encryption

Asymmetric Encryption (RSA) uses a public key for encryption and a private key for decryption. It provides strong security but is computationally slower.

Symmetric Encryption (AES) uses the same secret key for both encryption and decryption. It is fast but the key must be kept confidential.

2.2 RSA Basics

RSA generates a key pair (public and private). The client encrypts data with the public key; the server decrypts with the private key. Common padding modes include ENCRYPTION_OAEP , ENCRYPTION_PKCS1 , and ENCRYPTION_NONE . Java’s default implementation is RSA/None/PKCS1Padding . Key lengths should be multiples of 2048 bits for adequate security.

2.3 AES Basics

AES is a symmetric block cipher with modes such as ECB, CBC, CTR, CFB, OFB, and GCM. The typical transformation is AES/CBC/PKCS5Padding or AES/CBC/PKCS7Padding . The key size can be 128, 192, or 256 bits, and an IV (initialization vector) is required for modes like CBC.

3. Encryption Strategy

To balance security and performance, the strategy combines both algorithms:

Encrypt the bulk request parameters with AES (fast for large data).

Encrypt the AES key, IV, and timestamp with RSA (secure key exchange).

The client sends two fields: asy (AES‑encrypted payload) and sym (RSA‑encrypted AES key information).

3.1 Client Workflow

Generate a random 16‑byte AES key and IV.

Encrypt the actual request data with AES, producing asy .

Create a JSON containing key , keyVI , and time , then encrypt it with the server’s RSA public key to obtain sym .

Send asy and sym in the request body.

{
  "key":"0t7FtCDKofbEVpSZS",
  "keyVI":"0t7WESMofbEVpSZS",
  "time":211213232323323
}

3.2 Server Workflow

Define a custom annotation @RequestRSA to mark endpoints that require decryption.

Implement an AOP aspect that intercepts annotated methods, extracts asy and sym , and invokes RequestDecryptionUtil.getRequestDecryption() .

The utility first uses the RSA private key to decrypt sym , obtaining the AES key, IV, and timestamp.

It validates the timestamp (e.g., within 60 seconds) to prevent replay attacks.

Finally, it decrypts asy with AES and maps the resulting JSON to the original request object.

import java.lang.annotation.*;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestRSA {}
@Aspect
@Component
@Order(2)
@Slf4j
public class RequestRSAAspect {
    @Pointcut("execution(public * com.example.controller.*.*(..))")
    public void requestRAS() {}

    @Around("requestRAS()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        RequestRSA annotation = method.getAnnotation(RequestRSA.class);
        if (annotation != null) {
            Object[] args = joinPoint.getArgs();
            // extract body, parse JSON, get asy and sym
            // decrypt and replace argument
            // ...
        }
        return joinPoint.proceed();
    }
}

4. Server‑Side Decryption Utilities

The RequestDecryptionUtil class handles RSA decryption of sym , timestamp validation, and AES decryption of asy . It throws a ServiceException if the request is timed out.

public static Object getRequestDecryption(String sym, String asy, Class
clazz) {
    RSAPrivateKey privateKey = ActivityRSAUtil.getRSAPrivateKeyByString(PRIVATE_KEY);
    String rsaJson = ActivityRSAUtil.privateDecrypt(sym, privateKey);
    RSADecodeData data = JSONObject.parseObject(rsaJson, RSADecodeData.class);
    if (System.currentTimeMillis() - data.getTime() > TIMEOUT) {
        throw new ServiceException("Request timed out, please try again");
    }
    String aesJson = AES256Util.decode(data.getKey(), asy, data.getKeyVI());
    return JSONObject.parseObject(aesJson, clazz);
}

Supporting classes ActivityRSAUtil and AES256Util provide key generation, RSA encrypt/decrypt methods, and AES encrypt/decrypt with CBC/PKCS7 padding.

5. Conclusion

By combining RSA for secure key exchange and AES for efficient data encryption, the described approach mitigates the risk of parameter tampering, ensures confidentiality, and protects against replay attacks. The provided Java utilities and AOP integration make it straightforward to adopt this strategy in backend services.

Javabackend developmentRSAEncryptionAPI securityAES
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.