|
|
|
@ -3,12 +3,19 @@ package com.jianshui.common.utils.encrypt; |
|
|
|
|
import sun.misc.BASE64Decoder; |
|
|
|
|
import sun.misc.BASE64Encoder; |
|
|
|
|
|
|
|
|
|
import javax.crypto.BadPaddingException; |
|
|
|
|
import javax.crypto.Cipher; |
|
|
|
|
import javax.crypto.IllegalBlockSizeException; |
|
|
|
|
import javax.crypto.NoSuchPaddingException; |
|
|
|
|
import javax.crypto.spec.IvParameterSpec; |
|
|
|
|
import javax.crypto.spec.SecretKeySpec; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
|
import java.security.InvalidAlgorithmParameterException; |
|
|
|
|
import java.security.InvalidKeyException; |
|
|
|
|
import java.security.MessageDigest; |
|
|
|
|
import java.security.NoSuchAlgorithmException; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 功能描述 |
|
|
|
@ -65,6 +72,77 @@ public class InvoiceEncryptUtil { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String decrypt(String encryptedStr, String mkey) { |
|
|
|
|
byte[] encryptedData = {}; |
|
|
|
|
try { |
|
|
|
|
// 解析Base64编码的字符串
|
|
|
|
|
encryptedData = new BASE64Decoder().decodeBuffer(encryptedStr); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 取密钥和偏转向量
|
|
|
|
|
byte[] key = new byte[8]; |
|
|
|
|
byte[] iv = new byte[8]; |
|
|
|
|
getKeyIV(mkey, key, iv); |
|
|
|
|
SecretKeySpec deskey = new SecretKeySpec(key, "DES"); |
|
|
|
|
IvParameterSpec ivParam = new IvParameterSpec(iv); |
|
|
|
|
|
|
|
|
|
// 使用DES算法解密消息体
|
|
|
|
|
byte[] decryptedData = null; |
|
|
|
|
try { |
|
|
|
|
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); |
|
|
|
|
cipher.init(Cipher.DECRYPT_MODE, deskey, ivParam); |
|
|
|
|
decryptedData = cipher.doFinal(encryptedData); |
|
|
|
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException | |
|
|
|
|
InvalidKeyException | InvalidAlgorithmParameterException | |
|
|
|
|
IllegalBlockSizeException | BadPaddingException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 分离出MD5哈希和原始数据
|
|
|
|
|
byte[] originalData = null; |
|
|
|
|
byte[] calculatedMD5Hash = null; |
|
|
|
|
// 假设MD5哈希占用16字节
|
|
|
|
|
if (decryptedData.length >= 16) { |
|
|
|
|
calculatedMD5Hash = new byte[16]; |
|
|
|
|
originalData = new byte[decryptedData.length - 16]; |
|
|
|
|
System.arraycopy(decryptedData, 0, calculatedMD5Hash, 0, 16); |
|
|
|
|
System.arraycopy(decryptedData, 16, originalData, 0, originalData.length); |
|
|
|
|
} else { |
|
|
|
|
// 数据长度不符合预期,可能被篡改
|
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 验证MD5哈希
|
|
|
|
|
byte[] recalculatedMD5Hash = null; |
|
|
|
|
try { |
|
|
|
|
recalculatedMD5Hash = MD5Hash(originalData, 0, originalData.length); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MD5校验失败
|
|
|
|
|
if (!Arrays.equals(calculatedMD5Hash, recalculatedMD5Hash)) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 将原始数据转换回字符串
|
|
|
|
|
String originalStr = null; |
|
|
|
|
try { |
|
|
|
|
originalStr = new String(originalData, "UTF-8"); |
|
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return originalStr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* <li> |
|
|
|
|
* 方法名称:DES_CBC_Encrypt</li> <li> |
|
|
|
|