* Copyright (c) 2026 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.huawei.accountkit.realname;
import com.huawei.accountkit.realname.req.GetDetailInfoReq;
import com.huawei.accountkit.realname.resp.GetDetailInfoResp;
import com.huawei.accountkit.util.CallUtils;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.MGF1ParameterSpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.ArrayList;
import java.util.List;
import javax.crypto.Cipher;
import javax.crypto.spec.OAEPParameterSpec;
import javax.crypto.spec.PSource;
* 获取实名信息
*
* @author accountkit
* @since 2026-04-14
*/
public class GetDetailInfoService {
private static final String URL =
"https://openrealname.cloud.huawei.com/rest.php?nsp_svc=OpenRealName.User.getDetailInfo";
* 获取实名信息
*
* @param req GetDetailInfoReq
* @param privateKey 解密接口响应结果所需的私钥,请替换为创建服务账号时分配的私钥(秘钥文件中private_key的值,不含前缀"-----BEGIN PRIVATE
* KEY-----\n",不含后缀"\n-----END PRIVATE KEY-----\n")
* @return GetDetailInfoResp
* @throws IOException IOException
* @throws GeneralSecurityException GeneralSecurityException
*/
public static GetDetailInfoResp call(GetDetailInfoReq req, String privateKey)
throws IOException, GeneralSecurityException {
HttpPost httpPost = new HttpPost(URL);
List<NameValuePair> request = new ArrayList<>();
request.add(new BasicNameValuePair("access_token", req.getAccessToken()));
request.add(new BasicNameValuePair("serviceAccountClientID", req.getServiceAccountClientID()));
request.add(new BasicNameValuePair("serviceAccountKeyID", req.getServiceAccountKeyID()));
request.add(new BasicNameValuePair("queryRangeFlag", req.getQueryRangeFlag()));
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setEntity(new UrlEncodedFormEntity(request));
String res = CallUtils.remoteCall(httpPost);
JSONObject respJson = CallUtils.toJsonObject(res);
String encryptedInfo = respJson.getString("encryptedInfo");
String plainData = decryptByPrivateKey(Base64.decodeBase64(encryptedInfo), privateKey);
return CallUtils.parseObject(plainData, GetDetailInfoResp.class);
}
* 解密响应结果
*
* @param data 密文数据
* @param privateKey 解密接口响应结果所需的私钥,请替换为创建服务账号时分配的私钥(秘钥文件中private_key的值,不含前缀"-----BEGIN PRIVATE
* KEY-----\n",不含后缀"\n-----END PRIVATE KEY-----\n")
* @return 明文数据
* @throws GeneralSecurityException GeneralSecurityException
*/
private static String decryptByPrivateKey(byte[] data, String privateKey) throws GeneralSecurityException {
byte[] bytes = Base64.decodeBase64(privateKey);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
PrivateKey pk = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
OAEPParameterSpec spec =
new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT);
cipher.init(Cipher.DECRYPT_MODE, pk, spec);
return new String(cipher.doFinal(data), StandardCharsets.UTF_8);
}
}