package up.cpc.gw.test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import up.cpc.gw.entity.CommonException;
import up.cpc.gw.utils.SignatureUtil;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import static up.cpc.gw.utils.SignatureUtil.rsaCheckContent;
public class TestDemo {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(TestDemo.class);
//用于获取已方私钥
String upSecretPrivatekey = "";
String upDesKey = "Wnl3Z0Y3SmhiSTM3eDhNTw==";
//用于获取已方公钥钥进行验签
String spPublicKey = "";
//服务商加密解密私钥
String rsa_private_key_2048_pkcs8 = "";
//服务商加签验签公钥
String rsa_public_key_2048_pkcs8 = "";
//服务商用于加密 已方用于解密 秘钥
String spDeskey = "UVXVr6bpSgT8h9g0KKhjFwYm";
//将报文明文通过spDeskey进行加密
String content = "";
//时间戳和随机字符串
String timestamp = "1507709906";
String nonce = "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2p";
//报文明文
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", "u2fdjfi1");
jsonObject.put("scope", "AC");
String jsonString = jsonObject.toJSONString();
String srcStr = jsonString;
//报文敏明文装入map中
Map respBodyMap = new HashMap();
respBodyMap.put("code", "u2fdjfi1");
respBodyMap.put("scope", "AC");
//拼接字符串
String toSignContent = timestamp + "\n" + nonce_str + "\n" + jsonString+ "\n";
// String toSignContent = timestamp + “\n” + nonce + “\n” + JSON.toJSONString(respBodyMap) + “\n”;
String toSignContent = “123”;
//使用spDeskey加密字符串
// String encodeStr = encode3Des(spDeskey, srcStr);
//rsa_private_key_2048_pkcs8加密字符串
// String encodeStr = encode3Des(rsa_private_key_2048_pkcs8, srcStr);
//解密
Map
try {
contentMap = JSON.parseObject(SignatureUtil.decode3Des(upDesKey, content));
} catch (Exception e) {
}
//加签
String respSignature = null;
String RsaPrivateKey = SignatureUtil.decode3Des(upDesKey, upSecretPrivatekey);
RsaPrivateKey="";
try {
respSignature = SignatureUtil.rsa256Sign(toSignContent, RsaPrivateKey, "SHA256WithRSA", "UTF-8");
// respSignature = SignatureUtil.rsa256Sign(toSignContent, RsaPrivateKey, “RSA”, “UTF-8”);
} catch (Exception e) {
respBodyMap.put("respCd", "01");
respBodyMap.put("respMsg", "校企签名失败");
}
logger.info("签名---------------------"+respSignature);
//验签
boolean sign = false;
try {
sign = SignatureUtil.rsa256CheckContent(toSignContent, respSignature, spPublicKey, “UTF-8”);
// sign = SignatureUtil.rsa256Sign(toSignContent, spPublicKey, “SHA256WithRSA”, “UTF-8”);
} catch (CommonException e) {
logger.info("=身份认证验签,不通过 commonException=");
} catch (Exception e) {
logger.info("=身份验证验签,不通过 Exception=");
}
}
public static String decode3Des(String key, String desStr) {
Base64 base64 = new Base64();
byte[] keybyte = hex(key);
byte[] src = base64.decode(desStr);
try {
SecretKey deskey = new SecretKeySpec(keybyte, "DESede");
Cipher c1 = Cipher.getInstance("DESede");
c1.init(2, deskey);
byte[] pwd = c1.doFinal(src);
return new String(pwd);
} catch (NoSuchAlgorithmException var8) {
var8.printStackTrace();
} catch (NoSuchPaddingException var9) {
var9.printStackTrace();
} catch (Exception var10) {
var10.printStackTrace();
}
return null;
}
public static byte[] hex(String key) {
String f = DigestUtils.md5Hex(key);
byte[] bkeys = (new String(f)).getBytes();
byte[] enk = new byte[24];
for (int i = 0; i < 24; ++i) {
enk[i] = bkeys[i];
}
return enk;
}
/**
* 加密
*
* @param key
* @param srcStr
* @return
*/
public static String encode3Des(String key, String srcStr) {
byte[] keybyte = hex(key);
byte[] src = srcStr.getBytes();
try {
//生成密钥
SecretKey deskey = new SecretKeySpec(keybyte, "DESede");
//加密
Cipher c1 = Cipher.getInstance("DESede");
c1.init(Cipher.ENCRYPT_MODE, deskey);
String pwd = Base64.encodeBase64String(c1.doFinal(src));
// return c1.doFinal(src);//在单一方面的加密或解密
return pwd;
} catch (java.security.NoSuchAlgorithmException e1) {
// TODO: handle exception
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (Exception e3) {
e3.printStackTrace();
}
return null;
}
}