首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android在应用计费中:保护应用公钥

Android在应用计费中:保护应用公钥
EN

Stack Overflow用户
提问于 2013-01-16 14:47:53
回答 5查看 21.4K关注 0票数 61

来自安卓的应用计费版本3(TrivialDrive)示例应用程序随软件开发工具包一起提供

MainActivity.java

代码语言:javascript
复制
/* base64EncodedPublicKey should be YOUR APPLICATION'S PUBLIC KEY
 * (that you got from the Google Play developer console). This is not your
 * developer public key, it's the *app-specific* public key.
 *
 * Instead of just storing the entire literal string here embedded in the
 * program,  construct the key at runtime from pieces or
 * use bit manipulation (for example, XOR with some other string) to hide
 * the actual key.  The key itself is not secret information, but we don't
 * want to make it easy for an attacker to replace the public key with one
 * of their own and then fake messages from the server.
 */
String base64EncodedPublicKey = "CONSTRUCT_YOUR_KEY_AND_PLACE_IT_HERE";

嗯,我不确定我是否理解这个安全措施。我知道如何从Google Play开发人员控制台获取应用程序公钥(它已经是base64编码的)。

我不明白的是这部分

代码语言:javascript
复制
 /* Instead of just storing the entire literal string here embedded in the
 * program,  construct the key at runtime from pieces or
 * use bit manipulation (for example, XOR with some other string) to hide
 * the actual key
 */

据我所知,这个公钥是一个常量字符串,它是Google在应用程序上传过程中给出的。

我们如何使用任何位操作过程以编程方式创建相同的密钥?以前有人这么做过吗?有没有关于如何做到这一点的示例代码?

EN

回答 5

Stack Overflow用户

发布于 2015-02-20 18:41:00

另一种方法是对键进行一些基本的转换。

代码语言:javascript
复制
// Replace this with your encoded key.
String base64EncodedPublicKey = "";

// Get byte sequence to play with.
byte[] bytes = base64EncodedPublicKey.getBytes();

// Swap upper and lower case letters.
for (int i = 0; i < bytes.length; i++) {
    if(bytes[i] >= 'A' && bytes[i] <= 'Z')
        bytes[i] = (byte)( 'a' + (bytes[i] - 'A'));
    else if(bytes[i] >= 'a' && bytes[i] <= 'z')
        bytes[i] = (byte)( 'A' + (bytes[i] - 'a'));
}

// Assign back to string.
base64EncodedPublicKey = new String( bytes );

因此,我们的想法是将原始密钥作为base64EncodedPublicKey放入,并运行上面的代码,它将交换小写和大写字母,并将结果放回base64EncodedPublicKey中。然后,可以从调试器复制结果,并将其作为原始base64EncodedPublicKey值粘贴到代码中。在这一点上,你的key将被转换(大小写切换),并在运行时将其修复回正确的大小写,并继续工作。

上面的代码显然是非常基本的代码转换,但你可以更有创意,颠倒A-Z的顺序,交换奇数和偶数,将元音交换为偶数。这里的问题是,如果我在上面的代码片段中添加了一系列更有趣的代码转换,然后每个人都复制并粘贴到他们的项目中,黑客就可以很容易地看到并使用代码转换本身(通过查看这篇文章)!所以你只需要自己想出一些变化。

我特意在两个方向上做了上述工作(所以如果你运行两次,你会得到你的原始值),因为它使得在你的原始键上运行算法变得很容易。我认为这是一种整洁,它看起来像是纯文本的真正的密钥在那里,一个不经意的黑客可能会试图切换它,然后当它不起作用时,然后被弄糊涂了。

票数 13
EN

Stack Overflow用户

发布于 2013-01-16 15:38:41

你可以像这样把它分成几个部分

代码语言:javascript
复制
String piece1 = "SDFGJKGB4UIH234WE/FRT23RSDF/3DFUISDFVWE";
String piece2 = "SDFGJKGB4UIHUISDFVWE";
String piece3 = "BDYASGBDNAWGRET24IYE23das4saGBENWKD";
String piece4 = "432423SDF23R/+SDDS";

mHelper = new IabHelper(this, piece1 + piece2 + piece3 + piece4);

任何类型的操作都可以。

你不能对攻击者完全隐藏公钥,你只需要操纵字符串来稍微迷惑一下攻击者

您可以添加一些字符串,并在需要时将其删除,或者将其拆分成块。

票数 5
EN

Stack Overflow用户

发布于 2016-12-20 17:12:37

我所做的是将键转换为char数组,将其一分为二,然后在需要时重新构造它,如下所示:

代码语言:javascript
复制
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shop);

    char[] base64KeyByteArray = ArrayUtils.addAll(getPublicKeyChunk1(), getPublicKeyChunk2());

    Log.d(TAG, String.valueOf(base64KeyByteArray));
}

private char[] getPublicKeyChunk1() {
    return new char[]{82, 73, 67, 66, 73, 106, 65, 78, 66, 103, 107, 113, 104, 107,
            105, 71, 57, 119, 79, 66, 65, 81, 69, 70, 65, 65, 79, 67, 65, 81, 56, 65, 77, 73,
            73, 66, 67, 103, 75, 67, 65, 81, 69, 65, 121, 55, 81, 76, 122, 67, 105, 80, 65,
            110, 105, 101, 72, 66, 53, 57};
}

private char[] getPublicKeyChunk2() {
    return new char[]{82, 43, 68, 47, 79, 121, 122, 110, 85, 67, 118, 89, 108, 120, 43, 49,
            80, 100, 67, 108, 55, 90, 57, 103, 119, 57, 87, 78, 79, 111, 53, 101, 80, 71,
            117, 74, 104, 82, 87, 97, 100};
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14352758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档