提交 55af4911 authored 作者:  狄熙卜's avatar 狄熙卜

支持帆软加解密(只用默认配置进行加解密)

添加解密报错反馈
上级 51e40f24
...@@ -39,6 +39,32 @@ public class GmSm4CryptExecutor implements CryptExecutor { ...@@ -39,6 +39,32 @@ public class GmSm4CryptExecutor implements CryptExecutor {
} }
} }
public static String encrypt(String decryptString) {
if (StringUtils.isBlank(decryptString)) {
return decryptString;
} else if (StringUtils.startsWithIgnoreCase(decryptString, "4mspamtg")) {
return decryptString;
} else {
String separatorChars = CryptUtil.getSeparatorChars(decryptString);
if (!StringUtils.isNotEmpty(separatorChars)) {
return GmSm4Util.encryptEcb(decryptString);
} else {
String[] paramArray = StringUtils.split(decryptString, separatorChars);
StringBuilder encryptParam = new StringBuilder();
String[] var6 = paramArray;
int var7 = paramArray.length;
for(int var8 = 0; var8 < var7; ++var8) {
String temp = var6[var8];
encryptParam.append(GmSm4Util.encryptEcb(temp)).append(separatorChars);
}
return StringUtils.substring(encryptParam.toString(), 0, encryptParam.length() - 1);
}
}
}
private static boolean needDecrypt(String data,CryptProperties cryptProperties) { private static boolean needDecrypt(String data,CryptProperties cryptProperties) {
if (StringUtils.isNotBlank(cryptProperties.getSm4Salt()) ) { if (StringUtils.isNotBlank(cryptProperties.getSm4Salt()) ) {
if(StringUtils.startsWithIgnoreCase(data, cryptProperties.getSm4Salt())){ if(StringUtils.startsWithIgnoreCase(data, cryptProperties.getSm4Salt())){
...@@ -84,4 +110,34 @@ public class GmSm4CryptExecutor implements CryptExecutor { ...@@ -84,4 +110,34 @@ public class GmSm4CryptExecutor implements CryptExecutor {
} }
} }
} }
public static String decrypt(String encrytString) {
if (StringUtils.isBlank(encrytString)) {
return encrytString;
} else if (!StringUtils.startsWithIgnoreCase(encrytString, "4mspamtg")) {
return encrytString;
}else{
try {
String separatorChars = CryptUtil.getSeparatorChars(encrytString);
if (!StringUtils.isNotEmpty(separatorChars)) {
return GmSm4Util.decryptEcb(encrytString);
} else {
String[] paramArray = StringUtils.split(encrytString, separatorChars);
StringBuilder decryptParam = new StringBuilder();
String[] var6 = paramArray;
int var7 = paramArray.length;
for(int var8 = 0; var8 < var7; ++var8) {
String temp = var6[var8];
decryptParam.append(GmSm4Util.decryptEcb(temp)).append(separatorChars);
}
return StringUtils.substring(decryptParam.toString(), 0, decryptParam.length() - 1);
}
} catch (Exception var10) {
var10.printStackTrace();
return encrytString;
}
}
}
} }
...@@ -6,9 +6,12 @@ import org.apache.commons.lang3.StringUtils; ...@@ -6,9 +6,12 @@ import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils; import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator; import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Key; import java.security.Key;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.security.Security; import java.security.Security;
...@@ -198,8 +201,26 @@ public class GmSm4Util { ...@@ -198,8 +201,26 @@ public class GmSm4Util {
} }
public static byte[] decrypt_Ecb_Padding(byte[] key, byte[] cipherText) throws Exception { public static byte[] decrypt_Ecb_Padding(byte[] key, byte[] cipherText) throws Exception {
Cipher cipher = generateEcbCipher("SM4/ECB/PKCS5Padding", 2, key); Cipher cipher = null;
return cipher.doFinal(cipherText); try {
cipher = generateEcbCipher("SM4/ECB/PKCS5Padding", 2, key);
} catch (Exception e) {
e.printStackTrace();
return "加密字段错误,请联系管理员检查,error:0".getBytes("UTF-8");
}
if(cipher != null) {
try {
return cipher.doFinal(cipherText);
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
return "加密字段错误,请联系管理员检查,error:1".getBytes("UTF-8");
} catch (BadPaddingException e) {
e.printStackTrace();
return "加密字段错误,请联系管理员检查,error:2".getBytes("UTF-8");
}
}else{
return "加密字段错误,请联系管理员检查,error:3".getBytes("UTF-8");
}
} }
public static boolean verifyEcb(String hexKey, String cipherText, String paramStr) throws Exception { public static boolean verifyEcb(String hexKey, String cipherText, String paramStr) throws Exception {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论