From 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a Mon Sep 17 00:00:00 2001
From: xiejun <xiejun@vci-tech.com>
Date: 星期五, 01 十一月 2024 15:11:19 +0800
Subject: [PATCH] Revert "集成获取mdm分发通用数据格式接口集成"

---
 Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/AesUtil.java |  309 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 309 insertions(+), 0 deletions(-)

diff --git a/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/AesUtil.java b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/AesUtil.java
new file mode 100644
index 0000000..69152ff
--- /dev/null
+++ b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/AesUtil.java
@@ -0,0 +1,309 @@
+/*
+ *      Copyright (c) 2018-2028, DreamLu All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are met:
+ *
+ *  Redistributions of source code must retain the above copyright notice,
+ *  this list of conditions and the following disclaimer.
+ *  Redistributions in binary form must reproduce the above copyright
+ *  notice, this list of conditions and the following disclaimer in the
+ *  documentation and/or other materials provided with the distribution.
+ *  Neither the name of the dreamlu.net developer nor the names of its
+ *  contributors may be used to endorse or promote products derived from
+ *  this software without specific prior written permission.
+ *  Author: DreamLu 鍗㈡槬姊� (596392912@qq.com)
+ */
+package org.springblade.core.tool.utils;
+
+import org.springframework.lang.Nullable;
+import org.springframework.util.Assert;
+
+import javax.crypto.Cipher;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+import java.nio.charset.Charset;
+import java.util.Arrays;
+import java.util.Objects;
+
+/**
+ * 瀹屽叏鍏煎寰俊鎵�浣跨敤鐨凙ES鍔犲瘑宸ュ叿绫�
+ * aes鐨刱ey蹇呴』鏄�256byte闀匡紙姣斿32涓瓧绗︼級锛屽彲浠ヤ娇鐢ˋesKit.genAesKey()鏉ョ敓鎴愪竴缁刱ey
+ *
+ * @author L.cm
+ */
+public class AesUtil {
+
+	public static final Charset DEFAULT_CHARSET = Charsets.UTF_8;
+
+	/**
+	 * 鑾峰彇瀵嗛挜
+	 *
+	 * @return {String}
+	 */
+	public static String genAesKey() {
+		return StringUtil.random(32);
+	}
+
+	/**
+	 * 鍔犲瘑
+	 *
+	 * @param content    鏂囨湰鍐呭
+	 * @param aesTextKey 鏂囨湰瀵嗛挜
+	 * @return byte[]
+	 */
+	public static byte[] encrypt(String content, String aesTextKey) {
+		return encrypt(content.getBytes(DEFAULT_CHARSET), aesTextKey);
+	}
+
+	/**
+	 * 鍔犲瘑
+	 *
+	 * @param content    鏂囨湰鍐呭
+	 * @param charset    缂栫爜
+	 * @param aesTextKey 鏂囨湰瀵嗛挜
+	 * @return byte[]
+	 */
+	public static byte[] encrypt(String content, Charset charset, String aesTextKey) {
+		return encrypt(content.getBytes(charset), aesTextKey);
+	}
+
+	/**
+	 * 鍔犲瘑
+	 *
+	 * @param content    鍐呭
+	 * @param aesTextKey 鏂囨湰瀵嗛挜
+	 * @return byte[]
+	 */
+	public static byte[] encrypt(byte[] content, String aesTextKey) {
+		return encrypt(content, Objects.requireNonNull(aesTextKey).getBytes(DEFAULT_CHARSET));
+	}
+
+	/**
+	 * hex鍔犲瘑
+	 *
+	 * @param content    鏂囨湰鍐呭
+	 * @param aesTextKey 鏂囨湰瀵嗛挜
+	 * @return {String}
+	 */
+	public static String encryptToHex(String content, String aesTextKey) {
+		return HexUtil.encodeToString(encrypt(content, aesTextKey));
+	}
+
+	/**
+	 * hex鍔犲瘑
+	 *
+	 * @param content    鍐呭
+	 * @param aesTextKey 鏂囨湰瀵嗛挜
+	 * @return {String}
+	 */
+	public static String encryptToHex(byte[] content, String aesTextKey) {
+		return HexUtil.encodeToString(encrypt(content, aesTextKey));
+	}
+
+	/**
+	 * Base64鍔犲瘑
+	 *
+	 * @param content    鏂囨湰鍐呭
+	 * @param aesTextKey 鏂囨湰瀵嗛挜
+	 * @return {String}
+	 */
+	public static String encryptToBase64(String content, String aesTextKey) {
+		return Base64Util.encodeToString(encrypt(content, aesTextKey));
+	}
+
+	/**
+	 * Base64鍔犲瘑
+	 *
+	 * @param content    鍐呭
+	 * @param aesTextKey 鏂囨湰瀵嗛挜
+	 * @return {String}
+	 */
+	public static String encryptToBase64(byte[] content, String aesTextKey) {
+		return Base64Util.encodeToString(encrypt(content, aesTextKey));
+	}
+
+	/**
+	 * hex瑙e瘑
+	 *
+	 * @param content    鏂囨湰鍐呭
+	 * @param aesTextKey 鏂囨湰瀵嗛挜
+	 * @return {String}
+	 */
+	@Nullable
+	public static String decryptFormHexToString(@Nullable String content, String aesTextKey) {
+		byte[] hexBytes = decryptFormHex(content, aesTextKey);
+		if (hexBytes == null) {
+			return null;
+		}
+		return new String(hexBytes, DEFAULT_CHARSET);
+	}
+
+	/**
+	 * hex瑙e瘑
+	 *
+	 * @param content    鏂囨湰鍐呭
+	 * @param aesTextKey 鏂囨湰瀵嗛挜
+	 * @return byte[]
+	 */
+	@Nullable
+	public static byte[] decryptFormHex(@Nullable String content, String aesTextKey) {
+		if (StringUtil.isBlank(content)) {
+			return null;
+		}
+		return decryptFormHex(content.getBytes(DEFAULT_CHARSET), aesTextKey);
+	}
+
+	/**
+	 * hex瑙e瘑
+	 *
+	 * @param content    鍐呭
+	 * @param aesTextKey 鏂囨湰瀵嗛挜
+	 * @return byte[]
+	 */
+	public static byte[] decryptFormHex(byte[] content, String aesTextKey) {
+		return decrypt(HexUtil.decode(content), aesTextKey);
+	}
+
+	/**
+	 * Base64瑙e瘑
+	 *
+	 * @param content    鏂囨湰鍐呭
+	 * @param aesTextKey 鏂囨湰瀵嗛挜
+	 * @return {String}
+	 */
+	@Nullable
+	public static String decryptFormBase64ToString(@Nullable String content, String aesTextKey) {
+		byte[] hexBytes = decryptFormBase64(content, aesTextKey);
+		if (hexBytes == null) {
+			return null;
+		}
+		return new String(hexBytes, DEFAULT_CHARSET);
+	}
+
+	/**
+	 * Base64瑙e瘑
+	 *
+	 * @param content    鏂囨湰鍐呭
+	 * @param aesTextKey 鏂囨湰瀵嗛挜
+	 * @return byte[]
+	 */
+	@Nullable
+	public static byte[] decryptFormBase64(@Nullable String content, String aesTextKey) {
+		if (StringUtil.isBlank(content)) {
+			return null;
+		}
+		return decryptFormBase64(content.getBytes(DEFAULT_CHARSET), aesTextKey);
+	}
+
+	/**
+	 * Base64瑙e瘑
+	 *
+	 * @param content    鍐呭
+	 * @param aesTextKey 鏂囨湰瀵嗛挜
+	 * @return byte[]
+	 */
+	public static byte[] decryptFormBase64(byte[] content, String aesTextKey) {
+		return decrypt(Base64Util.decode(content), aesTextKey);
+	}
+
+	/**
+	 * 瑙e瘑
+	 *
+	 * @param content    鍐呭
+	 * @param aesTextKey 鏂囨湰瀵嗛挜
+	 * @return {String}
+	 */
+	public static String decryptToString(byte[] content, String aesTextKey) {
+		return new String(decrypt(content, aesTextKey), DEFAULT_CHARSET);
+	}
+
+	/**
+	 * 瑙e瘑
+	 *
+	 * @param content    鍐呭
+	 * @param aesTextKey 鏂囨湰瀵嗛挜
+	 * @return byte[]
+	 */
+	public static byte[] decrypt(byte[] content, String aesTextKey) {
+		return decrypt(content, Objects.requireNonNull(aesTextKey).getBytes(DEFAULT_CHARSET));
+	}
+
+	/**
+	 * 瑙e瘑
+	 *
+	 * @param content 鍐呭
+	 * @param aesKey  瀵嗛挜
+	 * @return byte[]
+	 */
+	public static byte[] encrypt(byte[] content, byte[] aesKey) {
+		return aes(Pkcs7Encoder.encode(content), aesKey, Cipher.ENCRYPT_MODE);
+	}
+
+	/**
+	 * 鍔犲瘑
+	 *
+	 * @param encrypted 鍐呭
+	 * @param aesKey    瀵嗛挜
+	 * @return byte[]
+	 */
+	public static byte[] decrypt(byte[] encrypted, byte[] aesKey) {
+		return Pkcs7Encoder.decode(aes(encrypted, aesKey, Cipher.DECRYPT_MODE));
+	}
+
+	/**
+	 * ase鍔犲瘑
+	 *
+	 * @param encrypted 鍐呭
+	 * @param aesKey    瀵嗛挜
+	 * @param mode      妯″紡
+	 * @return byte[]
+	 */
+	private static byte[] aes(byte[] encrypted, byte[] aesKey, int mode) {
+		Assert.isTrue(aesKey.length == 32, "IllegalAesKey, aesKey's length must be 32");
+		try {
+			Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
+			SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES");
+			IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey, 0, 16));
+			cipher.init(mode, keySpec, iv);
+			return cipher.doFinal(encrypted);
+		} catch (Exception e) {
+			throw Exceptions.unchecked(e);
+		}
+	}
+
+	/**
+	 * 鎻愪緵鍩轰簬PKCS7绠楁硶鐨勫姞瑙e瘑鎺ュ彛.
+	 */
+	private static class Pkcs7Encoder {
+		private static final int BLOCK_SIZE = 32;
+
+		private static byte[] encode(byte[] src) {
+			int count = src.length;
+			// 璁$畻闇�瑕佸~鍏呯殑浣嶆暟
+			int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE);
+			// 鑾峰緱琛ヤ綅鎵�鐢ㄧ殑瀛楃
+			byte pad = (byte) (amountToPad & 0xFF);
+			byte[] pads = new byte[amountToPad];
+			for (int index = 0; index < amountToPad; index++) {
+				pads[index] = pad;
+			}
+			int length = count + amountToPad;
+			byte[] dest = new byte[length];
+			System.arraycopy(src, 0, dest, 0, count);
+			System.arraycopy(pads, 0, dest, count, amountToPad);
+			return dest;
+		}
+
+		private static byte[] decode(byte[] decrypted) {
+			int pad = decrypted[decrypted.length - 1];
+			if (pad < 1 || pad > BLOCK_SIZE) {
+				pad = 0;
+			}
+			if (pad > 0) {
+				return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad);
+			}
+			return decrypted;
+		}
+	}
+}

--
Gitblit v1.9.3