Unity Sample Code for Game Key Encryption and Decryption

When integrating your Unity game with the Amazon Appstore SDK, you need to establish a secure communication channel between the Appstore and your app. This involves encrypting the client public key with your game’s public key, which you can obtain from the Amazon Developer Console. The encrypted payload is then sent to Amazon to verify your app’s identity.

This Unity / C# sample demonstrates encryption of the client public key using the game public key. The sample uses Bouncy Castle crypto libraries.

  • The game public key is a Base64-encoded RSA public key provided by Amazon in the Developer Console when you configure your app’s public key settings.
  • The client public key is a 1024-bit PKCS#8 RSA public key generated by your game client at runtime.
using System;
using System.Security.Cryptography;
using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
namespace crypto
{
    class Program
    {
        static String gamePublicKey = "your_game_public_key_here";
        static String clientPublicKey = "players_generated_1024_pkcs8_here";
        static void Main(string[] args)
        {
            // Build RSA parameters from the Base64-encoded game public key
            var rsaParameters = Base64ToRsaParameters(gamePublicKey);
            // Initialize RSA and import the game public key
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.ImportParameters(rsaParameters);
            // Encrypt the client public key using PKCS#1 v1.5 padding.
            // Note: For stronger security, consider using OAEP padding
            // by passing true instead of false.
            var encryptedData = rsa.Encrypt(Encoding.UTF8.GetBytes(clientPublicKey), false);
            var encryptedPayload = Convert.ToBase64String(encryptedData);
            Console.WriteLine(encryptedPayload);
        }
        static RSAParameters Base64ToRsaParameters(String cryptoKey)
        {
            byte[] publicKeyBytes = Convert.FromBase64String(cryptoKey);
            AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(publicKeyBytes);
            RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)asymmetricKeyParameter;
            RSAParameters rsaParameters = new RSAParameters
            {
                Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned(),
                Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned()
            };
            return rsaParameters;
        }
    }
}

Related:

Last updated: Mar 11, 2026