IntelligentRecognition/ah-jjsp-service/.svn/pristine/e8/e8dc831ed0fc7d5a4b5692a11e1...

106 lines
4.6 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.securityControl.auth.igwUtils;
import com.aostarit.smcrypto.CipherMode;
import com.aostarit.smcrypto.Sm2Utils;
import com.aostarit.smcrypto.Sm3Utils;
import com.aostarit.smcrypto.Sm4Utils;
import com.securityControl.auth.pojo.UnionUserCodeRes;
import java.util.Random;
/**
* 加解密工具文件传输使用
*
* @author shimengran
*/
public class EnDecryptUtil {
// --> isc app id
private static String ISC_APP_ID = "6a838ac78eb84da5875bb8063f41f124";
// --> signKey
private static String SM2_PRIVATE_KEY = "00E5B6E95395679A70558B23582AA878270238342B91ADC19D49EAD112A7D29E";
// --> isc secret
private static String SM4_KEY = "d9a27f830a67409893f200e55f88a711";
private EnDecryptUtil(){}
private static final Random random = new Random();
private static final Sm2Utils SM2_UTILS = new Sm2Utils(CipherMode.C1C2C3);
/***
* 传输加密
*
* @param context 原始明文
* @param sm4key SM4密钥
* @param sm2publicKey SM2公钥
* @return
* @throws Exception
*/
public static String transferEncrypt(String context, String sm4key, String sm2publicKey) throws Exception {
//对整个传输JSON使用SM4进行加密
context = Sm4Utils.ECB.encryptFromText(context, sm4key);
//再将加密后的消息体进行SM3摘要加密
String sm3_sm4cont = Sm3Utils.encryptFromText(context).toUpperCase();
// 拼装传输原文为 SM3摘要结果 8位随机字符 SM4密文
String sign = sm3_sm4cont + "|" + randomStr() + "|" + context;
return SM2_UTILS.encryptASN1FromText(sm2publicKey, sign);
}
/****
* 传输解密
*
* @param encontext 原始密文
* @param sm4key SM4密钥
* @param sm2privateKey SM2密钥
* @return
* @throws Exception
*/
public static String transferDecrypt(String encontext, String sm4key, String sm2privateKey) throws Exception {
int length=3;
String sourceData = SM2_UTILS.decryptASN1ToText(sm2privateKey, encontext);
String[] ctxq = sourceData.split("\\|");
if (ctxq.length != length){
throw new Exception("Transfer sign encrypt nonconforming to specifications");
}
if (!ctxq[0].equals(Sm3Utils.encryptFromText(ctxq[2]).toUpperCase())){
throw new Exception("Transfer sign verify failure");
}
return Sm4Utils.ECB.decryptToText(ctxq[2], sm4key);
}
private static String paraseSm2Source(String sourceData, String sm4key) throws Exception {
int length=3;
String[] ctxq = sourceData.split("\\|");
if (ctxq.length != length){
throw new Exception("Transfer sign encrypt nonconforming to specifications");
}
if (!ctxq[0].equals(Sm3Utils.encryptFromText(ctxq[2]).toUpperCase())){
throw new Exception("Transfer sign verify failure");
}
return Sm4Utils.ECB.decryptToText(ctxq[2], sm4key);
}
public static String randomStr() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 8; i++) {
builder.append(random.nextInt(10));
}
return builder.toString();
}
public static void main(String[] args) throws Exception {
String data = "308201D402205F33FB05ECF187326635B62FC387B8DB2F854EEACE7F45992E86AB5EB65C60890220371C2EAAD9386E9A61C3D8ACA7122696148CE7F1ACC8CBCCFA6902FB5D099E8304202B258EFDE35B38EE4513E26F757F68E9E3D4E98B01794422172EE265AA1678FC0482016A97D5D51A1CA452CD3446F79059C39E5A01B33E426D684B18A52C466152E794D07659348371379D191EA2D4287A28747E1461669EA9B5EB1B734FE84D8CACACCD04DB24B86426AA287F16A2FC4C74E193F9F84AC29A218E34745A09A2ABFE02171A067CB40669FE7BAE629BABE14E2FA737E019701E5E91D7899266207ADAC16229D16A6A1E278AED863A9F63E34E036EF1CDD66D43B15CF64C10CB656DFB37190EC7BE9A9740952DF51E37DF2CE04FC90DF14F73B221EE51E735BED6FFFEE220C6F2201D26F61A15706FE63BEEC56CA567CBB444FF3DA716E3CCF3963420D56F92C88860D0C56145B9FE7D759EEC19B7C8121C44DD95685D40795E69A57EE18AC876FE5D5FAD047588BDC6DEF47A2CC19ABBFAAC08ED7FDC7EFFD6D0EC656A15245D5E0C94C08D8626545833A2098261DACA58226E54E2E41B6CC2C6AF9D1138A5D8477B4293B05852FC3D22166050804D660CBDAAB4373207656C59C6652C8534BFFF4A229CFC38E42F";
String res = EnDecryptUtil.transferDecrypt(data, SM4_KEY, SM2_PRIVATE_KEY);
UnionUserCodeRes unionUserCodeRes = SerUtil.fromJson(res, UnionUserCodeRes.class);
System.err.println(unionUserCodeRes.getCode());
System.err.println(unionUserCodeRes.getId());
System.err.println(unionUserCodeRes.getTime());
System.err.println(unionUserCodeRes.getName());
}
}