提交 fbb2eec0 authored 作者: inroi's avatar inroi

微调

上级 1294d557
......@@ -43,7 +43,7 @@ public class LicVerificationController {
@ApiOperation("Lic在线验证")
@PostMapping("/lic/online/verification")
public RestResponse licOnlineVerification(@RequestBody LicOnlineVerificationInVo inVo) {
public String licOnlineVerification(@RequestBody LicOnlineVerificationInVo inVo) {
return licVerificationService.licOnlineVerification(inVo);
}
......
package com.system.serivce;
import com.system.transfer.response.RestResponse;
import com.system.transfer.verification.*;
/**
......@@ -37,7 +36,7 @@ public interface ILicVerificationService {
* @param inVo 条件
* @return 结果
*/
RestResponse licOnlineVerification(LicOnlineVerificationInVo inVo);
String licOnlineVerification(LicOnlineVerificationInVo inVo);
}
......@@ -60,13 +60,17 @@ public class LicVerificationServiceImpl implements ILicVerificationService {
}
@Override
public RestResponse licOnlineVerification(LicOnlineVerificationInVo inVo) {
public String licOnlineVerification(LicOnlineVerificationInVo inVo) {
String result;
try {
result = RsaUtil.decryptByPrivateKeyToLong(inVo.getData(), rsaKeyConstant.getPrivateKey());
} catch (Exception e) {
e.printStackTrace();
return RestResponse.fail("Lic文件信息解密失败");
try {
return RsaUtil.encryptByPrivateKeyToLong(JSON.toJSONString(RestResponse.fail("Lic文件信息解密失败")), rsaKeyConstant.getPrivateKey());
} catch (Exception ex) {
return "";
}
}
LicOnlineVerificationLogCreateInVo request = new LicOnlineVerificationLogCreateInVo();
......@@ -93,8 +97,23 @@ public class LicVerificationServiceImpl implements ILicVerificationService {
request.setResponse(JsonUtil.toString(response));
}
try {
response.setData(RsaUtil.sign(rsaKeyConstant.getPrivateKey(), JsonUtil.toString(response)));
} catch (Exception e) {
e.printStackTrace();
try {
return RsaUtil.encryptByPrivateKeyToLong(JSON.toJSONString(RestResponse.fail("Lic文件信息签名失败")), rsaKeyConstant.getPrivateKey());
} catch (Exception ex) {
return "";
}
}
licVerificationMapper.licOnlineVerificationLogCreate(request);
return response;
try {
return RsaUtil.encryptByPrivateKeyToLong(JSON.toJSONString(response), rsaKeyConstant.getPrivateKey());
} catch (Exception ex) {
return "";
}
}
private RestResponse purchaseToResult(Map<String, Object> map) {
......
......@@ -3,9 +3,7 @@ package com.system.utils;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
......@@ -23,6 +21,11 @@ public class RsaUtil {
public static final String KEY_ALGORITHM = "RSA";
/**
* 签名算法
*/
public static final String SIGNATURE_ALGORITHM = "SHA256withRSA";
/**
* RSA最大加密明文大小
*/
private static final int MAX_ENCRYPT_BLOCK = 117;
......@@ -191,5 +194,39 @@ public class RsaUtil {
return new String(outputStream.toByteArray());
}
/**
* 私钥签名
*/
public static String sign(String privateKey, String plainText) throws Exception {
//64位解码加密后的字符串
byte[] byteKey = Base64.getDecoder().decode(privateKey);
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(byteKey);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//签名
PrivateKey key = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(key);
signature.update(plainText.getBytes());
return Base64.getEncoder().encodeToString(signature.sign());
}
/**
* 公钥验签
*/
public static boolean verifySign(String publicKey, String plainText, String signStr) throws Exception {
//64位解码加密后的字符串
byte[] byteKey = Base64.getDecoder().decode(publicKey);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(byteKey);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//验签
PublicKey key = keyFactory.generatePublic(x509EncodedKeySpec);
Signature verifySign = Signature.getInstance(SIGNATURE_ALGORITHM);
verifySign.initVerify(key);
verifySign.update(plainText.getBytes());
return verifySign.verify(Base64.getDecoder().decode(signStr));
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论