我正在使用返回敏感数据的 API。该数据需要在客户端使用客户端生成的密钥对进行解密。
下面是将发送到服务器的
JSON
有效负载。该有效负载用于加密敏感数据。 注意:我不控制 API 服务器。
{
"exponent": "010001"
"keyId": 0,
"identifier": "RSA Key Identifier",
"appName": "App name",
"modulus": "30820122300d06092a864886f70d01010105000382010f003082010a0282010100b88c1fe0246b1b34e76184e12a35fc68c45821c399482ed4ebf1c5f6ac79f8c10f331d65b000bc2b41b7e51303d4a2d5a685faf1e6069c30a3ddd25febdc02fa9758f7cbe31a6f464293754c441ec8a054543f15ec5af94a64da545056825963de18efa204889834bdaaeab65ec8497ee2f34c25c79b93272baee0a0f8ad4e0137959b01875f01d71c639e16ce1b894471e182f3322a500299b5366d798333eab5ee23e4662c4fd80a82d83ebc974c5196902c339003c43ca16bcbf9cfdf8ca2d507e94eac0909bef34c1277233337226b5e4ab797ea31ed89628353d3623ec34afaf8a267cf86bacc6dda059ded35d764ce6b1ebc5f328a9dacf8eeceb59a950203010001",
}
我没有任何密码学知识,但我研究了如何使用
openssl
创建 RSA 密钥对
在
Config
下面用于创建 RSA
密钥对
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
//Continue to create the key PAIR//
$resource = openssl_pkey_new($config);
openssl_pkey_export($resource, $privateKey);
$pubKeyArr = openssl_pkey_get_details($resource);
$publicKey = $pubKeyArr["key"];
file_put_contents('publicKey',$publicKey);
file_put_contents('privateKey', $privateKey);
下面是我分别生成的公钥和私钥的副本。
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuIwf4CRrGzTnYYThKjX8
aMRYIcOZSC7U6/HF9qx5+MEPMx1lsAC8K0G35RMD1KLVpoX68eYGnDCj3dJf69wC
+pdY98vjGm9GQpN1TEQeyKBUVD8V7Fr5SmTaVFBWgllj3hjvogSImDS9quq2XshJ
fuLzTCXHm5MnK67goPitTgE3lZsBh18B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99+qQdqnzSbGGl4L7Xa9mbrVsz
2qQ15A7lBKMcX5MwSW5W2gB6nQKBgQDicg1L5XY0qZUDx8De1N6UwE6rJ+VSmdPx
DoMUUMg0DPd7D7nkeHSzntC+qmaWqt8oEeauk97PPZATRknx6IcrcPf2SAmJb9TK
6bf52A2gqUjbHJT0NanIqVoYlZl/EfPOr18NuC/ul7qfyVBO3HWfQ1J9GySgWfLb
IFukZCDCWQKBgBHfMrLBXadSST2pRQOnaAKohUombpkvaWKPVo/YmDZ0zxrw5tsG
351OMc8YfAi0OuoA54t5it5o2yXcjupj9m+A3MqcWukWWVFUKDGt68doqOtPNkIY
jilgWDDEfbaE2jh0TFEXVYHCnVt2mC9y9UoghR4dLZPKpCMoxyjtd8kJAoGAc0in
Vk1i6ZqwX7VRjuCG1Oo24tIz4OKp0R6XQz+BPWaglAlElXlxyISBN6CfC/PAGrFK
ZPSpSY7JC9cMDFlaf5ZK1xqDqHHDvu5QGKWTAolMqyiY+N9dKeh2EvAOrm+gVUhZ
Ny3fUTPz8idWeF+qjkOC+mhYND628jI+FapYNzkCgYEAw8UpkbgdYGZTmbl1hsDT
g0jDyYFTiDOArqd/WQGBg6vO882/CZmdnn6ffUYEadWc74yfLqDtcQxsUevLoGnu
VbgT8n45PQ4tci3Da/LgzhpYryxpVGCQKhl5t8s6W3CWhf+DpT7eVzsMHE4seJso
CiGA3Ugc3EJQnhafcgF9yRA=
-----END PRIVATE KEY-----
这是我面临的挑战。
Modulus
Exponent
identifier
。这是我迄今为止尝试过的。
来自 API 服务器的响应(Base64 编码)
$extendedDetails = '{"ExtendedDetails": {
"CardNumber": "GZf3NX5j6OB+EBZVgWGSHk/502IlNOnDpZrLJWG82h3FI8F7x5ufN9wsjgxKqCNvvc48d3nGsak90HS/uJR7XAoqvS3Oh0CuG/PEkHw0J+s32L/aADQp/+yArwBY7kDmw0Kps0O53HSGwiGJuzdKtA+08nJU5buTwzcIBv69d4LO/uu1f+tS/wSiYsEB2bfz2NticD5fifCT9Y5HYN9zi47aDkG8B6SD6QJLZPUt0asnoT38UlMDoAU/jAxTyefEu60dBkGtEIHZPbS3vfKdkUdL5kHF38Chk/52A36taAsTRhNzu9zHsLdEmBGNL4pd2VWcWWjqhAg4wAkvSk3FzG8c+VGof/Urzn+mqfoXDIJZkCRp3ZTsl2470AtrEu6R05o6U4mB",
"ExpiryDate": "LXJofV+/HKYp2viJC/L95zRZKgoshvCZr8B4R4xNBqrihk9j9xLcYdE+XJ5KLZ7zIY/4SN7h/9tOzQKwsVIZyyJhpUlIJJDgqtDggHZL1R1uYV8ssEy1x5JFbWOp/Bk7uZ9hkgUIWwoMCPA9oiKPbzfNsPEt4mR0zDlF2lBEGO835ngwXmM86YdiIKpV18wkrBPgkDXG6at870XEk/q8jgArbIqAy4HB5lU8uATb44/27JgBMD6bTVGj6+Y5l3to+zq2LbRr1mYOl3RjuoCe1lliaAZ/X4p0vJ66g1up50o0AZTvFvsbvcZ7wBy8IM9OOC6zFFRwN6FVgBdT8A8XGO3B9Rso1ol/odMUMFOhkSrRhw/MC5rub6RatMwbWOU+f/oU8A9T",
"CVV2": "Gpw4q/FcRCYp/sMB+1wio67JURMDgiZPQm1a0qk7e1zoPEGKRXXHJ0SOCYg1DatorpuTcLmOTpNvZyBH7lmHyKAEbng+ahVqCndPp7pggYakTtq8BqCTDP6LzpJ7EODRzYjVT2moCL3wVTgG+lx/0UpXBLucbgN7PG/Lcsr1rZavcSDfEVxbty2SDQRBc0g44wm0vBoDmWqo7f62KNFzujTQjRT4v08EfsRDNq3ZVnD6BgPlUINydZvrzaAThVq2jYFraDk66gCkC94W9WFvQFjkFnYC8L0zHqB36+CzGD8l5p6Y1d81oBB3A0Z0yLuiMvurgxanspsskydt5DgnYTy+XQp0K9nzXDOuB4G3WN3Vtbqwpj0SpsMWinBAF2X/4PvgzWpR",
"CardLimits": []
}}';
$cDetails = json_decode($extendedDetails, true);
$pan = base64_decode($cDetails['ExtendedDetails']['CardNumber']);
$exp = base64_decode($cDetails['ExtendedDetails']['ExpiryDate']);
$cv2 = base64_decode($cDetails['ExtendedDetails']['CVV2']);
$decryption_result = openssl_private_decrypt($pan, $decryptedPan, file_get_contents('privateKey'));
var_dump($decryption_result); // Returns bool(false)//
var_dump($decryptedPan); // Empty String//
openssl_private_decrypt
返回false,我的数据没有解密。
正如 Topaco 所指出的,您的
modulus
字段不仅仅包含模数,它还包含整个公钥,包括指数,采用SubjectPublicKeyInfo(或 SPKI)格式。要获得模数,您需要执行以下操作:
$pem_pub = "-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuIwf4CRrGzTnYYThKjX8
aMRYIcOZSC7U6/HF9qx5+MEPMx1lsAC8K0G35RMD1KLVpoX68eYGnDCj3dJf69wC
+pdY98vjGm9GQpN1TEQeyKBUVD8V7Fr5SmTaVFBWgllj3hjvogSImDS9quq2XshJ
fuLzTCXHm5MnK67goPitTgE3lZsBh18B1xxjnhbOG4lEceGC8zIqUAKZtTZteYMz
6rXuI+RmLE/YCoLYPryXTFGWkCwzkAPEPKFry/nP34yi1QfpTqwJCb7zTBJ3IzM3
ImteSreX6jHtiWKDU9NiPsNK+viiZ8+Gusxt2gWd7TXXZM5rHrxfMoqdrPjuzrWa
lQIDAQAB
-----END PUBLIC KEY-----";
$rsa_pub = openssl_pkey_get_public($pem_pub);
$pub_key_details = openssl_pkey_get_details($rsa_pub);
$modulus = $pub_key_details["rsa"]["n"];
$modulus_hex = bin2hex($modulus);