|
@@ -0,0 +1,117 @@
|
|
|
+/*
|
|
|
+ * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
|
|
|
+ * <p>
|
|
|
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
+ * use this file except in compliance with the License. You may obtain a copy of
|
|
|
+ * the License at
|
|
|
+ * <p>
|
|
|
+ * https://www.apache.org/licenses/LICENSE-2.0
|
|
|
+ * <p>
|
|
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
|
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
+ * License for the specific language governing permissions and limitations under
|
|
|
+ * the License.
|
|
|
+ */
|
|
|
+package com.baomidou.mybatisplus.core.toolkit;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
|
|
|
+
|
|
|
+import javax.crypto.Cipher;
|
|
|
+import javax.crypto.spec.IvParameterSpec;
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.util.Base64;
|
|
|
+
|
|
|
+/**
|
|
|
+ * AES CBC模式加密工具类
|
|
|
+ *
|
|
|
+ * @author hubin
|
|
|
+ * @since 2020-05-23
|
|
|
+ */
|
|
|
+public class AES {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加密
|
|
|
+ *
|
|
|
+ * @param data 需要加密的内容
|
|
|
+ * @param key 加密密码
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static byte[] encrypt(byte[] data, byte[] key) {
|
|
|
+ try {
|
|
|
+ SecretKeySpec secretKey = new SecretKeySpec(key, Constants.AES);
|
|
|
+ byte[] enCodeFormat = secretKey.getEncoded();
|
|
|
+ SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, Constants.AES);
|
|
|
+ Cipher cipher = Cipher.getInstance(Constants.AES_CBC_CIPHER);
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(key));
|
|
|
+ return cipher.doFinal(data);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new MybatisPlusException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解密
|
|
|
+ *
|
|
|
+ * @param data 待解密内容
|
|
|
+ * @param key 解密密钥
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static byte[] decrypt(byte[] data, byte[] key) {
|
|
|
+ try {
|
|
|
+ SecretKeySpec secretKey = new SecretKeySpec(key, Constants.AES);
|
|
|
+ byte[] enCodeFormat = secretKey.getEncoded();
|
|
|
+ SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, Constants.AES);
|
|
|
+ Cipher cipher = Cipher.getInstance(Constants.AES_CBC_CIPHER);
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(key));
|
|
|
+ return cipher.doFinal(data);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new MybatisPlusException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加密
|
|
|
+ *
|
|
|
+ * @param data 需要加密的内容
|
|
|
+ * @param key 加密密码
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String encrypt(String data, String key) {
|
|
|
+ try {
|
|
|
+ byte[] valueByte = encrypt(data.getBytes(Constants.UTF_8), key.getBytes(Constants.UTF_8));
|
|
|
+ return Base64.getEncoder().encodeToString(valueByte);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ throw new MybatisPlusException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解密
|
|
|
+ *
|
|
|
+ * @param data 待解密内容 base64 字符串
|
|
|
+ * @param key 解密密钥
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String decrypt(String data, String key) {
|
|
|
+ try {
|
|
|
+ byte[] originalData = Base64.getDecoder().decode(data.getBytes());
|
|
|
+ byte[] valueByte = decrypt(originalData, key.getBytes(Constants.UTF_8));
|
|
|
+ return new String(valueByte);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ throw new MybatisPlusException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成一个随机字符串密钥
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * @throws NoSuchAlgorithmException
|
|
|
+ */
|
|
|
+ public static String generateRandomKey() {
|
|
|
+ return IdWorker.get32UUID().substring(0, 16);
|
|
|
+ }
|
|
|
+}
|