更换加密方法
Showing
3 changed files
with
69 additions
and
2 deletions
| 1 | package com.lego; | ||
| 2 | |||
| 3 | import com.lego.common.utils.EncryptUtils; | ||
| 4 | |||
| 5 | import java.io.File; | ||
| 6 | import java.io.FileInputStream; | ||
| 7 | import java.io.FileOutputStream; | ||
| 8 | |||
| 9 | /** | ||
| 10 | * @author chentao | ||
| 11 | * @date 2025/8/7 | ||
| 12 | */ | ||
| 13 | public class ManageApplication { | ||
| 14 | public static void main(String[] args) { | ||
| 15 | String path1 = "f:/1.mp4"; | ||
| 16 | String path2 = "f:/2.mp4"; | ||
| 17 | try{ | ||
| 18 | FileInputStream inputStream = new FileInputStream(path1); | ||
| 19 | FileOutputStream outputStream = new FileOutputStream(path2); | ||
| 20 | byte[] arr = new byte[inputStream.available()]; | ||
| 21 | inputStream.read(arr); | ||
| 22 | byte[] decrypt = EncryptUtils.decryptByAes(arr); | ||
| 23 | outputStream.write(decrypt); | ||
| 24 | |||
| 25 | inputStream.close(); | ||
| 26 | outputStream.close(); | ||
| 27 | } | ||
| 28 | catch (Exception e){ | ||
| 29 | e.printStackTrace(); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | } |
| ... | @@ -12,6 +12,7 @@ import cn.hutool.crypto.asymmetric.SM2; | ... | @@ -12,6 +12,7 @@ import cn.hutool.crypto.asymmetric.SM2; |
| 12 | import java.nio.charset.StandardCharsets; | 12 | import java.nio.charset.StandardCharsets; |
| 13 | import java.util.HashMap; | 13 | import java.util.HashMap; |
| 14 | import java.util.Map; | 14 | import java.util.Map; |
| 15 | import java.util.Objects; | ||
| 15 | 16 | ||
| 16 | /** | 17 | /** |
| 17 | * 安全相关工具类 | 18 | * 安全相关工具类 |
| ... | @@ -72,6 +73,18 @@ public class EncryptUtils { | ... | @@ -72,6 +73,18 @@ public class EncryptUtils { |
| 72 | return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).encryptBase64(data, StandardCharsets.UTF_8); | 73 | return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).encryptBase64(data, StandardCharsets.UTF_8); |
| 73 | } | 74 | } |
| 74 | 75 | ||
| 76 | public static byte[] encryptByAes(byte[] data, String password) { | ||
| 77 | if (StrUtil.isBlank(password)) { | ||
| 78 | throw new IllegalArgumentException("AES需要传入秘钥信息"); | ||
| 79 | } | ||
| 80 | // aes算法的秘钥要求是16位、24位、32位 | ||
| 81 | int[] array = {16, 24, 32}; | ||
| 82 | if (!ArrayUtil.contains(array, password.length())) { | ||
| 83 | throw new IllegalArgumentException("AES秘钥长度要求为16位、24位、32位"); | ||
| 84 | } | ||
| 85 | return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).encrypt(data); | ||
| 86 | } | ||
| 87 | |||
| 75 | /** | 88 | /** |
| 76 | * AES加密 | 89 | * AES加密 |
| 77 | * | 90 | * |
| ... | @@ -82,6 +95,10 @@ public class EncryptUtils { | ... | @@ -82,6 +95,10 @@ public class EncryptUtils { |
| 82 | return encryptByAes(data, AES_SECRET); | 95 | return encryptByAes(data, AES_SECRET); |
| 83 | } | 96 | } |
| 84 | 97 | ||
| 98 | public static byte[] encryptByAes(byte[] data) { | ||
| 99 | return encryptByAes(data, AES_SECRET); | ||
| 100 | } | ||
| 101 | |||
| 85 | /** | 102 | /** |
| 86 | * AES加密 | 103 | * AES加密 |
| 87 | * | 104 | * |
| ... | @@ -118,6 +135,18 @@ public class EncryptUtils { | ... | @@ -118,6 +135,18 @@ public class EncryptUtils { |
| 118 | * @param password 秘钥字符串 | 135 | * @param password 秘钥字符串 |
| 119 | * @return 解密后字符串 | 136 | * @return 解密后字符串 |
| 120 | */ | 137 | */ |
| 138 | public static byte[] decryptByAes(byte[] data, String password) { | ||
| 139 | if (StrUtil.isBlank(password)) { | ||
| 140 | throw new IllegalArgumentException("AES需要传入秘钥信息"); | ||
| 141 | } | ||
| 142 | // aes算法的秘钥要求是16位、24位、32位 | ||
| 143 | int[] array = {16, 24, 32}; | ||
| 144 | if (!ArrayUtil.contains(array, password.length())) { | ||
| 145 | throw new IllegalArgumentException("AES秘钥长度要求为16位、24位、32位"); | ||
| 146 | } | ||
| 147 | return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).decrypt(data); | ||
| 148 | } | ||
| 149 | |||
| 121 | public static String decryptByAes(String data, String password) { | 150 | public static String decryptByAes(String data, String password) { |
| 122 | if (StrUtil.isBlank(password)) { | 151 | if (StrUtil.isBlank(password)) { |
| 123 | throw new IllegalArgumentException("AES需要传入秘钥信息"); | 152 | throw new IllegalArgumentException("AES需要传入秘钥信息"); |
| ... | @@ -143,6 +172,13 @@ public class EncryptUtils { | ... | @@ -143,6 +172,13 @@ public class EncryptUtils { |
| 143 | return ""; | 172 | return ""; |
| 144 | } | 173 | } |
| 145 | 174 | ||
| 175 | public static byte[] decryptByAes(byte[] data) { | ||
| 176 | if(Objects.nonNull(data)) { | ||
| 177 | return decryptByAes(data, AES_SECRET); | ||
| 178 | } | ||
| 179 | return null; | ||
| 180 | } | ||
| 181 | |||
| 146 | /** | 182 | /** |
| 147 | * sm4加密 | 183 | * sm4加密 |
| 148 | * | 184 | * | ... | ... |
| ... | @@ -211,8 +211,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService { | ... | @@ -211,8 +211,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService { |
| 211 | UploadResult uploadResult; | 211 | UploadResult uploadResult; |
| 212 | try { | 212 | try { |
| 213 | if(".pdf".equals(suffix) || ".mp4".equals(suffix) || ".json".equals(suffix) || ".ldr".equals(suffix)) { | 213 | if(".pdf".equals(suffix) || ".mp4".equals(suffix) || ".json".equals(suffix) || ".ldr".equals(suffix)) { |
| 214 | String temp = EncryptUtils.encryptByAes(StrUtil.str(file.getBytes(), StandardCharsets.UTF_8)); | 214 | byte[] encryptBytes = EncryptUtils.encryptByAes(file.getBytes()); |
| 215 | byte[] encryptBytes = StrUtil.bytes(temp, CharsetUtil.CHARSET_UTF_8); | ||
| 216 | uploadResult = storage.uploadSuffix(encryptBytes, suffix, file.getContentType()); | 215 | uploadResult = storage.uploadSuffix(encryptBytes, suffix, file.getContentType()); |
| 217 | } | 216 | } |
| 218 | else{ | 217 | else{ | ... | ... |
-
Please register or sign in to post a comment