This example code shows how to encrypt a push notification content and send it to a device. It uses RSA and AES encryption algorithms to encrypt the data.
With AES the data is encrypted and with RSA publickey the AES is encrypted and send to the devices. The client receives the encrypted data along with encrypted AES key. Since the client has the RSA privatekey, it can decrypt the AES key. Thereafter, it uses the AES key to decrypt the data and shows the content to the user.
- Server knows the RSA publickey and push notification token
- Server generates an AES key
val aesKey = cryptoManager.generateAESkey()
- Server encrypts the data with AES key (
CryptoManager.decryptSymmetric
)
val payloadEncryptedResult = cryptoManager.encryptSymmetric(payloadStr, aesKey)
- Server encrypts the AES key with RSA publickey (
CryptoManager.decryptAsymmetric
)
val encryptedAesKeyResult = cryptoManager.encryptAsymmetric(encodedAesKeyStr, publicKey)
- Server sends message to device with push notification token
CryptoManager can also be used to decrypt the content in Android
- Once the push notification is received, get the encrypted AES key from the received data
- Since the device has the RSA privatekey, it can decrypt the AES key
val privateKey = PRIVATE_KEY
val decryptedAesKeyResult = cryptoManager.decryptAsymmetric(encryptedAesKey, privateKey)
- Use the decryptedAesKey to decrypt the message
val messageResult = cryptoManager.decryptSymmetric(cipherData, encodedAesKeyStr)
You can use your own data set. In this example we use the following data
{
"encrypted-content": {
"version": "string",
"title": "fallback title",
"message": "fallback message",
"type": "notification type (if needed)",
"key": "encrypted symmetric key (AES). This key is encrypted with the asymmetic publickey",
"payload": "encrypted push message data. This is encrypted using the symmetic AES key"
}
}
//Data structure of encrypted content
{
"title": "encrypted title",
"message": "encrypted message",
"type": "notification type (if needed)",
"url": "Deeplink which describes what to do after the notification is opend"
}
Run the main method, it will start a Ktor server http://localhost:8222