提交 3978e74d authored 作者:  狄熙卜's avatar 狄熙卜

修改解密报错输出

上级 27fea82b
...@@ -155,7 +155,7 @@ public class GmSm4Util { ...@@ -155,7 +155,7 @@ public class GmSm4Util {
} }
} }
public static String decryptWithUserSalt(String hexKey, String cipherText, String salt, String encoding) throws Exception { public static String decryptWithUserSalt(String hexKey, String cipherText, String salt, String encoding) {
if (hexKey.length() == 16) { if (hexKey.length() == 16) {
hexKey = toHexString(hexKey); hexKey = toHexString(hexKey);
} }
...@@ -163,64 +163,83 @@ public class GmSm4Util { ...@@ -163,64 +163,83 @@ public class GmSm4Util {
byte[] keyData = ByteUtils.fromHexString(hexKey); byte[] keyData = ByteUtils.fromHexString(hexKey);
byte[] cipherData; byte[] cipherData;
byte[] srcData; byte[] srcData;
String result = "";
if (StringUtils.startsWithIgnoreCase(cipherText, salt)) { if (StringUtils.startsWithIgnoreCase(cipherText, salt)) {
if (StringUtils.endsWith(cipherText, "等")) { if (StringUtils.endsWith(cipherText, "等")) {
String realCipherText = StringUtils.removeEnd(StringUtils.removeStartIgnoreCase(cipherText, salt), "等"); String realCipherText = StringUtils.removeEnd(StringUtils.removeStartIgnoreCase(cipherText, salt), "等");
srcData = ByteUtils.fromHexString(realCipherText); srcData = ByteUtils.fromHexString(realCipherText);
try {
srcData = decrypt_Ecb_Padding(keyData, srcData); srcData = decrypt_Ecb_Padding(keyData, srcData);
return new String(srcData, encoding) + "等"; result = new String(srcData, encoding) + "等";
}catch (Exception e){
LOGGER.error("解密失败,对应的密文内容为 "+cipherText);
}
return result;
} else { } else {
cipherData = ByteUtils.fromHexString(StringUtils.removeStartIgnoreCase(cipherText, salt)); cipherData = ByteUtils.fromHexString(StringUtils.removeStartIgnoreCase(cipherText, salt));
try {
srcData = decrypt_Ecb_Padding(keyData, cipherData); srcData = decrypt_Ecb_Padding(keyData, cipherData);
return new String(srcData, encoding); result = new String(srcData, encoding);
}catch (Exception e){
LOGGER.error("解密失败,对应的密文内容为 "+cipherText);
}
return result;
} }
} else { } else {
cipherData = ByteUtils.fromHexString(cipherText); cipherData = ByteUtils.fromHexString(cipherText);
try {
srcData = decrypt_Ecb_Padding(keyData, cipherData); srcData = decrypt_Ecb_Padding(keyData, cipherData);
return new String(srcData, encoding); result = new String(srcData, encoding);
}catch (Exception e){
LOGGER.error("解密失败,对应的密文内容为 "+cipherText);
}
return result;
} }
} }
public static String decryptEcb(String cipherText) throws Exception { public static String decryptEcb(String cipherText){
byte[] keyData = ByteUtils.fromHexString("645B0A010D939BD614A620BCAF645470"); byte[] keyData = ByteUtils.fromHexString("645B0A010D939BD614A620BCAF645470");
byte[] cipherData; byte[] cipherData;
byte[] srcData; byte[] srcData;
String result = "";
if (StringUtils.startsWithIgnoreCase(cipherText, SaltsEnum.GM_SM4_SALTS.getSalt())) { if (StringUtils.startsWithIgnoreCase(cipherText, SaltsEnum.GM_SM4_SALTS.getSalt())) {
if (StringUtils.endsWith(cipherText, "等")) { if (StringUtils.endsWith(cipherText, "等")) {
String realCipherText = StringUtils.removeEnd(StringUtils.removeStartIgnoreCase(cipherText, SaltsEnum.GM_SM4_SALTS.getSalt()), "等"); String realCipherText = StringUtils.removeEnd(StringUtils.removeStartIgnoreCase(cipherText, SaltsEnum.GM_SM4_SALTS.getSalt()), "等");
srcData = ByteUtils.fromHexString(realCipherText); srcData = ByteUtils.fromHexString(realCipherText);
try{
srcData = decrypt_Ecb_Padding(keyData, srcData); srcData = decrypt_Ecb_Padding(keyData, srcData);
return new String(srcData, "UTF-8") + "等"; result = new String(srcData, "UTF-8") + "等";
}catch (Exception e){
LOGGER.error("解密失败,对应的密文内容为 "+cipherText);
}
return result;
} else { } else {
cipherData = ByteUtils.fromHexString(StringUtils.removeStartIgnoreCase(cipherText, SaltsEnum.GM_SM4_SALTS.getSalt())); cipherData = ByteUtils.fromHexString(StringUtils.removeStartIgnoreCase(cipherText, SaltsEnum.GM_SM4_SALTS.getSalt()));
try{
srcData = decrypt_Ecb_Padding(keyData, cipherData); srcData = decrypt_Ecb_Padding(keyData, cipherData);
return new String(srcData, "UTF-8"); result = new String(srcData, "UTF-8");
}catch (Exception e){
LOGGER.error("解密失败,对应的密文内容为 "+cipherText);
}
return result;
} }
} else { } else {
cipherData = ByteUtils.fromHexString(cipherText); cipherData = ByteUtils.fromHexString(cipherText);
try{
srcData = decrypt_Ecb_Padding(keyData, cipherData); srcData = decrypt_Ecb_Padding(keyData, cipherData);
return new String(srcData, "UTF-8"); result = new String(srcData, "UTF-8");
}catch (Exception e){
LOGGER.error("解密失败,对应的密文内容为 "+cipherText);
}
return result;
} }
} }
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 = null; Cipher cipher = null;
try {
cipher = generateEcbCipher("SM4/ECB/PKCS5Padding", 2, key); cipher = generateEcbCipher("SM4/ECB/PKCS5Padding", 2, key);
} catch (Exception e) {
e.printStackTrace();
return "加密字段错误,请联系管理员检查,error:0".getBytes("UTF-8");
}
if(cipher != null) { if(cipher != null) {
try {
return cipher.doFinal(cipherText); return cipher.doFinal(cipherText);
} catch (IllegalBlockSizeException e) {
LOGGER.error("解密失败,IllegalBlockSizeException");
throw e;
} catch (BadPaddingException e) {
LOGGER.error("解密失败,BadPaddingException");
throw e;
}
}else{ }else{
LOGGER.info("解密失败"); LOGGER.info("解密失败");
return "加密字段错误,请联系管理员检查,error:3".getBytes("UTF-8"); return "加密字段错误,请联系管理员检查,error:3".getBytes("UTF-8");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论