package com.test.main.b2b;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;
public class 支付回调解密 {
public static String encodingAesKey = "sdIeXprnCzbVmDIp";
public static String encryptedData = "urMQfiTkA+nLIV/gpAxwQaFurMQfiTkA+nLIV/gpAxwQaFurMQfiTkA+nLIV/gpAxwQaFurMQfiTkA+nLIV/gpAxwQaFurMQfiTkA+nLIV/gpAxwQaFurMQfiTkA+nLIV/gpAxwQaF";
public static void main(String[] args) throws Exception {
String decrypt = decrypt(encryptedData, encodingAesKey);
System.out.println(decrypt);
}
public static String decrypt(String encryptedMsg, String encodingAesKey) throws Exception {
byte[] aesKey = Base64.decodeBase64(encodingAesKey + "=");
byte[] original;
try {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec key_spec = new SecretKeySpec(aesKey, "AES");
IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey, 0, 16));
cipher.init(Cipher.DECRYPT_MODE, key_spec, iv);
byte[] encrypted = Base64.decodeBase64(encryptedMsg);
original = cipher.doFinal(encrypted);
} catch (Exception e) {
e.printStackTrace();
throw new AesException(AesException.DecryptAESError);
}
String xmlContent;
try {
byte[] bytes = PKCS7Encoder.decode(original);
byte[] networkOrder = Arrays.copyOfRange(bytes, 16, 20);
int xmlLength = recoverNetworkBytesOrder(networkOrder);
xmlContent = new String(Arrays.copyOfRange(bytes, 20, 20 + xmlLength), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
throw new AesException(AesException.IllegalBuffer);
}
return xmlContent;
}
static int recoverNetworkBytesOrder(byte[] orderBytes) {
int sourceNumber = 0;
for (int i = 0; i < 4; i++) {
sourceNumber <<= 8;
sourceNumber |= orderBytes[i] & 0xff;
}
return sourceNumber;
}
}