简体   繁体   English

将 google 验证签名代码 (kotlin) 转换为 PHP (laravel)

[英]Convert google verify signature code (kotlin) to PHP (laravel)

I need to verify a signature, which is a security feature in google's developer android api.我需要验证一个签名,这是谷歌开发者 android api 中的一项安全功能。 They have a working example, which is written in kotlin.他们有一个用 kotlin 编写的工作示例。

Atm.自动取款机I am trying to convert this code:我正在尝试转换此代码:

val decodedKey = Base64.decode(encodedPublicKey, Base64.DEFAULT)
val keyFactory = KeyFactory.getInstance("RSA")
return keyFactory.generatePublic(X509EncodedKeySpec(decodedKey))

The encodedPublicKey is fix. encodedPublicKey已修复。 I get it from google.我从谷歌得到它。

I installed phpseclib and currently I try to convert the above code:我安装了phpseclib ,目前我尝试转换上面的代码:

$decodedKey = base64_decode($encodedPublicKey);
$x509 = new X509();
$x509->loadX509($encodedPublicKey);
$rsa = $x509->getPublicKey();
return [$rsa, $x509];

I discovered that not even base64_decode($encodedPublicKey) works.我发现甚至base64_decode($encodedPublicKey) It returns nothing, while the kotlin code Base64.decode(encodedPublicKey, Base64.DEFAULT) returns many decoded keys, example:它什么都不返回,而 kotlin 代码Base64.decode(encodedPublicKey, Base64.DEFAULT)返回许多已解码的密钥,例如:

D/IABUtil/Security: decodedKey 0 :48

EDIT编辑

kotlins Base64.decode(encodedPublicKey, Base64.DEFAULT) returns a bytearray. kotlins Base64.decode(encodedPublicKey, Base64.DEFAULT)返回一个Base64.decode(encodedPublicKey, Base64.DEFAULT) I managed to get the same result by using unpack() in php:我通过在 php 中使用unpack()获得了相同的结果:

$decodedKey = unpack('c*', $decodedKey); // ByteArray

So at the end I used open ssl for this.所以最后我为此使用了 open ssl。

$publicKey = env('BASE_64_ENCODED_PUBLIC_KEY');

$key = "-----BEGIN PUBLIC KEY-----\n" . chunk_split($publicKey, 64, "\n") . "-----END PUBLIC KEY-----";
$key = openssl_get_publickey($key);
if (false === $key) {
    return ["Could not get public Key"];
}

$verify = openssl_verify($originalJson, base64_decode($signature), $key, "sha1WithRSAEncryption");

Credit: android in app billing v3 with php信用: android in app billing v3 with php

It was important to convert the public key into the correct format.将公钥转换为正确的格式很重要。 It must have 64 characters in each line.每行必须有 64 个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM