Idea Maven 构建
- 1. maven下载
- 2. Idea 配置
- 3. 配置Maven镜像
- 4. 在Maven项目pom.xml中添加依赖
- 5. 创建jar包,更新pom,执行代码
1. maven下载
【官网】https://maven.apache.org/download.cgi
【其他版本】https://dlcdn.apache.org/maven/maven-3/
2. Idea 配置
- 在Idea界面上关闭所有项目,全局配置:
Ctrl + Alt + S
打开设置,搜索maven,配置如下
(1) maven home path: D:/Program Files/Java/apache-maven-3.9.1
(2)User seeting file:D:\Program Files\Java\apache-maven-3.9.1\conf\settings.xml
(3) Local repository :D:\Program Files\Java\repository
2. 配置运行程序Runner
-DarchetypeCatalog=internal
3. 配置Maven镜像
【阿里云效】https://developer.aliyun.com/mvn/guide
<mirror><id>aliyunmaven</id><mirrorOf>*</mirrorOf><name>阿里云公共仓库</name><url>https://maven.aliyun.com/repository/public</url>
</mirror>
4. 在Maven项目pom.xml中添加依赖
<dependencies><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.4</version></dependency></dependencies>
5. 创建jar包,更新pom,执行代码
右击Java -> New -> Package
输入package名称 -> 新建Java文件
点击idea右侧的 Maven图标-> 点击左上角刷新,更近pom中的依赖包
运行下面代码
package org.apache.jmeter.functions;/*** @author heartingme* @date 2023/1/10 4:25 下午* movingbrick*/import org.apache.commons.lang3.StringUtils;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;/*** @author heartingme* @date 2023/1/10 9:20 下午* movingbrick*/
public class AESUtil {/*** 编码*/private static final String ENCODING = "UTF-8";/*** 算法定义*/private static final String AES_ALGORITHM = "AES";/*** 指定填充方式*/private static final String CIPHER_PADDING = "AES/ECB/PKCS5Padding";/*** AES加密** @param content 待加密内容* @param aesKey 密码* @return*/public static String encrypt(String content, String aesKey) {if (StringUtils.isBlank(content)) {return null;}//判断秘钥是否为16位if (StringUtils.isNotBlank(aesKey) && aesKey.length() == 16) {try {//对密码进行编码byte[] bytes = aesKey.getBytes(ENCODING);//设置加密算法,生成秘钥SecretKeySpec skeySpec = new SecretKeySpec(bytes, AES_ALGORITHM);// "算法/模式/补码方式"Cipher cipher = Cipher.getInstance(CIPHER_PADDING);//选择加密cipher.init(Cipher.ENCRYPT_MODE, skeySpec);//根据待加密内容生成字节数组byte[] encrypted = cipher.doFinal(content.getBytes(ENCODING));//返回base64字符串Base64.Encoder encoder = Base64.getEncoder();return encoder.encodeToString(encrypted);} catch (Exception e) {throw new RuntimeException(e);}} else {return "secret key length should be i6";}}/*** 解密** @param content 待解密内容* @param aesKey 密码* @return*/public static String decrypt(String content, String aesKey) {if (StringUtils.isBlank(content)) {return null;}//判断秘钥是否为16位if (StringUtils.isNotBlank(aesKey) && aesKey.length() == 16) {try {//对密码进行编码byte[] bytes = aesKey.getBytes(ENCODING);//设置解密算法,生成秘钥SecretKeySpec skeySpec = new SecretKeySpec(bytes, AES_ALGORITHM);// "算法/模式/补码方式"Cipher cipher = Cipher.getInstance(CIPHER_PADDING);//选择解密cipher.init(Cipher.DECRYPT_MODE, skeySpec);//先进行Base64解码Base64.Decoder decoder = Base64.getDecoder();byte[] decodeBase64 = decoder.decode(content);//根据待解密内容进行解密byte[] decrypted = cipher.doFinal(decodeBase64);//将字节数组转成字符串return new String(decrypted, ENCODING);} catch (Exception e) {throw new RuntimeException(e);}} else {return "secret key length should be i6";}}public static void main(String[] args) {// AES支持三种长度的密钥:128位、192位、256位。// 代码中这种就是128位的加密密钥,16字节 * 8位/字节 = 128位。String random = "12345678";System.out.println("密钥key:" + random);String aes = "{\"token\":\"f08d023a-4739-4dbc-898f-161968681281\"}";System.out.println("---------解密---------");String decrypt = decrypt(aesResult, random);System.out.println("aes解密结果:" + decrypt);}
}
项目链接