From a09af6b6a13fe806781163774cbe2b1a82f979de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BA=9C=E5=BC=BA?= <8569561+fuqiangma@user.noreply.gitee.com> Date: Sat, 17 Jun 2023 09:48:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B8=B8=E8=A7=81=E7=9A=84=E4=BA=94=E7=A7=8D?= =?UTF-8?q?=E5=8A=A0=E5=AF=86=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- suanfa/.gitignore | 27 ++++ suanfa/pom.xml | 118 ++++++++++++++ .../main/java/com/woniu/WoNiuApplication.java | 18 +++ .../src/main/java/com/woniu/suanfa/AES.java | 73 +++++++++ .../src/main/java/com/woniu/suanfa/DES.java | 78 +++++++++ .../src/main/java/com/woniu/suanfa/MD5SF.java | 45 ++++++ .../src/main/java/com/woniu/suanfa/RSA.java | 66 ++++++++ .../main/java/com/woniu/suanfa/SHA256SF.java | 29 ++++ .../src/main/resources/application.properties | 12 ++ .../src/main/resources/mapper/StudentDao.xml | 25 +++ suanfa/src/main/resources/mybatis-config.xml | 35 ++++ suanfa/woniu-web.iml | 151 ++++++++++++++++++ 12 files changed, 677 insertions(+) create mode 100644 suanfa/.gitignore create mode 100644 suanfa/pom.xml create mode 100644 suanfa/src/main/java/com/woniu/WoNiuApplication.java create mode 100644 suanfa/src/main/java/com/woniu/suanfa/AES.java create mode 100644 suanfa/src/main/java/com/woniu/suanfa/DES.java create mode 100644 suanfa/src/main/java/com/woniu/suanfa/MD5SF.java create mode 100644 suanfa/src/main/java/com/woniu/suanfa/RSA.java create mode 100644 suanfa/src/main/java/com/woniu/suanfa/SHA256SF.java create mode 100644 suanfa/src/main/resources/application.properties create mode 100644 suanfa/src/main/resources/mapper/StudentDao.xml create mode 100644 suanfa/src/main/resources/mybatis-config.xml create mode 100644 suanfa/woniu-web.iml diff --git a/suanfa/.gitignore b/suanfa/.gitignore new file mode 100644 index 0000000..682d4ed --- /dev/null +++ b/suanfa/.gitignore @@ -0,0 +1,27 @@ +# Created by .ignore support plugin (hsz.mobi) +### Java template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +/.idea/ +/target/ diff --git a/suanfa/pom.xml b/suanfa/pom.xml new file mode 100644 index 0000000..5515d9a --- /dev/null +++ b/suanfa/pom.xml @@ -0,0 +1,118 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.9.RELEASE + + + + com.ayi + woniu-web + 0.0.1-SNAPSHOT + woniu-web + create by woniu + + jar + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-aop + + + cn.hutool + hutool-all + 5.3.3 + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.projectlombok + lombok + + + org.springframework.boot + spring-boot-starter-cache + + + + com.baomidou + mybatis-plus-boot-starter + 3.5.2 + + + + + mysql + mysql-connector-java + 8.0.29 + + + + com.alipay.sofa + runtime-sofa-boot-starter + 3.18.0 + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.1.4 + + + org.springframework.boot + spring-boot-starter-validation + + + + + com.github.pagehelper + pagehelper + 4.2.0 + + + org.mybatis + mybatis + 3.5.6 + + + com.alibaba + fastjson + 1.2.76 + + + com.alibaba + easyexcel + 3.0.5 + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/suanfa/src/main/java/com/woniu/WoNiuApplication.java b/suanfa/src/main/java/com/woniu/WoNiuApplication.java new file mode 100644 index 0000000..d699542 --- /dev/null +++ b/suanfa/src/main/java/com/woniu/WoNiuApplication.java @@ -0,0 +1,18 @@ +package com.woniu; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@SpringBootApplication +@EnableTransactionManagement +public class WoNiuApplication { + + public static void main(String[] args) { + SpringApplication.run(WoNiuApplication.class, args); + } + + + +} diff --git a/suanfa/src/main/java/com/woniu/suanfa/AES.java b/suanfa/src/main/java/com/woniu/suanfa/AES.java new file mode 100644 index 0000000..ca992cf --- /dev/null +++ b/suanfa/src/main/java/com/woniu/suanfa/AES.java @@ -0,0 +1,73 @@ +package com.woniu.suanfa; + +import javax.crypto.Cipher; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +public class AES { + + private static final String AES_ALGORITHM = "AES"; + // AES加密模式为CBC,填充方式为PKCS5Padding + private static final String AES_TRANSFORMATION = "AES/CBC/PKCS5Padding"; + // AES密钥为16位 + private static final String AES_KEY = "1234567890123456"; + // AES初始化向量为16位 + private static final String AES_IV = "abcdefghijklmnop"; + + /** + * AES加密 + * + * @param data 待加密的数据 + * @return 加密后的数据,使用Base64编码 + */ + public static String encrypt(String data) throws Exception { + // 将AES密钥转换为SecretKeySpec对象 + SecretKeySpec secretKeySpec = new SecretKeySpec(AES_KEY.getBytes(), AES_ALGORITHM); + // 将AES初始化向量转换为IvParameterSpec对象 + IvParameterSpec ivParameterSpec = new IvParameterSpec(AES_IV.getBytes()); + // 根据加密算法获取加密器 + Cipher cipher = Cipher.getInstance(AES_TRANSFORMATION); + // 初始化加密器,设置加密模式、密钥和初始化向量 + cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); + // 加密数据 + byte[] encryptedData = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); + // 对加密后的数据使用Base64编码 + return Base64.getEncoder().encodeToString(encryptedData); + } + + /** + * AES解密 + * + * @param encryptedData 加密后的数据,使用Base64编码 + * @return 解密后的数据 + */ + public static String decrypt(String encryptedData) throws Exception { + // 将AES密钥转换为SecretKeySpec对象 + SecretKeySpec secretKeySpec = new SecretKeySpec(AES_KEY.getBytes(), AES_ALGORITHM); + // 将AES初始化向量转换为IvParameterSpec对象 + IvParameterSpec ivParameterSpec = new IvParameterSpec(AES_IV.getBytes()); + // 根据加密算法获取解密器 + Cipher cipher = Cipher.getInstance(AES_TRANSFORMATION); + // 初始化解密器,设置解密模式、密钥和初始化向量 + cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec); + // 对加密后的数据使用Base64解码 + byte[] decodedData = Base64.getDecoder().decode(encryptedData); + // 解密数据 + byte[] decryptedData = cipher.doFinal(decodedData); + // 返回解密后的数据 + return new String(decryptedData, StandardCharsets.UTF_8); + } + + public static void main(String[] args) throws Exception { + String data = "Hello World"; + + String encryptedData = encrypt(data); + System.out.println("加密后的数据:" + encryptedData); + + String decryptedData = decrypt(encryptedData); + System.out.println("解密后的数据:" + decryptedData); + } + +} diff --git a/suanfa/src/main/java/com/woniu/suanfa/DES.java b/suanfa/src/main/java/com/woniu/suanfa/DES.java new file mode 100644 index 0000000..9ac8483 --- /dev/null +++ b/suanfa/src/main/java/com/woniu/suanfa/DES.java @@ -0,0 +1,78 @@ +package com.woniu.suanfa; + + +import javax.crypto.Cipher; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.DESKeySpec; +import java.security.spec.KeySpec; +import java.util.Base64; + +public class DES { + + private static final String DES_ALGORITHM = "DES"; + + /** + * DES加密 + * + * @param data 待加密的数据 + * @param key 密钥,长度必须为8位 + * @return 加密后的数据,使用Base64编码 + */ + public static String encrypt(String data, String key) throws Exception { + // 根据密钥生成密钥规范 + KeySpec keySpec = new DESKeySpec(key.getBytes()); + // 根据密钥规范生成密钥工厂 + SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM); + // 根据密钥工厂和密钥规范生成密钥 + SecretKey secretKey = secretKeyFactory.generateSecret(keySpec); + + // 根据加密算法获取加密器 + Cipher cipher = Cipher.getInstance(DES_ALGORITHM); + // 初始化加密器,设置加密模式和密钥 + cipher.init(Cipher.ENCRYPT_MODE, secretKey); + // 加密数据 + byte[] encryptedData = cipher.doFinal(data.getBytes()); + // 对加密后的数据进行Base64编码 + return Base64.getEncoder().encodeToString(encryptedData); + } + + /** + * DES解密 + * + * @param encryptedData 加密后的数据,使用Base64编码 + * @param key 密钥,长度必须为8位 + * @return 解密后的数据 + */ + public static String decrypt(String encryptedData, String key) throws Exception { + // 根据密钥生成密钥规范 + KeySpec keySpec = new DESKeySpec(key.getBytes()); + // 根据密钥规范生成密钥工厂 + SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM); + // 根据密钥工厂和密钥规范生成密钥 + SecretKey secretKey = secretKeyFactory.generateSecret(keySpec); + + // 对加密后的数据进行Base64解码 + byte[] decodedData = Base64.getDecoder().decode(encryptedData); + // 根据加密算法获取解密器 + Cipher cipher = Cipher.getInstance(DES_ALGORITHM); + // 初始化解密器,设置解密模式和密钥 + cipher.init(Cipher.DECRYPT_MODE, secretKey); + // 解密数据 + byte[] decryptedData = cipher.doFinal(decodedData); + // 将解密后的数据转换为字符串 + return new String(decryptedData); + } + + public static void main(String[] args) throws Exception { + String data = "Hello World"; + String key = "12345678"; + + String encryptedData = encrypt(data, key); + System.out.println("加密后的数据:" + encryptedData); + + String decryptedData = decrypt(encryptedData, key); + System.out.println("解密后的数据:" + decryptedData); + } + +} diff --git a/suanfa/src/main/java/com/woniu/suanfa/MD5SF.java b/suanfa/src/main/java/com/woniu/suanfa/MD5SF.java new file mode 100644 index 0000000..a34254a --- /dev/null +++ b/suanfa/src/main/java/com/woniu/suanfa/MD5SF.java @@ -0,0 +1,45 @@ +package com.woniu.suanfa; + +import java.security.MessageDigest; +import java.util.Formatter; + +/** + * + * 大家平时的工作中,可能也在很多地方用到了加密、解密,比如: + * + * 用户的密码密文存储 + * 银行卡号之类敏感数据,需要加密传输 + * 重要接口,比如支付,客户端要对请求生成一个签名,服务端要对签名进行验证 + * + * + * 不可逆加密 MD5 SHA-256 + * 可逆加密 + * 对称加密 DES AES + * 非对称加密 RSA + * + * + */ +public class MD5SF { + private static final String MD5_ALGORITHM = "MD5"; + + public static String encrypt(String data) throws Exception { + // 获取MD5算法实例 + MessageDigest messageDigest = MessageDigest.getInstance(MD5_ALGORITHM); + // 计算散列值 + byte[] digest = messageDigest.digest(data.getBytes()); + Formatter formatter = new Formatter(); + // 补齐前导0,并格式化 + for (byte b : digest) { + formatter.format("%02x", b); + } + return formatter.toString(); + } + + public static void main(String[] args) throws Exception { + String data = "Hello World"; + String encryptedData = encrypt(data); + System.out.println("加密后的数据:" + encryptedData); + } + + +} diff --git a/suanfa/src/main/java/com/woniu/suanfa/RSA.java b/suanfa/src/main/java/com/woniu/suanfa/RSA.java new file mode 100644 index 0000000..6b12b6f --- /dev/null +++ b/suanfa/src/main/java/com/woniu/suanfa/RSA.java @@ -0,0 +1,66 @@ +package com.woniu.suanfa; + +import javax.crypto.Cipher; +import java.nio.charset.StandardCharsets; +import java.security.*; +import java.util.Base64; + +public class RSA { + + private static final String RSA_ALGORITHM = "RSA"; + + /** + * 生成RSA密钥对 + * + * @return RSA密钥对 + */ + public static KeyPair generateKeyPair() throws NoSuchAlgorithmException { + KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA_ALGORITHM); + keyPairGenerator.initialize(2048); // 密钥大小为2048位 + return keyPairGenerator.generateKeyPair(); + } + + /** + * 使用公钥加密数据 + * + * @param data 待加密的数据 + * @param publicKey 公钥 + * @return 加密后的数据 + */ + public static String encrypt(String data, PublicKey publicKey) throws Exception { + Cipher cipher = Cipher.getInstance(RSA_ALGORITHM); + cipher.init(Cipher.ENCRYPT_MODE, publicKey); + byte[] encryptedData = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); + return Base64.getEncoder().encodeToString(encryptedData); + } + + /** + * 使用私钥解密数据 + * + * @param encryptedData 加密后的数据 + * @param privateKey 私钥 + * @return 解密后的数据 + */ + public static String decrypt(String encryptedData, PrivateKey privateKey) throws Exception { + byte[] decodedData = Base64.getDecoder().decode(encryptedData); + Cipher cipher = Cipher.getInstance(RSA_ALGORITHM); + cipher.init(Cipher.DECRYPT_MODE, privateKey); + byte[] decryptedData = cipher.doFinal(decodedData); + return new String(decryptedData, StandardCharsets.UTF_8); + } + + public static void main(String[] args) throws Exception { + KeyPair keyPair = generateKeyPair(); + PublicKey publicKey = keyPair.getPublic(); + PrivateKey privateKey = keyPair.getPrivate(); + + String data = "Hello World"; + + String encryptedData = encrypt(data, publicKey); + System.out.println("加密后的数据:" + encryptedData); + + String decryptedData = decrypt(encryptedData, privateKey); + System.out.println("解密后的数据:" + decryptedData); + } + +} diff --git a/suanfa/src/main/java/com/woniu/suanfa/SHA256SF.java b/suanfa/src/main/java/com/woniu/suanfa/SHA256SF.java new file mode 100644 index 0000000..38ecbb3 --- /dev/null +++ b/suanfa/src/main/java/com/woniu/suanfa/SHA256SF.java @@ -0,0 +1,29 @@ +package com.woniu.suanfa; + +import java.security.MessageDigest; + +public class SHA256SF { + + private static final String SHA_256_ALGORITHM = "SHA-256"; + + public static String encrypt(String data) throws Exception { + //获取SHA-256算法实例 + MessageDigest messageDigest = MessageDigest.getInstance(SHA_256_ALGORITHM); + //计算散列值 + byte[] digest = messageDigest.digest(data.getBytes()); + StringBuilder stringBuilder = new StringBuilder(); + //将byte数组转换为15进制字符串 + for (byte b : digest) { + stringBuilder.append(Integer.toHexString((b & 0xFF) | 0x100), 1, 3); + } + return stringBuilder.toString(); + } + + public static void main(String[] args) throws Exception { + String data = "Hello World"; + String encryptedData = encrypt(data); + System.out.println("加密后的数据:" + encryptedData); + } + + +} diff --git a/suanfa/src/main/resources/application.properties b/suanfa/src/main/resources/application.properties new file mode 100644 index 0000000..0a75ef4 --- /dev/null +++ b/suanfa/src/main/resources/application.properties @@ -0,0 +1,12 @@ +spring.datasource.username=root +spring.datasource.password=123456 +spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver + +mybatis.mapper-locations=classpath:/mapper/*.xml + +spring.cache.type=simple +spring.application.name=yyyy +#spring.jackson.date-format=yyyy-MM-dd HH:mm:ss +#spring.jackson.time-zone=GMT+8 + diff --git a/suanfa/src/main/resources/mapper/StudentDao.xml b/suanfa/src/main/resources/mapper/StudentDao.xml new file mode 100644 index 0000000..03028fd --- /dev/null +++ b/suanfa/src/main/resources/mapper/StudentDao.xml @@ -0,0 +1,25 @@ + + + + + + + + insert into student(name, phone_number) + values (#{student.name}, #{student.phoneNumber}) + + + + insert into sys_person_phone_encrypt(person_id,phone_key) + values (#{persondId},#{phoneKeywords}) + + + + + + \ No newline at end of file diff --git a/suanfa/src/main/resources/mybatis-config.xml b/suanfa/src/main/resources/mybatis-config.xml new file mode 100644 index 0000000..b099e5d --- /dev/null +++ b/suanfa/src/main/resources/mybatis-config.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/suanfa/woniu-web.iml b/suanfa/woniu-web.iml new file mode 100644 index 0000000..3afeba8 --- /dev/null +++ b/suanfa/woniu-web.iml @@ -0,0 +1,151 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file