一键登录 API
最近更新:2026-08-19
展开全部
一键登录 API
功能说明
提交 loginToken,验证后返回加密的手机号码。
调用地址
Android、Harmony、iOS 使用
POST https://api.verification.jpush.cn/v1/web/loginTokenVerify
Web 使用
POST https://api.verification.jpush.cn/v1/web/h5/loginTokenVerify
调用验证
详情参见 REST API 概述的 鉴权方式 说明。
请求示例
curl --insecure -X POST -v https://api.verification.jpush.cn/v1/web/loginTokenVerify -H "Content-Type: application/json" -u "7d431e42dfa6a6d693ac2d04:5e987ac6d2e04d95a9d8f0d1" -d '{
"loginToken": "STsid0000001542695429579Ob28vB7b0cYTI9w0GGZrv8ujUu05qZvw",
"exID": "1234566"
}'
curl --insecure -X POST -v https://api.verification.jpush.cn/v1/web/loginTokenVerify -H "Content-Type: application/json" -u "7d431e42dfa6a6d693ac2d04:5e987ac6d2e04d95a9d8f0d1" -d '{
"loginToken": "STsid0000001542695429579Ob28vB7b0cYTI9w0GGZrv8ujUu05qZvw",
"exID": "1234566"
}'
此代码块在浮窗中显示
请求参数
| 关键字 | 类型 | 选项 | 含义 |
|---|---|---|---|
| loginToken | String | 必填 | 认证 SDK 获取到的 loginToken |
| exID | String | 可选 | 开发者自定义的 id,非必填 |
响应示例
请求成功
{
"id": 117270465679982592,
"code": 8000,
"content": "get phone success",
"exID": "1234566",
"phone": "HpBLIQ/6SkFl0pAq0LMdw1aZ8RHoofgWmaY//LE+0ahkSdHC5oTCnjrR8Tj8y5naKVI03torFU+EzAQnwtVqAoQyYckT0S3Q02TKuAal3VRGiR5Lmp4g2A5Mh4/W5A4o6QFviHuBVJZE/WV0AzU5w4NGhpyQntOeF0UyovYATy4="
}
{
"id": 117270465679982592,
"code": 8000,
"content": "get phone success",
"exID": "1234566",
"phone": "HpBLIQ/6SkFl0pAq0LMdw1aZ8RHoofgWmaY//LE+0ahkSdHC5oTCnjrR8Tj8y5naKVI03torFU+EzAQnwtVqAoQyYckT0S3Q02TKuAal3VRGiR5Lmp4g2A5Mh4/W5A4o6QFviHuBVJZE/WV0AzU5w4NGhpyQntOeF0UyovYATy4="
}
此代码块在浮窗中显示
请求失败
{
"code": 8001,
"content": "get phone fail"
}
{
"code": 8001,
"content": "get phone fail"
}
此代码块在浮窗中显示
响应参数
| 关键字 | 类型 | 含义 |
|---|---|---|
| id | Long | 流水号,请求出错时可能为空 |
| exID | String | 开发者自定义的 id,若请求时为空返回为空 |
| code | Integer | 返回码 |
| content | String | 返回码说明 |
| phone | String | 加密后的手机号码,需用配置在极光的公钥对应的私钥解密。解析后的手机号码示例:大陆号码:13812345678;香港移动号码:+852-12345678 |
RSA 私钥解密示例
1024 位 / PKCS#1 v1.5
Java
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
public class RSADecrypt {public static void main(String[] args) throws Exception {String encrypted = args[0];
String prikey = args[1];
String result = decrypt(encrypted, prikey);
System.out.println(result);
}
public static String decrypt(String cryptograph, String prikey) throws Exception {PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(prikey));
PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte [] b = Base64.getDecoder().decode(cryptograph);
return new String(cipher.doFinal(b), StandardCharsets.UTF_8);
}
}
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
public class RSADecrypt {public static void main(String[] args) throws Exception {String encrypted = args[0];
String prikey = args[1];
String result = decrypt(encrypted, prikey);
System.out.println(result);
}
public static String decrypt(String cryptograph, String prikey) throws Exception {PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(prikey));
PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte [] b = Base64.getDecoder().decode(cryptograph);
return new String(cipher.doFinal(b), StandardCharsets.UTF_8);
}
}
此代码块在浮窗中显示
Python
需要先安装 pycryptodome:
pip install pycryptodome
pip install pycryptodome
此代码块在浮窗中显示
#!/usr/bin/env python3
import base64
import sys
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
PREFIX = "-----BEGIN PRIVATE KEY-----"
SUFFIX = "-----END PRIVATE KEY-----"
encrypted = sys.argv[1]
prikey = sys.argv[2]
key = "{}\n{}\n{}".format(PREFIX, prikey, SUFFIX)
private_key = RSA.import_key(key)
cipher = PKCS1_v1_5.new(private_key)
sentinel = b""
result = cipher.decrypt(base64.b64decode(encrypted), sentinel)
if result == sentinel:
raise ValueError("decrypt failed")
print(result.decode("utf-8"))
#!/usr/bin/env python3
import base64
import sys
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
PREFIX = "-----BEGIN PRIVATE KEY-----"
SUFFIX = "-----END PRIVATE KEY-----"
encrypted = sys.argv[1]
prikey = sys.argv[2]
key = "{}\n{}\n{}".format(PREFIX, prikey, SUFFIX)
private_key = RSA.import_key(key)
cipher = PKCS1_v1_5.new(private_key)
sentinel = b""
result = cipher.decrypt(base64.b64decode(encrypted), sentinel)
if result == sentinel:
raise ValueError("decrypt failed")
print(result.decode("utf-8"))
此代码块在浮窗中显示
PHP
<?php
$prefix = '-----BEGIN PRIVATE KEY-----';
$suffix = '-----END PRIVATE KEY-----';
$encrypted = $argv[1];
$prikey = $argv[2];
$key = $prefix . "\n" . $prikey . "\n" . $suffix;
$success = openssl_private_decrypt(
base64_decode($encrypted, true),
$result,
openssl_pkey_get_private($key),
OPENSSL_PKCS1_PADDING
);
if (!$success) {
throw new RuntimeException('decrypt failed');
}
echo $result . PHP_EOL;
<?php
$prefix = '-----BEGIN PRIVATE KEY-----';
$suffix = '-----END PRIVATE KEY-----';
$encrypted = $argv[1];
$prikey = $argv[2];
$key = $prefix . "\n" . $prikey . "\n" . $suffix;
$success = openssl_private_decrypt(
base64_decode($encrypted, true),
$result,
openssl_pkey_get_private($key),
OPENSSL_PKCS1_PADDING
);
if (!$success) {
throw new RuntimeException('decrypt failed');
}
echo $result . PHP_EOL;
此代码块在浮窗中显示
Go
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"os"
)
func main() {
prefix := "-----BEGIN PRIVATE KEY-----"
suffix := "-----END PRIVATE KEY-----"
encrypted := os.Args[1]
prikey := os.Args[2]
encryptedBytes, err := base64.StdEncoding.DecodeString(encrypted)
if err != nil {
fmt.Fprintln(os.Stderr, "invalid encrypted")
os.Exit(1)
}
key := prefix + "\n" + prikey + "\n" + suffix
result, err := rsaDecrypt(encryptedBytes, []byte(key))
if err != nil {
fmt.Fprintln(os.Stderr, "decrypt failed:", err)
os.Exit(1)
}
fmt.Println(string(result))
}
// rsaDecrypt 使用 RSA PKCS#1 v1.5 私钥解密。
func rsaDecrypt(encrypted, prikey []byte) ([]byte, error) {
block, _ := pem.Decode(prikey)
if block == nil {
return nil, errors.New("private key error")
}
parsedKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
privateKey, ok := parsedKey.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("invalid private key")
}
return rsa.DecryptPKCS1v15(rand.Reader, privateKey, encrypted)
}
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"os"
)
func main() {
prefix := "-----BEGIN PRIVATE KEY-----"
suffix := "-----END PRIVATE KEY-----"
encrypted := os.Args[1]
prikey := os.Args[2]
encryptedBytes, err := base64.StdEncoding.DecodeString(encrypted)
if err != nil {
fmt.Fprintln(os.Stderr, "invalid encrypted")
os.Exit(1)
}
key := prefix + "\n" + prikey + "\n" + suffix
result, err := rsaDecrypt(encryptedBytes, []byte(key))
if err != nil {
fmt.Fprintln(os.Stderr, "decrypt failed:", err)
os.Exit(1)
}
fmt.Println(string(result))
}
// rsaDecrypt 使用 RSA PKCS#1 v1.5 私钥解密。
func rsaDecrypt(encrypted, prikey []byte) ([]byte, error) {
block, _ := pem.Decode(prikey)
if block == nil {
return nil, errors.New("private key error")
}
parsedKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
privateKey, ok := parsedKey.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("invalid private key")
}
return rsa.DecryptPKCS1v15(rand.Reader, privateKey, encrypted)
}
此代码块在浮窗中显示
2048 位 / PKCS1Padding
Java
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
public class RSADecrypt {
public static void main(String[] args) throws Exception {
String encrypted = args[0];
String prikey = args[1];
String result = decrypt(encrypted, prikey);
System.out.println(result);
}
public static String decrypt(String cryptograph, String prikey) throws Exception {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
Base64.getDecoder().decode(prikey));
PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] b = Base64.getDecoder().decode(cryptograph);
return new String(cipher.doFinal(b), StandardCharsets.UTF_8);
}
}
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
public class RSADecrypt {
public static void main(String[] args) throws Exception {
String encrypted = args[0];
String prikey = args[1];
String result = decrypt(encrypted, prikey);
System.out.println(result);
}
public static String decrypt(String cryptograph, String prikey) throws Exception {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
Base64.getDecoder().decode(prikey));
PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] b = Base64.getDecoder().decode(cryptograph);
return new String(cipher.doFinal(b), StandardCharsets.UTF_8);
}
}
此代码块在浮窗中显示
Python
需要先安装 pycryptodome:
pip install pycryptodome
pip install pycryptodome
此代码块在浮窗中显示
#!/usr/bin/env python3
import base64
import sys
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
PREFIX = "-----BEGIN PRIVATE KEY-----"
SUFFIX = "-----END PRIVATE KEY-----"
encrypted = sys.argv[1]
prikey = sys.argv[2]
key = "{}\n{}\n{}".format(PREFIX, prikey, SUFFIX)
private_key = RSA.import_key(key)
cipher = PKCS1_v1_5.new(private_key)
sentinel = b""
result = cipher.decrypt(base64.b64decode(encrypted), sentinel)
if result == sentinel:
raise ValueError("decrypt failed")
print(result.decode("utf-8"))
#!/usr/bin/env python3
import base64
import sys
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
PREFIX = "-----BEGIN PRIVATE KEY-----"
SUFFIX = "-----END PRIVATE KEY-----"
encrypted = sys.argv[1]
prikey = sys.argv[2]
key = "{}\n{}\n{}".format(PREFIX, prikey, SUFFIX)
private_key = RSA.import_key(key)
cipher = PKCS1_v1_5.new(private_key)
sentinel = b""
result = cipher.decrypt(base64.b64decode(encrypted), sentinel)
if result == sentinel:
raise ValueError("decrypt failed")
print(result.decode("utf-8"))
此代码块在浮窗中显示
PHP
<?php
$prefix = '-----BEGIN PRIVATE KEY-----';
$suffix = '-----END PRIVATE KEY-----';
$encrypted = $argv[1];
$prikey = $argv[2];
$key = $prefix . "\n" . $prikey . "\n" . $suffix;
$success = openssl_private_decrypt(
base64_decode($encrypted, true),
$result,
openssl_pkey_get_private($key),
OPENSSL_PKCS1_PADDING
);
if (!$success) {
throw new RuntimeException('decrypt failed');
}
echo $result . PHP_EOL;
<?php
$prefix = '-----BEGIN PRIVATE KEY-----';
$suffix = '-----END PRIVATE KEY-----';
$encrypted = $argv[1];
$prikey = $argv[2];
$key = $prefix . "\n" . $prikey . "\n" . $suffix;
$success = openssl_private_decrypt(
base64_decode($encrypted, true),
$result,
openssl_pkey_get_private($key),
OPENSSL_PKCS1_PADDING
);
if (!$success) {
throw new RuntimeException('decrypt failed');
}
echo $result . PHP_EOL;
此代码块在浮窗中显示
Go
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"os"
)
func main() {
prefix := "-----BEGIN PRIVATE KEY-----"
suffix := "-----END PRIVATE KEY-----"
encrypted := os.Args[1]
prikey := os.Args[2]
encryptedBytes, err := base64.StdEncoding.DecodeString(encrypted)
if err != nil {
fmt.Fprintln(os.Stderr, "invalid encrypted")
os.Exit(1)
}
key := prefix + "\n" + prikey + "\n" + suffix
result, err := rsaDecrypt(encryptedBytes, []byte(key))
if err != nil {
fmt.Fprintln(os.Stderr, "decrypt failed:", err)
os.Exit(1)
}
fmt.Println(string(result))
}
// rsaDecrypt 使用 RSA PKCS#1 v1.5 私钥解密。
func rsaDecrypt(encrypted, prikey []byte) ([]byte, error) {
block, _ := pem.Decode(prikey)
if block == nil {
return nil, errors.New("private key error")
}
parsedKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
privateKey, ok := parsedKey.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("invalid private key")
}
return rsa.DecryptPKCS1v15(rand.Reader, privateKey, encrypted)
}
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"os"
)
func main() {
prefix := "-----BEGIN PRIVATE KEY-----"
suffix := "-----END PRIVATE KEY-----"
encrypted := os.Args[1]
prikey := os.Args[2]
encryptedBytes, err := base64.StdEncoding.DecodeString(encrypted)
if err != nil {
fmt.Fprintln(os.Stderr, "invalid encrypted")
os.Exit(1)
}
key := prefix + "\n" + prikey + "\n" + suffix
result, err := rsaDecrypt(encryptedBytes, []byte(key))
if err != nil {
fmt.Fprintln(os.Stderr, "decrypt failed:", err)
os.Exit(1)
}
fmt.Println(string(result))
}
// rsaDecrypt 使用 RSA PKCS#1 v1.5 私钥解密。
func rsaDecrypt(encrypted, prikey []byte) ([]byte, error) {
block, _ := pem.Decode(prikey)
if block == nil {
return nil, errors.New("private key error")
}
parsedKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
privateKey, ok := parsedKey.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("invalid private key")
}
return rsa.DecryptPKCS1v15(rand.Reader, privateKey, encrypted)
}
此代码块在浮窗中显示
2048 位 / OAEPWithSHA-256AndMGF1Padding
控制台选择 2048 位 / OAEPWithSHA-256AndMGF1Padding 时,请使用对应的 2048 位 RSA 私钥进行解密。OAEP digest 和 MGF1 digest 均为 SHA-256,OAEP label 为空。
Java
import javax.crypto.Cipher;
import javax.crypto.spec.OAEPParameterSpec;
import javax.crypto.spec.PSource;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.MGF1ParameterSpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
public class RSADecrypt {
public static void main(String[] args) throws Exception {
String encrypted = args[0];
String prikey = args[1];
String result = decrypt(encrypted, prikey);
System.out.println(result);
}
public static String decrypt(String cryptograph, String prikey) throws Exception {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
Base64.getDecoder().decode(prikey));
PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
OAEPParameterSpec parameterSpec = new OAEPParameterSpec(
"SHA-256",
"MGF1",
MGF1ParameterSpec.SHA256,
PSource.PSpecified.DEFAULT);
cipher.init(Cipher.DECRYPT_MODE, privateKey, parameterSpec);
byte[] b = Base64.getDecoder().decode(cryptograph);
return new String(cipher.doFinal(b), StandardCharsets.UTF_8);
}
}
import javax.crypto.Cipher;
import javax.crypto.spec.OAEPParameterSpec;
import javax.crypto.spec.PSource;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.MGF1ParameterSpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
public class RSADecrypt {
public static void main(String[] args) throws Exception {
String encrypted = args[0];
String prikey = args[1];
String result = decrypt(encrypted, prikey);
System.out.println(result);
}
public static String decrypt(String cryptograph, String prikey) throws Exception {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
Base64.getDecoder().decode(prikey));
PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
OAEPParameterSpec parameterSpec = new OAEPParameterSpec(
"SHA-256",
"MGF1",
MGF1ParameterSpec.SHA256,
PSource.PSpecified.DEFAULT);
cipher.init(Cipher.DECRYPT_MODE, privateKey, parameterSpec);
byte[] b = Base64.getDecoder().decode(cryptograph);
return new String(cipher.doFinal(b), StandardCharsets.UTF_8);
}
}
此代码块在浮窗中显示
Python
需要先安装 pycryptodome:
pip install pycryptodome
pip install pycryptodome
此代码块在浮窗中显示
#!/usr/bin/env python3
import base64
import sys
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto.Signature.pss import MGF1
PREFIX = "-----BEGIN PRIVATE KEY-----"
SUFFIX = "-----END PRIVATE KEY-----"
encrypted = sys.argv[1]
prikey = sys.argv[2]
key = "{}\n{}\n{}".format(PREFIX, prikey, SUFFIX)
private_key = RSA.import_key(key)
cipher = PKCS1_OAEP.new(
private_key,
hashAlgo=SHA256,
mgfunc=lambda seed, length: MGF1(seed, length, SHA256),
label=b"",
)
result = cipher.decrypt(base64.b64decode(encrypted)).decode("utf-8")
print(result)
#!/usr/bin/env python3
import base64
import sys
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto.Signature.pss import MGF1
PREFIX = "-----BEGIN PRIVATE KEY-----"
SUFFIX = "-----END PRIVATE KEY-----"
encrypted = sys.argv[1]
prikey = sys.argv[2]
key = "{}\n{}\n{}".format(PREFIX, prikey, SUFFIX)
private_key = RSA.import_key(key)
cipher = PKCS1_OAEP.new(
private_key,
hashAlgo=SHA256,
mgfunc=lambda seed, length: MGF1(seed, length, SHA256),
label=b"",
)
result = cipher.decrypt(base64.b64decode(encrypted)).decode("utf-8")
print(result)
此代码块在浮窗中显示
PHP
PHP 原生 openssl_private_decrypt 无法同时显式指定 OAEP digest 和 MGF1 digest。以下示例使用 phpseclib 3:
composer require phpseclib/phpseclib:^3.0
composer require phpseclib/phpseclib:^3.0
此代码块在浮窗中显示
<?php
require __DIR__ . '/vendor/autoload.php';
use phpseclib3\Crypt\RSA;
$prefix = '-----BEGIN PRIVATE KEY-----';
$suffix = '-----END PRIVATE KEY-----';
$encrypted = $argv[1];
$prikey = $argv[2];
$key = $prefix . "\n" . $prikey . "\n" . $suffix;
$privateKey = RSA::loadPrivateKey($key)
->withPadding(RSA::ENCRYPTION_OAEP)
->withHash('sha256')
->withMGFHash('sha256')
->withLabel('');
$encryptedBytes = base64_decode($encrypted, true);
if ($encryptedBytes === false) {
throw new RuntimeException('invalid encrypted');
}
$result = $privateKey->decrypt($encryptedBytes);
echo $result . PHP_EOL;
<?php
require __DIR__ . '/vendor/autoload.php';
use phpseclib3\Crypt\RSA;
$prefix = '-----BEGIN PRIVATE KEY-----';
$suffix = '-----END PRIVATE KEY-----';
$encrypted = $argv[1];
$prikey = $argv[2];
$key = $prefix . "\n" . $prikey . "\n" . $suffix;
$privateKey = RSA::loadPrivateKey($key)
->withPadding(RSA::ENCRYPTION_OAEP)
->withHash('sha256')
->withMGFHash('sha256')
->withLabel('');
$encryptedBytes = base64_decode($encrypted, true);
if ($encryptedBytes === false) {
throw new RuntimeException('invalid encrypted');
}
$result = $privateKey->decrypt($encryptedBytes);
echo $result . PHP_EOL;
此代码块在浮窗中显示
Go
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"os"
)
func main() {
prefix := "-----BEGIN PRIVATE KEY-----"
suffix := "-----END PRIVATE KEY-----"
encrypted := os.Args[1]
prikey := os.Args[2]
encryptedBytes, err := base64.StdEncoding.DecodeString(encrypted)
if err != nil {
fmt.Fprintln(os.Stderr, "invalid encrypted")
os.Exit(1)
}
key := prefix + "\n" + prikey + "\n" + suffix
result, err := rsaDecrypt(encryptedBytes, []byte(key))
if err != nil {
fmt.Fprintln(os.Stderr, "decrypt failed:", err)
os.Exit(1)
}
fmt.Println(string(result))
}
// rsaDecrypt 使用 RSA OAEP-SHA256 私钥解密。
func rsaDecrypt(encrypted, prikey []byte) ([]byte, error) {
block, _ := pem.Decode(prikey)
if block == nil {
return nil, errors.New("private key error")
}
parsedKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
privateKey, ok := parsedKey.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("invalid private key")
}
// label 传 nil 表示使用空字节串;MGF1 与 OAEP 均使用 SHA-256。
return rsa.DecryptOAEP(sha256.New(), rand.Reader, privateKey, encrypted, nil)
}
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"os"
)
func main() {
prefix := "-----BEGIN PRIVATE KEY-----"
suffix := "-----END PRIVATE KEY-----"
encrypted := os.Args[1]
prikey := os.Args[2]
encryptedBytes, err := base64.StdEncoding.DecodeString(encrypted)
if err != nil {
fmt.Fprintln(os.Stderr, "invalid encrypted")
os.Exit(1)
}
key := prefix + "\n" + prikey + "\n" + suffix
result, err := rsaDecrypt(encryptedBytes, []byte(key))
if err != nil {
fmt.Fprintln(os.Stderr, "decrypt failed:", err)
os.Exit(1)
}
fmt.Println(string(result))
}
// rsaDecrypt 使用 RSA OAEP-SHA256 私钥解密。
func rsaDecrypt(encrypted, prikey []byte) ([]byte, error) {
block, _ := pem.Decode(prikey)
if block == nil {
return nil, errors.New("private key error")
}
parsedKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
privateKey, ok := parsedKey.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("invalid private key")
}
// label 传 nil 表示使用空字节串;MGF1 与 OAEP 均使用 SHA-256。
return rsa.DecryptOAEP(sha256.New(), rand.Reader, privateKey, encrypted, nil)
}
此代码块在浮窗中显示
文档内容是否对您有帮助?