Sensitive Data Encryption and Decryption
Overview
When handling API requests, especially those involving sensitive data (such as card numbers, CVV, expiration dates, etc.), it's essential to ensure the security of the data. To protect this sensitive data, we use the RSA encryption algorithm combined with the ECB mode and OAEPPadding padding scheme.
RSA Encryption
Encryption Algorithm
RSA/ECB/OAEPPadding
- RSA: An asymmetric encryption algorithm that uses a public key and a private key to encrypt and decrypt data.
- ECB (Electronic Codebook Mode): An encryption mode where each block is encrypted independently, suitable for encrypting plaintext blocks of the same length.
- OAEPPadding: A padding scheme in RSA that provides additional security and prevents certain types of attacks.
Encryption and Decryption Process
- Generate Key Pair:
- Generate an RSA public and private key. The public key is used for encrypting data, and the private key is used for decrypting data. Please refer to the previous document Generate Key Pair to generate your keys. Store your private key securely, and upload your public key to the platform.
- With RSA encryption, you only need to use PayCloud's public key, so you don't need to generate your own keys. If you need to decrypt sensitive data returned by PayCloud, you will need your private key. Currently, in business scenarios, sensitive data is not returned to developers, so this step can be temporarily ignored.
- Encrypt Sensitive Data:
- Encrypt sensitive data using the RSA public key. In our API usage scenario, when you request PayCloud, encrypt the data using PayCloud’s public key
GATEWAY_RSA_PUBLIC_KEY. - During the encryption process, use ECB mode and the OAEPPadding padding scheme.
- Transmit Encrypted Data:
- Encode the encrypted byte array as a Base64 string to facilitate transmission over the network. The encrypted data will be transmitted through the API.
- Decrypt Data:
- The recipient decodes the Base64 string and decrypts the data using the RSA private key.
- During the decryption process, the same padding scheme (OAEPPadding) is used.
When you receive encrypted data in PayCloud's response or Webhook notification message, use your private key APP_RSA_PRIVATE_KEY to decrypt it in the same manner.
Important Notes
- Key Management: Ensure secure management of both public and private keys, and do not expose the private key to unauthorized personnel.
- Data Security: Ensure that data remains confidential before encryption and that no plaintext data is leaked during transmission.
- Compliance: Follow applicable regulations and standards (e.g., PCI-DSS) when handling sensitive data.
Frequently Asked Questions
- Why does RSA encryption produce different results each time when encrypting the same plaintext?
The different results in RSA encryption occur because of the use of random padding mechanisms. This is an important feature of RSA encryption that enhances security and prevents encryption mode attacks.
- Random Padding: Each time data is encrypted, the padding algorithm (e.g., OAEP) generates new random data, which is then encrypted along with the plaintext. Therefore, even if the plaintext is the same, the encryption result will differ due to different random padding.
- Prevention of Replay Attacks: Through random padding, RSA encryption avoids the same ciphertext being generated from repeated encryption operations, which is important for preventing certain attacks like replay attacks.
Therefore, as long as decryption ensures success, the encryption process can be considered correct.
References
Sample Code
Here is a sample code using the Java SDK that demonstrates how to use RSA for encryption. For the complete code, please refer to Java SDK:
