# Unity Sample Code for Game Key Encryption and Decryption

**URL:** <https://community.amazondeveloper.com/t/unity-sample-code-for-game-key-encryption-and-decryption/2222>\
**Category:** Knowledge Base\
**Created:** [December 20, 2023, 9:54pm UTC](https://community.amazondeveloper.com/t/unity-sample-code-for-game-key-encryption-and-decryption/2222 "2023-12-20T21:54:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Levon](https://dhm3gbtzu79o6.cloudfront.net/user_avatar/community.amazondeveloper.com/levon/32/74_2.png) [@Levon](https://community.amazondeveloper.com/u/Levon)\
**Post date:** [December 20, 2023, 9:54pm UTC](https://community.amazondeveloper.com/t/unity-sample-code-for-game-key-encryption-and-decryption/2222/1 "2023-12-20T21:54:03Z")

</div>

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](https://developer.amazon.com/). 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](https://bouncycastle.org).

- 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.

```csharp
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:**

- [Configure Unity App with Public Key](https://developer.amazon.com/docs/cross-platform-plugins/cpp-public-key-unity-projects.html)

_Last updated: Mar 11, 2026_
