package com.jzbox.util;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jzbox.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
/**
-
极光解密
*/
@Slf4j
public class AuroraUserLogin {
// 私钥
public static final String prikey = “”;/**
-
获取phone
-
@param loginToken
-
@return
-
@throws Exception
*/
public static String decrypt(String loginToken) throws Exception {
String host = “https://api.verification.jpush.cn/v1/web/loginTokenVerify”;
String appKey = “”;
String Secret = “”;
Map<String, Object> map = new HashMap<>();
map.put(“loginToken”, loginToken);
map.put(“exID”, null);Map<String, String> headers = new HashMap<String, String>();
headers.put(“Authorization”,“Basic ${base64_auth_string}”);
headers.put(“Content-Type”, “application/json; charset=UTF-8”);String result = HttpRequest.post(host).header(“Authorization”, "Basic " + Base64.getUrlEncoder().
encodeToString((appKey + “:” + Secret).getBytes())).
header(“Content-Type”,“application/json; charset=UTF-8”)
.body(JSON.toJSONString(map))
.execute().body();log.info(“result:[{}]”,result);
JSONObject jsonObject = JSON.parseObject(result);
log.info(“获取phone:{}”,JSON.toJSON(jsonObject));
String cryptograph_phone = (String) jsonObject.get(“phone”);if(StrUtil.isBlank(cryptograph_phone)){
throw new BizException(“极光认证出错”);
}
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(prikey));
PrivateKey privateKey = KeyFactory.getInstance(“RSA”).generatePrivate(keySpec);Cipher cipher = Cipher.getInstance(“RSA”);
cipher.init(Cipher.DECRYPT_MODE, privateKey);byte[] b = Base64.getDecoder().decode(cryptograph_phone);
return new String(cipher.doFinal(b));
}
-
}