求大师java SHA256WithRSA 以下代码 转C#
签名规则:
(1)、将参数key以ASCII码表顺序排序
(2)、以key1=value1&key2=value2的方式拼接签名字符串,其中剔除空值和sign字段
(3)、以sha256算法对签名字符串进行哈希得到hash
(4)、用RSASSA-PKCS1-V1_5-SIGN对hash进行签名
问题相关代码
(1)、将参数key以ASCII码表顺序排序
(2)、以key1=value1&key2=value2的方式拼接签名字符串,其中剔除空值和sign字段
(3)、以sha256算法对签名字符串进行哈希得到hash
(4)、用RSASSA-PKCS1-V1_5-SIGN对hash进行签名
转成.net代码
package com.inspur.icity;
import org.json.JSONObject;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.util.*;
public class AuthUtil {
private static final String BASE_URL = "https://app.zwfw.nmg.goc.cn:4443";
public static class AuthToken {
private String token;
private String refreshToken;
private Date tokenExpiredTime;
private Date refreshTokenExpiredTime;
private String openId;
public AuthToken() {
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getRefreshToken() {
return refreshToken;
}
public void setRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}
public Date getTokenExpiredTime() {
return tokenExpiredTime;
}
public void setTokenExpiredTime(Date tokenExpiredTime) {
this.tokenExpiredTime = tokenExpiredTime;
}
public Date getRefreshTokenExpiredTime() {
return refreshTokenExpiredTime;
}
public void setRefreshTokenExpiredTime(Date refreshTokenExpiredTime) {
this.refreshTokenExpiredTime = refreshTokenExpiredTime;
}
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
}
public static class UserFaceInfo {
private String realName;
private String idCard;
private String openId;
private String faceUrl;
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
public String getFaceUrl() {
return faceUrl;
}
public void setFaceUrl(String faceUrl) {
this.faceUrl = faceUrl;
}
}
public static class UserAuthInfo {
private String realName;
private String idCard;
private String openId;
private String checkPhone;
private String mobilePhone;
public String getMobilePhone() {
return mobilePhone;
}
public void setMobilePhone(String mobilePhone) {
this.mobilePhone = mobilePhone;
}
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
public String getCheckPhone() {
return checkPhone;
}
public void setCheckPhone(String checkPhone) {
this.checkPhone = checkPhone;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
}
/**
* 使用code 获取token
*
* @param appId 应用appId
* @param code 获取到的code
* @param privateKey 应用私钥
* @return AuthToken
* @throws Exception 签名失败或者返回值出错
*/
public static AuthToken getTokenByCode(String appId, String code, String privateKey) throws Exception {
Map<String, String> params = new HashMap<>();
params.put("app_id", appId);
params.put("code", code);
params.put("grant_type", "authorization_code");
params.put("sign", getSign(params, privateKey));
String res = OkHttp3ClientUtil.getInstance().postSync(BASE_URL + "/icity/auth2.0/token", null, params);
return parseAuthTokenResult(res);
}
public static AuthToken getServiceToken(String appId, String privateKey) throws Exception {
Map<String, String> params = new HashMap<>();
params.put("app_id", appId);
params.put("grant_type", "service_token");
params.put("sign", getSign(params, privateKey));
String res = OkHttp3ClientUtil.getInstance().postSync(BASE_URL + "/icity/auth2.0/token", null, params);
return parseAuthTokenResult(res);
}
/**
* 使用refresh_token刷新token
* refresh_token存在过期的情况,调用者应处理refresh_token过期时的 TokenExpiredException
*
* @param appId 应用appId
* @param refreshToken 之前获取的refresh_token
* @param privateKey 应用私钥
* @return AuthToken
* @throws Exception
*/
public static AuthToken refreshToken(String appId, String refreshToken, String privateKey) throws Exception {
Map<String, String> params = new HashMap<>();
params.put("app_id", appId);
params.put("grant_type", "refresh_token");
params.put("refresh_token", refreshToken);
params.put("sign", getSign(params, privateKey));
String res = OkHttp3ClientUtil.getInstance().postSync(BASE_URL + "/icity/auth2.0/token", null, params);
JSONObject result = new JSONObject(res);
if (result.optInt("code") == 1008) {
throw new TokenExpiredException("refresh_token已过期");
}
return parseAuthTokenResult(res);
}
/**
* 当使用token获取用户信息时候,token存在过期的情况,如果token过期
* 此时应重新授权,获取使用refresh_token刷新,
* 调用者应处理token过期时的 TokenExpiredException
*/
public static UserAuthInfo getUserAuthInfo(String appId, String token, String privateKey) throws Exception {
Map<String, String> params = getRequestInfoParams(appId, token, privateKey);
String res = OkHttp3ClientUtil.getInstance().getSync(BASE_URL + "/icity/auth2.0/get_user_info", null, params);
System.out.println(res);
JSONObject result = new JSONObject(res);
int code = result.optInt("code");
if (code == 1000) {
JSONObject data = result.optJSONObject("data");
JSONObject authInfo = data.getJSONObject("auth_info");
UserAuthInfo userAuthInfo = new UserAuthInfo();
userAuthInfo.setIdCard(authInfo.optString("id_card"));
userAuthInfo.setRealName(authInfo.optString("real_name"));
userAuthInfo.setCheckPhone(authInfo.optString("check_phone"));
userAuthInfo.setMobilePhone(authInfo.optString("mobile_phone"));
userAuthInfo.setOpenId(authInfo.optString("open_id"));
return userAuthInfo;
} else if (code == 1008) {//token已过期
throw new TokenExpiredException("token已过期");
} else {
throw new Exception("getUserFaceAuthInfo error,result " + res);
}
}
private static Map<String, String> getRequestInfoParams(String appId, String token, String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException {
Map<String, String> params = new HashMap<>();
params.put("app_id", appId);
params.put("auth_token", token);
params.put("sign", getSign(params, privateKey));
return params;
}
private static AuthToken parseAuthTokenResult(String res) throws Exception {
JSONObject result = new JSONObject(res);
System.out.println(res);
if (result.optInt("code") == 1000) {
JSONObject data = result.optJSONObject("data");
JSONObject authTokenJson = data.optJSONObject("auth_token");
AuthToken authToken = new AuthToken();
authToken.setToken(authTokenJson.optString("auth_token"));
authToken.setRefreshToken(authTokenJson.optString("refresh_token"));
authToken.setTokenExpiredTime(new Date(authTokenJson.optLong("token_expired_time")));
authToken.setRefreshTokenExpiredTime(new Date(authTokenJson.optLong("refresh_token_expired_time")));
authToken.setOpenId(authTokenJson.optString("open_id"));
return authToken;
} else {
throw new Exception("get token by code error,result is :" + res);
}
}
public static String getSign(Map<String, String> params, String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException {
List<String> keys = new ArrayList<>();
for (String k : params.keySet()) {
if (params.get(k) != null && params.get(k).length() > 0) {
keys.add(k);
}
}
Collections.sort(keys);
StringBuilder signBuilder = new StringBuilder();
int count = 0;
for (String k : keys) {
if (count > 0) {
signBuilder.append("&");
}
signBuilder.append(k);
signBuilder.append("=");
signBuilder.append(params.get(k));
count++;
}
return RsaSignUtil.sign(privateKey, signBuilder.toString());
}
}