工具类代码
<p>[TOC]</p>
<h5>FileMD5util 工具类代码</h5>
<pre><code>public class FileMd5Util {
public static String getFileContentMD5(File file) {
// 获取文件MD5的二进制数组(128位)
String md5Str = null;
FileInputStream fis;
try {
fis = new FileInputStream(file);
byte[] bytes = getFileMD5Bytes128(fis);
// 对文件MD5的二进制数组进行base64编码
Assert.isTrue(bytes.length &gt; 0, &quot;无效的文件MD5串&quot;);
md5Str = Base64.encodeBase64String(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return md5Str;
}
public static byte[] getFileMD5Bytes128(FileInputStream fis) {
try {
MessageDigest md5 = MessageDigest.getInstance(&quot;MD5&quot;);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer, 0, 1024)) != -1) {
md5.update(buffer, 0, length);
}
return md5.digest();
} catch (Exception e) {
e.printStackTrace();
return new byte[]{};
} finally {
if (Objects.nonNull(fis)) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}</code></pre>
<h5>加解密 工具类代码</h5>
<ul>
<li>
<p>CreditCryptoUtils</p>
<pre><code>public class CreditCryptoUtils {
/**
* 解密
*
* @param param 请求内容(包含 key、content、sign、timestamp、orgCode)
* @param prikey RSA 私钥(用于解密 AES 密钥)
* @param md5key 签名密钥(用于校验数据完整性)
* @return 解密后的数据
*/
public static String decryption(RequestContent param, String md5key, String prikey) {
try {
// 1️⃣ 获取传输的 key 和 content
byte[] keyDecodeBase64 = Base64.decodeBase64(param.getKey().getBytes(StandardCharsets.UTF_8));
byte[] aesKey = RsaUtils.decryptByPrivateKey(keyDecodeBase64, prikey);
byte[] contentDecodeBase64 = Base64.decodeBase64(param.getContent().getBytes(StandardCharsets.UTF_8));
byte[] contentBytes = AesUtils.decrypt(contentDecodeBase64, aesKey);
String data = new String(contentBytes, Charsets.UTF_8);
// 2️⃣ 计算 MD5 签名
String expectedSign = DigestUtils.md5Hex(
param.getContent() + param.getKey() + param.getTimestamp() + md5key + param.getOrgCode()
);
// 3️⃣ 校验签名
if (!expectedSign.equals(param.getSign())) {
throw new RuntimeException(&quot;签名校验失败!&quot;);
}
return data;
} catch (Exception e) {
throw new RuntimeException(&quot;解密失败&quot;, e);
}
}
/**
* 加密
*
* @param data 原始数据
* @param md5key 签名密钥
* @param pubkey RSA 公钥
* @param orgCode 机构编码
* @return 加密后的 JSON 字符串
*/
public static String encryption(String data, String md5key, String pubkey, String orgCode) {
try {
// 1️⃣ 生成 AES 随机秘钥
byte[] aesKey = AesUtils.randomKey();
// 2️⃣ AES 加密数据
byte[] contentBytes = AesUtils.encrypt(data.getBytes(StandardCharsets.UTF_8), aesKey);
String content = Base64.encodeBase64String(contentBytes);
// 3️⃣ RSA 加密 AES 密钥
byte[] keyBytes = RsaUtils.encryptByPublicKey(aesKey, pubkey);
String key = Base64.encodeBase64String(keyBytes);
// 4️⃣ 计算签名
Long timestamp = Calendar.getInstance().getTimeInMillis();
String sign = DigestUtils.md5Hex(content + key + timestamp + md5key + orgCode);
// 5️⃣ 组装 JSON 响应
JSONObject res = new JSONObject();
res.put(&quot;key&quot;, key);
res.put(&quot;content&quot;, content);
res.put(&quot;timestamp&quot;, Long.toString(timestamp));
res.put(&quot;sign&quot;, sign);
res.put(&quot;orgCode&quot;, orgCode);
return res.toJSONString();
} catch (Exception e) {
throw new RuntimeException(&quot;加密失败&quot;, e);
}
}
}</code></pre>
</li>
<li>
<p>AesUtils</p>
<pre><code>public class AesUtils {
public static byte[] randomKey() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance(&quot;AES&quot;);
SecureRandom secureRandom = new SecureRandom();
keyGenerator.init(128, secureRandom);
return keyGenerator.generateKey().getEncoded();
}
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(key, &quot;AES&quot;);
Cipher cipher = Cipher.getInstance(&quot;AES/ECB/PKCS5Padding&quot;);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] encryptedData, byte[] key) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(key, &quot;AES&quot;);
Cipher cipher = Cipher.getInstance(&quot;AES/ECB/PKCS5Padding&quot;);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
return cipher.doFinal(encryptedData);
}
}</code></pre>
</li>
<li>RsaUtils
<pre><code>public class RsaUtils {
public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
byte[] keyBytes = Base64.decodeBase64(publicKey);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(&quot;RSA&quot;);
PublicKey publicK = keyFactory.generatePublic(keySpec);
Cipher cipher = Cipher.getInstance(&quot;RSA&quot;);
cipher.init(Cipher.ENCRYPT_MODE, publicK);
return cipher.doFinal(data);
}
public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) throws Exception {
byte[] keyBytes = Base64.decodeBase64(privateKey);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(&quot;RSA&quot;);
PrivateKey privateK = keyFactory.generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance(&quot;RSA&quot;);
cipher.init(Cipher.DECRYPT_MODE, privateK);
return cipher.doFinal(encryptedData);
}
}</code></pre></li>
</ul>