public AESObfuscator(byte[] salt, string password) { try { SecretKeyFactory factory = SecretKeyFactory.GetInstance(KEYGEN_ALGORITHM); PBEKeySpec keySpec = new PBEKeySpec(password.ToCharArray(), salt, 1024, 256); ISecretKey tmp = factory.GenerateSecret(keySpec); ISecretKey secret = new SecretKeySpec(tmp.GetEncoded(), "AES"); mEncryptor = Cipher.GetInstance(CIPHER_ALGORITHM); mEncryptor.Init(Cipher.EncryptMode, secret, new IvParameterSpec(IV)); mDecryptor = Cipher.GetInstance(CIPHER_ALGORITHM); mDecryptor.Init(Cipher.DecryptMode, secret, new IvParameterSpec(IV)); } catch (GeneralSecurityException e) { // This can't happen on a compatible Android device. throw new RuntimeException("Invalid environment", e); } }
/// <summary> /// Initializes a new instance of the <see cref="AesObfuscator"/> class. /// The aes obfuscator. /// </summary> /// <param name="salt"> /// an array of random bytes to use for each (un)obfuscation /// </param> /// <param name="applicationId"> /// application identifier, e.g. the package name /// </param> /// <param name="deviceId"> /// device identifier. Use as many sources as possible to /// create this unique identifier. /// </param> public AesObfuscator(byte[] salt, string applicationId, string deviceId) { try { SecretKeyFactory factory = SecretKeyFactory.GetInstance(KeygenAlgorithm); IKeySpec keySpec = new PBEKeySpec((applicationId + deviceId).ToCharArray(), salt, 1024, 256); ISecretKey tmp = factory.GenerateSecret(keySpec); ISecretKey secret = new SecretKeySpec(tmp.GetEncoded(), "AES"); this.encryptor = Cipher.GetInstance(CipherAlgorithm); this.encryptor.Init(CipherMode.EncryptMode, secret, new IvParameterSpec(Iv)); this.decryptor = Cipher.GetInstance(CipherAlgorithm); this.decryptor.Init(CipherMode.DecryptMode, secret, new IvParameterSpec(Iv)); } catch (GeneralSecurityException e) { // This can't happen on a compatible Android device. throw new RuntimeException("Invalid environment", e); } }
/// <summary> /// Requests that the specified Purchasable be purchased on behalf of the current user. /// The IAP client service is responsible for identifying the user and requesting credentials as appropriate, /// as well as providing all of the UI for the purchase flow. When purchases are successful, a Product object /// is returned that describes the product that was purchased. /// </summary> /// <param name="product">The Purchasable object that describes the item to be purchased.</param> /// <returns>Returns true if the purchase was successful.</returns> public async Task<bool> RequestPurchaseAsync(Product product) { if (ReferenceEquals(product, null)) throw new ArgumentNullException("product"); var tcs = new TaskCompletionSource<bool>(); // Create the Purchasable object from the supplied product var sr = SecureRandom.GetInstance("SHA1PRNG"); // This is an ID that allows you to associate a successful purchase with // it's original request. The server does nothing with this string except // pass it back to you, so it only needs to be unique within this instance // of your app to allow you to pair responses with requests. var uniqueId = sr.NextLong().ToString("X"); JSONObject purchaseRequest = new JSONObject(); purchaseRequest.Put("uuid", uniqueId); purchaseRequest.Put("identifier", product.Identifier); var purchaseRequestJson = purchaseRequest.ToString(); byte[] keyBytes = new byte[16]; sr.NextBytes(keyBytes); var key = new SecretKeySpec(keyBytes, "AES"); byte[] ivBytes = new byte[16]; sr.NextBytes(ivBytes); var iv = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.GetInstance("AES/CBC/PKCS5Padding", "BC"); cipher.Init(CipherMode.EncryptMode, key, iv); var payload = cipher.DoFinal(Encoding.UTF8.GetBytes(purchaseRequestJson)); cipher = Cipher.GetInstance("RSA/ECB/PKCS1Padding", "BC"); cipher.Init(CipherMode.EncryptMode, _publicKey); var encryptedKey = cipher.DoFinal(keyBytes); var purchasable = new Purchasable( product.Identifier, Convert.ToBase64String(encryptedKey, Base64FormattingOptions.None), Convert.ToBase64String(ivBytes, Base64FormattingOptions.None), Convert.ToBase64String(payload, Base64FormattingOptions.None)); var listener = new PurchaseListener(tcs, _publicKey, product, uniqueId); RequestPurchase(purchasable, listener); // No timeout for purchase as it shows a user dialog return await tcs.Task; }
/// <summary> /// Returns the secret key to use for initializing the Mac. /// </summary> /// <param name="algorithm">The algorithm.</param> /// <param name="keyMaterial">The key material.</param> /// <returns>The secret key.</returns> internal static SecretKeySpec GetSecretKey(MacAlgorithm algorithm, byte[] keyMaterial) { string algorithmName = MacAlgorithmProviderFactory.GetAlgorithmName(algorithm); var signingKey = new SecretKeySpec(keyMaterial, algorithmName); return signingKey; }