|
|
|
@ -23,19 +23,12 @@ import java.security.SecureRandom; |
|
|
|
|
public class AisinoInvoiceEncryptUtil { |
|
|
|
|
|
|
|
|
|
private static final String ALGORITHM = "AES/GCM/NoPadding"; |
|
|
|
|
private static final int KEY_SIZE = 128; // 密钥长度为128位
|
|
|
|
|
private static final int IV_LENGTH = 12; // 初始化向量长度为12字节(96位)
|
|
|
|
|
private static final int TAG_LENGTH = 16; // GCM模式的认证标签长度为16字节
|
|
|
|
|
|
|
|
|
|
/** 加密共有两种方法 一、DES 二、AES*/ |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* <li> |
|
|
|
|
* 方法名称:encrypt</li> <li> |
|
|
|
|
* 加密方法 |
|
|
|
|
* |
|
|
|
|
* @param xmlStr 需要加密的消息字符串 |
|
|
|
|
* @return 加密后的字符串 |
|
|
|
|
*/ |
|
|
|
|
/** 一、DES加密方法*/ |
|
|
|
|
public static String encrypt(String xmlStr, String mkey) { |
|
|
|
|
byte[] encrypt = {}; |
|
|
|
|
|
|
|
|
@ -77,23 +70,7 @@ public class AisinoInvoiceEncryptUtil { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* <li> |
|
|
|
|
* 方法名称:DES_CBC_Encrypt</li> <li> |
|
|
|
|
* 功能描述: |
|
|
|
|
* |
|
|
|
|
* <pre> |
|
|
|
|
* 经过封装的DES/CBC加密算法,如果包含中文,请注意编码。 |
|
|
|
|
* </pre> |
|
|
|
|
* |
|
|
|
|
* </li> |
|
|
|
|
* |
|
|
|
|
* @param sourceBuf 需要加密内容的字节数组。 |
|
|
|
|
* @param deskey KEY 由8位字节数组通过SecretKeySpec类转换而成。 |
|
|
|
|
* @param ivParam IV偏转向量,由8位字节数组通过IvParameterSpec类转换而成。 |
|
|
|
|
* @return 加密后的字节数组 |
|
|
|
|
* @throws Exception |
|
|
|
|
*/ |
|
|
|
|
/** DES加密接口*/ |
|
|
|
|
public static byte[] DES_CBC_Encrypt(byte[] sourceBuf, |
|
|
|
|
SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception { |
|
|
|
|
byte[] cipherByte; |
|
|
|
@ -119,7 +96,6 @@ public class AisinoInvoiceEncryptUtil { |
|
|
|
|
|
|
|
|
|
byte[] ciphertext = cipher.doFinal(sourceBuf); |
|
|
|
|
|
|
|
|
|
// 将初始化向量和密文拼接在一起
|
|
|
|
|
byte[] result = new byte[IV_LENGTH + ciphertext.length]; |
|
|
|
|
System.arraycopy(iv, 0, result, 0, IV_LENGTH); |
|
|
|
|
System.arraycopy(ciphertext, 0, result, IV_LENGTH, ciphertext.length); |
|
|
|
@ -137,7 +113,7 @@ public class AisinoInvoiceEncryptUtil { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** AES加密方法*/ |
|
|
|
|
/** 二、AES加密方法*/ |
|
|
|
|
public static String encryptAES(String xmlStr, String mkey) { |
|
|
|
|
byte[] encrypt = {}; |
|
|
|
|
|
|
|
|
@ -175,23 +151,7 @@ public class AisinoInvoiceEncryptUtil { |
|
|
|
|
return bytesToHex(temp); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* <li> |
|
|
|
|
* 方法名称:MD5Hash</li> <li> |
|
|
|
|
* 功能描述: |
|
|
|
|
* |
|
|
|
|
* <pre> |
|
|
|
|
* MD5,进行了简单的封装,以适用于加,解密字符串的校验。 |
|
|
|
|
* </pre> |
|
|
|
|
* |
|
|
|
|
* </li> |
|
|
|
|
* |
|
|
|
|
* @param buf 需要MD5加密字节数组。 |
|
|
|
|
* @param offset 加密数据起始位置。 |
|
|
|
|
* @param length 需要加密的数组长度。 |
|
|
|
|
* @return |
|
|
|
|
* @throws Exception |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
public static byte[] MD5Hash(byte[] buf, int offset, int length) |
|
|
|
|
throws Exception { |
|
|
|
|
MessageDigest md = MessageDigest.getInstance("MD5"); |
|
|
|
@ -200,21 +160,6 @@ public class AisinoInvoiceEncryptUtil { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* <li> |
|
|
|
|
* 方法名称:addMD5</li> <li> |
|
|
|
|
* 功能描述: |
|
|
|
|
* |
|
|
|
|
* <pre> |
|
|
|
|
* MD校验码 组合方法,前16位放MD5Hash码。 把MD5验证码byte[],加密内容byte[]组合的方法。 |
|
|
|
|
* </pre> |
|
|
|
|
* |
|
|
|
|
* </li> |
|
|
|
|
* |
|
|
|
|
* @param md5Byte 加密内容的MD5Hash字节数组。 |
|
|
|
|
* @param bodyByte 加密内容字节数组 |
|
|
|
|
* @return 组合后的字节数组,比加密内容长16个字节。 |
|
|
|
|
*/ |
|
|
|
|
public static byte[] addMD5(byte[] md5Byte, byte[] bodyByte) { |
|
|
|
|
int length = bodyByte.length + md5Byte.length; |
|
|
|
|
byte[] resutlByte = new byte[length]; |
|
|
|
@ -232,20 +177,6 @@ public class AisinoInvoiceEncryptUtil { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* <li> |
|
|
|
|
* 方法名称:getKeyIV</li> <li> |
|
|
|
|
* 功能描述: |
|
|
|
|
* |
|
|
|
|
* <pre> |
|
|
|
|
* |
|
|
|
|
* </pre> |
|
|
|
|
* </li> |
|
|
|
|
* |
|
|
|
|
* @param encryptKey |
|
|
|
|
* @param key |
|
|
|
|
* @param iv |
|
|
|
|
*/ |
|
|
|
|
public static void getKeyIV(String encryptKey, byte[] key, byte[] iv) { |
|
|
|
|
// 密钥Base64解密
|
|
|
|
|
BASE64Decoder decoder = new BASE64Decoder(); |
|
|
|
@ -277,10 +208,7 @@ public class AisinoInvoiceEncryptUtil { |
|
|
|
|
System.out.println(keyHex); |
|
|
|
|
|
|
|
|
|
String str = "test"; |
|
|
|
|
// String key = "LTEO+oOgWMsuQAOUglqXuQ=="; // 2233
|
|
|
|
|
String key = "c5fdd10b2abd23b6c1c077935da40fca"; // 2233
|
|
|
|
|
// str = encrypt(str, key);
|
|
|
|
|
// System.out.println(str);
|
|
|
|
|
String key = "iAJ9xpsGu1QfOy7ZU0w0IA++"; |
|
|
|
|
System.out.println(encryptAES(str,key)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|