Android in-app billing Verification of Receipt in Dot Net(C#)

前端 未结 5 1585
甜味超标
甜味超标 2020-12-30 10:40

I have a Android application which provides in-app billing and we have our application server to which android application connects to provide services to the user, on in-ap

5条回答
  •  礼貌的吻别
    2020-12-30 11:21

    My solution based on BouncyCastle C# nuget.

    Replace the message, signature and key with your one and test it. No need for java to get the Modulus or Exponent.

    [TestMethod]
    public void ValidadeMessageTest()
    {
        //Base64-encoded RSA public key obtained from Google PlayStore, for the app. Go to DevelomentTools->Service & APIs
        var GooglePlayPK = "";
    
        bool validateReceipt(String message,String  messageSignature)
        {
            const String SIGNATURE_ALGORITHM = "SHA1";
    
            var rsaParameters = new RSAParameters();
            byte[] publicKeyBytes = Convert.FromBase64String(GooglePlayPK);
            AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(publicKeyBytes);
            RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)asymmetricKeyParameter;
            rsaParameters.Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned();
            rsaParameters.Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned();
    
            using (var rsa = new RSACryptoServiceProvider())
            {
                var encoder = new ASCIIEncoding();
                byte[] bytesToVerify = encoder.GetBytes(message);
                byte[] signedBytes = Convert.FromBase64String(messageSignature);
    
                    rsa.ImportParameters(rsaParameters);
                    return  rsa.VerifyData(bytesToVerify, CryptoConfig.MapNameToOID(SIGNATURE_ALGORITHM), signedBytes);                   
            }        
        }
        //test your receipt
        Assert.IsTrue(validateReceipt(, ));
    }
    

提交回复
热议问题