关于谷歌的App Engine Python中的Andr​​oid应用程序内购买的消息验证签名 [英] Verifying signature on android in-app purchase message in Python on Google App Engine

查看:167
本文介绍了关于谷歌的App Engine Python中的Andr​​oid应用程序内购买的消息验证签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android开发者网站上的示例应用程序验证用java code购买JSON。有没有人有任何运气工作如何验证购买蟒蛇。特别是在GAE?

以下是从Android应用内结算<一个相关的摘录href="http://developer.android.com/guide/market/billing/billing_integrate.html#billing-download">example计划。这是需要什么转换使用到Python PyCrypto 这是重新写完全蟒蛇由谷歌,是唯一安全的lib适用于应用程序引擎。希望谷歌是凉爽使用下面摘录了我。

 私有静态最后弦乐KEY_FACTORY_ALGORITHM =RSA;
私有静态最后弦乐SIGNATURE_ALGORITHM =SHA1withRSA;
字符串base64En codedPublicKey =在这里你的公钥;

公钥密钥= Security.generatePublicKey(base64En codedPublicKey);
验证= Security.verify(键,签名数据,签名);

公共静态公钥generatePublicKey(字符串连接codedPublicKey){
    尝试 {
        byte []的德codeDKEY = Base64.de code(EN codedPublicKey);
        的KeyFactory的KeyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
        返回keyFactory.generatePublic(新X509En codedKeySpec(德codeDKEY));
    } 抓住 ...
    }
}
公共静态布尔验证(公钥公钥,签名数据字符串,字符串签名){
    如果(Consts.DEBUG){
        Log.i(TAG的签名:+签名);
    }
    签名的签名;
    尝试 {
        SIG = Signature.getInstance(SIGNATURE_ALGORITHM);
        sig.initVerify(公钥);
        sig.update(signedData.getBytes());
        如果(!sig.verify(Base64.de code(签字))){
            Log.e(TAG,签名验证失败。);
            返回false;
        }
        返回true;
    } 抓住 ...
    }
    返回false;
}
 

解决方案

我是这样做的:

 从Crypto.Hash进口SHA
从Crypto.PublicKey进口RSA
从Crypto.Signature进口PKCS1_v1_5
从BASE64进口b64de code

DEF块(S,N):
    对于开局范围(0,len个(S),N):
        产量S [启动:启动+ N]

高清pem_format(键):
    回报'\ n'.join([
        ----- BEGIN公钥-----,
        '\ n'.join(块(键,64)),
        ----- END公钥-----
    ])

高清validate_purchase(公钥,签名数据,签名):
    键= RSA.importKey(pem_format(公钥))
    验证= PKCS1_v1_5.new(钥匙)
    数据= SHA.new(签名数据)
    SIG = b64de code(签名)
    返回verifier.verify(数据,SIG)
 

这假定公钥是你的base64 EN codeD谷歌Play商店的关键在一行上,你从开发者控制台得到它。

有关的人,而使用m2crypto谁, validate_purchase()将改变为:

 从M2Crypto进口RSA,BIO,执行副总裁
从BASE64进口b64de code

#pem_format()如上述

高清validate_purchase(公钥,签名数据,签名):
    生物= BIO.MemoryBuffer(pem_format(公钥))
    RSA = RSA.load_pub_key_bio(生物)
    键= EVP.PKey()
    key.assign_rsa(RSA)
    key.verify_init()
    key.verify_update(签名数据)
    返回key.verify_final(b64de code(签字))== 1
 

The sample application on the android developers site validates the purchase json using java code. Has anybody had any luck working out how to validate the purchase in python. In particular in GAE?

The following are the relevant excerpts from the android in-app billing example program. This is what would need to be converted to python using PyCrypto which was re-written to be completely python by Google and is the only Security lib available on app engine. Hopefully Google is cool with me using the excerpts below.

private static final String KEY_FACTORY_ALGORITHM = "RSA";
private static final String SIGNATURE_ALGORITHM = "SHA1withRSA";
String base64EncodedPublicKey = "your public key here";

PublicKey key = Security.generatePublicKey(base64EncodedPublicKey);
verified = Security.verify(key, signedData, signature);

public static PublicKey generatePublicKey(String encodedPublicKey) {
    try {
        byte[] decodedKey = Base64.decode(encodedPublicKey);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
        return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
    } catch ...
    }
}
public static boolean verify(PublicKey publicKey, String signedData, String signature) {
    if (Consts.DEBUG) {
        Log.i(TAG, "signature: " + signature);
    }
    Signature sig;
    try {
        sig = Signature.getInstance(SIGNATURE_ALGORITHM);
        sig.initVerify(publicKey);
        sig.update(signedData.getBytes());
        if (!sig.verify(Base64.decode(signature))) {
            Log.e(TAG, "Signature verification failed.");
            return false;
        }
        return true;
    } catch ...
    }
    return false;
}

解决方案

Here's how i did it:

from Crypto.Hash import SHA
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from base64 import b64decode

def chunks(s, n):
    for start in range(0, len(s), n):
        yield s[start:start+n]

def pem_format(key):
    return '\n'.join([
        '-----BEGIN PUBLIC KEY-----',
        '\n'.join(chunks(key, 64)),
        '-----END PUBLIC KEY-----'
    ])

def validate_purchase(publicKey, signedData, signature):
    key = RSA.importKey(pem_format(publicKey))
    verifier = PKCS1_v1_5.new(key)
    data = SHA.new(signedData)
    sig = b64decode(signature)
    return verifier.verify(data, sig)

This assumes that publicKey is your base64 encoded Google Play Store key on one line as you get it from the Developer Console.

For people who rather use m2crypto, validate_purchase() would change to:

from M2Crypto import RSA, BIO, EVP
from base64 import b64decode

# pem_format() as above

def validate_purchase(publicKey, signedData, signature):
    bio = BIO.MemoryBuffer(pem_format(publicKey))
    rsa = RSA.load_pub_key_bio(bio)
    key = EVP.PKey()
    key.assign_rsa(rsa)
    key.verify_init()
    key.verify_update(signedData)
    return key.verify_final(b64decode(signature)) == 1

这篇关于关于谷歌的App Engine Python中的Andr​​oid应用程序内购买的消息验证签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆