From 7730dfa2b1ffb7e14d29db565ca0f25060bb4715 Mon Sep 17 00:00:00 2001 From: pengren Date: Mon, 15 Apr 2024 11:52:31 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E5=8F=B7=EF=BC=9AZSSAC-163=20?= =?UTF-8?q?=E6=8F=8F=E8=BF=B0=EF=BC=9A=E5=AE=89=E8=A3=85=E5=8C=85=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/common/CommonController.java | 1 - sf-admin/src/main/resources/file/server.crt | 20 + sf-common/pom.xml | 6 + .../com/sf/common/utils/file/FileUtils.java | 33 +- .../com/sf/common/utils/file/ZipUtils.java | 137 ++++++ .../sf/common/utils/openssl/Base64Utils.java | 112 +++++ .../com/sf/common/utils/openssl/ByteUtil.java | 72 +++ .../sf/common/utils/openssl/RsaSignUtil.java | 447 ++++++++++++++++++ .../com/sf/common/utils/sign/Sha265Utils.java | 26 + .../sf/file/controller/SysOssController.java | 17 +- .../com/sf/file/service/ISysOssService.java | 4 + .../file/service/impl/SysOssServiceImpl.java | 9 +- 12 files changed, 875 insertions(+), 9 deletions(-) create mode 100644 sf-admin/src/main/resources/file/server.crt create mode 100644 sf-common/src/main/java/com/sf/common/utils/file/ZipUtils.java create mode 100644 sf-common/src/main/java/com/sf/common/utils/openssl/Base64Utils.java create mode 100644 sf-common/src/main/java/com/sf/common/utils/openssl/ByteUtil.java create mode 100644 sf-common/src/main/java/com/sf/common/utils/openssl/RsaSignUtil.java create mode 100644 sf-common/src/main/java/com/sf/common/utils/sign/Sha265Utils.java diff --git a/sf-admin/src/main/java/com/sf/web/controller/common/CommonController.java b/sf-admin/src/main/java/com/sf/web/controller/common/CommonController.java index 8bd4c5a..ab7e914 100644 --- a/sf-admin/src/main/java/com/sf/web/controller/common/CommonController.java +++ b/sf-admin/src/main/java/com/sf/web/controller/common/CommonController.java @@ -96,7 +96,6 @@ public class CommonController return AjaxResult.error(e.getMessage()); } } - /** * 通用上传请求(多个) */ diff --git a/sf-admin/src/main/resources/file/server.crt b/sf-admin/src/main/resources/file/server.crt new file mode 100644 index 0000000..297fd5c --- /dev/null +++ b/sf-admin/src/main/resources/file/server.crt @@ -0,0 +1,20 @@ +-----BEGIN CERTIFICATE----- +MIIDSDCCAjACCQCvqL+z6dKTrjANBgkqhkiG9w0BAQsFADBmMQswCQYDVQQGEwJD +TjERMA8GA1UECAwIc2hpY2h1YW4xEDAOBgNVBAcMB2NoZW5nZHUxEjAQBgNVBAoM +CXpob25ndGlhbjEKMAgGA1UECwwBQjESMBAGA1UEAwwJemhvbmd0aWFuMB4XDTI0 +MDQxNzA2MjIxNloXDTI1MDQxNzA2MjIxNlowZjELMAkGA1UEBhMCQ04xETAPBgNV +BAgMCHNoaWNodWFuMRAwDgYDVQQHDAdjaGVuZ2R1MRIwEAYDVQQKDAl6aG9uZ3Rp +YW4xCjAIBgNVBAsMAUIxEjAQBgNVBAMMCXpob25ndGlhbjCCASIwDQYJKoZIhvcN +AQEBBQADggEPADCCAQoCggEBAMIXQc0iVEuN/c8PnryNQYP3WbF6irqnjTbk/kCV +552p30ZmdeRDGcFKdkohQLKT06sbqlaBzhGFpnLlb5iz1dG1sGZbw064RFZRlgED +P7ROP891dvj9scPxfWC1Moy6/9LXBQLIshD7zTxzOemqxdyJ1jELianrWWm4XFKM +jvl7BGcfS/VIEafrJ5WGzuRMKtf6SY8aD5Olu0sN61tnSIgjWwZnKkv4V764UBpN +ybTsk1ctefUNMkE5Q+b2gu+0i5qH18B5/H5O7oAEAA+IWIPxIzuINnAYvHRhNQ/W +LMaI/GhYpQMhlmXtOmFFFe3fL4fKRMCKa3x2Va/GzfPjwecCAwEAATANBgkqhkiG +9w0BAQsFAAOCAQEAZmqK3ZfCA3A99VRfSRmCnpBbAW7SIxNeRqKFa5dunOMb7R6B ++bV/ALLJmu8S5D7pSR/wqCGURmnetUnBAEQmYJsjSq3142PeADkr8NmZ9bOXBVNM +szfSPOeM8HCUVjG9r+VMuy6yuPXFzp6QcKC13pzdziMdTHyKSu25fbYwE3lYOXk9 +4nLpnJeT09gecXPdGllrVTKkgkE7L7h4iX/QzsGiZ/JbrCD77nPFqr7fkepsrHjl +4U1Pfjc3NStquhT5ZINDr7MBg/JaHNW57ynt1ud0y1lUOu2o5fpmyYVy/4llRl+T +VJ6mJYXIWr+i10bVjNv2tGPEylmI1JHelYRqAQ== +-----END CERTIFICATE----- diff --git a/sf-common/pom.xml b/sf-common/pom.xml index b6c4893..a370090 100644 --- a/sf-common/pom.xml +++ b/sf-common/pom.xml @@ -15,6 +15,12 @@ + + + commons-fileupload + commons-fileupload + 1.4 + org.springframework diff --git a/sf-common/src/main/java/com/sf/common/utils/file/FileUtils.java b/sf-common/src/main/java/com/sf/common/utils/file/FileUtils.java index c53b282..691c88b 100644 --- a/sf-common/src/main/java/com/sf/common/utils/file/FileUtils.java +++ b/sf-common/src/main/java/com/sf/common/utils/file/FileUtils.java @@ -6,25 +6,27 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; -import java.net.HttpURLConnection; -import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileItemFactory; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.ArrayUtils; import com.sf.common.config.SmarterFrameworkConfig; -import com.sf.common.exception.ServiceException; import com.sf.common.utils.DateUtils; import com.sf.common.utils.StringUtils; import com.sf.common.utils.uuid.IdUtils; import org.apache.commons.io.FilenameUtils; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.commons.CommonsMultipartFile; /** * 文件处理工具类 @@ -295,5 +297,28 @@ public class FileUtils String baseName = FilenameUtils.getBaseName(fileName); return baseName; } + /** + * 根据OutputStream,生成文件 + */ + public static MultipartFile outputToFile(ByteArrayOutputStream bos) throws IOException { + try { + // 文件输出流 + byte[] bytes = bos.toByteArray(); + + FileItemFactory factory = new DiskFileItemFactory(16, null); + FileItem item = factory.createItem("file", "application/zip", false, "apk.zip"); + OutputStream os = item.getOutputStream(); + os.write(bytes); + bos.flush(); + os.flush(); + + // MultipartFile + MultipartFile file = new CommonsMultipartFile(item); + return file; + } catch (Exception e) { + System.err.println("加载私钥失败"); + } + return null; + } } diff --git a/sf-common/src/main/java/com/sf/common/utils/file/ZipUtils.java b/sf-common/src/main/java/com/sf/common/utils/file/ZipUtils.java new file mode 100644 index 0000000..d3afc39 --- /dev/null +++ b/sf-common/src/main/java/com/sf/common/utils/file/ZipUtils.java @@ -0,0 +1,137 @@ +package com.sf.common.utils.file; + + + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipOutputStream; + + +/** + * 压缩文件工具类 + */ +public class ZipUtils { + private ZipFile zipFile; + private ZipOutputStream zipOut; //压缩Zip + private ZipEntry zipEntry; + private static int bufSize; //size of bytes + private byte[] buf; + private int readedBytes; + + public ZipUtils() { + this(512); + } + + public ZipUtils(int bufSize) { + this.bufSize = bufSize; + this.buf = new byte[this.bufSize]; + } + + //压缩文件夹内的文件 + public void doZip(String zipDirectory) {//zipDirectoryPath:需要压缩的文件夹名 + File file; + File zipDir; + + zipDir = new File(zipDirectory); + String zipFileName = zipDir.getName() + ".zip";//压缩后生成的zip文件名 + + try { + this.zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName))); + handleDir(zipDir, this.zipOut); + this.zipOut.close(); + } catch (IOException ioe) { + ioe.printStackTrace(); + } + } + + //由doZip调用,递归完成目录文件读取 + public void handleDir(File dir, ZipOutputStream zipOut) throws IOException { + FileInputStream fileIn; + File[] files; + + files = dir.listFiles(); + + if (files.length == 0) {//如果目录为空,则单独创建之. + //ZipEntry的isDirectory()方法中,目录以"/"结尾. + this.zipOut.putNextEntry(new ZipEntry(dir.toString() + "/")); + this.zipOut.closeEntry(); + } else {//如果目录不为空,则分别处理目录和文件. + for (File fileName : files) { + //System.out.println(fileName); + + if (fileName.isDirectory()) { + handleDir(fileName, this.zipOut); + } else { + fileIn = new FileInputStream(fileName); + this.zipOut.putNextEntry(new ZipEntry(fileName.toString())); + + while ((this.readedBytes = fileIn.read(this.buf)) > 0) { + this.zipOut.write(this.buf, 0, this.readedBytes); + } + } + } + } + } + + public static void zip(ZipOutputStream zipOut, InputStream inputStream, String fileName) throws IOException { + zipOut.putNextEntry(new ZipEntry(fileName)); + + int readBytes = 512; + byte[] buf = new byte[readBytes]; + while ((readBytes = inputStream.read(buf)) > 0) { + zipOut.write(buf, 0, readBytes); + } + zipOut.closeEntry(); + } + + //解压指定zip文件 + public void unZip(String unZipfileName) {//unZipfileName需要解压的zip文件名 + FileOutputStream fileOut; + File file; + InputStream inputStream; + + try { + this.zipFile = new ZipFile(unZipfileName); + + for (Enumeration entries = this.zipFile.entries(); entries.hasMoreElements(); ) { + ZipEntry entry = (ZipEntry) entries.nextElement(); + file = new File(entry.getName()); + + if (entry.isDirectory()) { + file.mkdirs(); + } else { + //如果指定文件的目录不存在,则创建之. + File parent = file.getParentFile(); + if (!parent.exists()) { + parent.mkdirs(); + } + + inputStream = zipFile.getInputStream(entry); + + fileOut = new FileOutputStream(file); + while ((this.readedBytes = inputStream.read(this.buf)) > 0) { + fileOut.write(this.buf, 0, this.readedBytes); + } + fileOut.close(); + + inputStream.close(); + } + } + this.zipFile.close(); + } catch (IOException ioe) { + ioe.printStackTrace(); + } + } + + //设置缓冲区大小 + public void setBufSize(int bufSize) { + this.bufSize = bufSize; + } +} \ No newline at end of file diff --git a/sf-common/src/main/java/com/sf/common/utils/openssl/Base64Utils.java b/sf-common/src/main/java/com/sf/common/utils/openssl/Base64Utils.java new file mode 100644 index 0000000..ed28f3b --- /dev/null +++ b/sf-common/src/main/java/com/sf/common/utils/openssl/Base64Utils.java @@ -0,0 +1,112 @@ +package com.sf.common.utils.openssl; + +import java.io.UnsupportedEncodingException; + +/** + * Base64 加密解密工具类 + * @author XIHONGLEI + * @date 2018-03-27 + */ +public class Base64Utils { + private static char[] base64EncodeChars = new char[] + {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', + 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', + '6', '7', '8', '9', '+', '/'}; + private static byte[] base64DecodeChars = new byte[] + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, + 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, + 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, + -1, -1, -1}; + + public static String encode(byte[] data) { + StringBuffer sb = new StringBuffer(); + int len = data.length; + int i = 0; + int b1, b2, b3; + while (i < len) { + b1 = data[i++] & 0xff; + if (i == len) { + sb.append(base64EncodeChars[b1 >>> 2]); + sb.append(base64EncodeChars[(b1 & 0x3) << 4]); + sb.append("=="); + break; + } + b2 = data[i++] & 0xff; + if (i == len) { + sb.append(base64EncodeChars[b1 >>> 2]); + sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]); + sb.append(base64EncodeChars[(b2 & 0x0f) << 2]); + sb.append("="); + break; + } + b3 = data[i++] & 0xff; + sb.append(base64EncodeChars[b1 >>> 2]); + sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]); + sb.append(base64EncodeChars[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]); + sb.append(base64EncodeChars[b3 & 0x3f]); + } + return sb.toString(); + } + + public static byte[] decode(String str) { + try { + return decodePrivate(str); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + return new byte[] + {}; + } + + private static byte[] decodePrivate(String str) throws UnsupportedEncodingException { + StringBuffer sb = new StringBuffer(); + byte[] data = null; + data = str.getBytes("US-ASCII"); + int len = data.length; + int i = 0; + int b1, b2, b3, b4; + while (i < len) { + do { + b1 = base64DecodeChars[data[i++]]; + } while (i < len && b1 == -1); + if (b1 == -1) { + break; + + } + do { + b2 = base64DecodeChars[data[i++]]; + } while (i < len && b2 == -1); + if (b2 == -1) { + break; + } + sb.append((char) ((b1 << 2) | ((b2 & 0x30) >>> 4))); + + do { + b3 = data[i++]; + if (b3 == 61) { + return sb.toString().getBytes("iso8859-1"); + } + b3 = base64DecodeChars[b3]; + } while (i < len && b3 == -1); + if (b3 == -1) { + break; + } + sb.append((char) (((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2))); + do { + b4 = data[i++]; + if (b4 == 61) { + return sb.toString().getBytes("iso8859-1"); + } + b4 = base64DecodeChars[b4]; + } while (i < len && b4 == -1); + if (b4 == -1) { + break; + } + sb.append((char) (((b3 & 0x03) << 6) | b4)); + } + return sb.toString().getBytes("iso8859-1"); + } +} \ No newline at end of file diff --git a/sf-common/src/main/java/com/sf/common/utils/openssl/ByteUtil.java b/sf-common/src/main/java/com/sf/common/utils/openssl/ByteUtil.java new file mode 100644 index 0000000..21eb69d --- /dev/null +++ b/sf-common/src/main/java/com/sf/common/utils/openssl/ByteUtil.java @@ -0,0 +1,72 @@ +package com.sf.common.utils.openssl; + +import java.io.*; + +/** + * byte数组工具类实现byte[]与文件之间的相互转换 + * @author XIHONGLEI + * @Date 2018-03-26 + */ +public class ByteUtil { + /** + * 获得指定文件的byte数组 + */ + public static byte[] getBytes(String filePath){ + byte[] buffer = null; + try { + File file = new File(filePath); + FileInputStream fis = new FileInputStream(file); + ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); + byte[] b = new byte[1000]; + int n; + while ((n = fis.read(b)) != -1) { + bos.write(b, 0, n); + } + fis.close(); + bos.close(); + buffer = bos.toByteArray(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return buffer; + } + + /** + * 根据byte数组,生成文件 + */ + public static void saveFile(byte[] bfile, String filePath,String fileName) { + BufferedOutputStream bos = null; + FileOutputStream fos = null; + File file = null; + try { + File dir = new File(filePath); + //判断文件目录是否存在 + if(!dir.exists()&&dir.isDirectory()){ + dir.mkdirs(); + } + file = new File(filePath+"\\"+fileName); + fos = new FileOutputStream(file); + bos = new BufferedOutputStream(fos); + bos.write(bfile); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (bos != null) { + try { + bos.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + if (fos != null) { + try { + fos.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + } + } +} \ No newline at end of file diff --git a/sf-common/src/main/java/com/sf/common/utils/openssl/RsaSignUtil.java b/sf-common/src/main/java/com/sf/common/utils/openssl/RsaSignUtil.java new file mode 100644 index 0000000..ed5026e --- /dev/null +++ b/sf-common/src/main/java/com/sf/common/utils/openssl/RsaSignUtil.java @@ -0,0 +1,447 @@ +package com.sf.common.utils.openssl; + +import cn.hutool.core.io.resource.ClassPathResource; +import com.sf.common.utils.file.FileUtils; +import com.sf.common.utils.file.ZipUtils; +import com.sf.common.utils.sign.Sha265Utils; +import org.springframework.stereotype.Component; +import org.springframework.web.multipart.MultipartFile; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import java.io.*; +import java.security.*; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; +import java.util.zip.ZipOutputStream; + +/** + * rsa加密 + */ +@Component +public class RsaSignUtil { + + private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqvm+B2e3shKKN1PGXfTWGc7+gC+XnKvtZX/T9ADWtL7KvAqhAREtxcFRtPC9ONbEqSfHb53mUUJbmKrp5C1rFrWQahBP8mpFOtjoReOkqF/idbwAKtvOq5DIuQT7Wl7wgDdh4OvEWqnUaWvkV0qJNK/Cny1YL8R+DJrnlAiREQwIDAQAB"; + private static final String PRIVATE_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDCF0HNIlRLjf3PD568jUGD91mxeoq6p4025P5Aleedqd9GZnXkQxnBSnZKIUCyk9OrG6pWgc4RhaZy5W+Ys9XRtbBmW8NOuERWUZYBAz+0Tj/PdXb4/bHD8X1gtTKMuv/S1wUCyLIQ+808cznpqsXcidYxC4mp61lpuFxSjI75ewRnH0v1SBGn6yeVhs7kTCrX+kmPGg+TpbtLDetbZ0iII1sGZypL+Fe+uFAaTcm07JNXLXn1DTJBOUPm9oLvtIuah9fAefx+Tu6ABAAPiFiD8SM7iDZwGLx0YTUP1izGiPxoWKUDIZZl7TphRRXt3y+HykTAimt8dlWvxs3z48HnAgMBAAECggEAMnoB7uuCqLOg0nzpd3f1FfD6LqID+k8g9qnPRdS2i9Zl7tUuy9J3nEF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8rAVYJjklcIZHI1TF0Mm7RVrM+Kw3VzHR21jf7RNboBAAH+YeUH9yZg936iqO4Gi5wegUtttXgrxxG5rwi8iUXbbiC/xLVm0ldHW5Q=="; + public static MultipartFile signZip(MultipartFile file) throws Exception { + RsaSignUtil rsaSignUtil = new RsaSignUtil(); + //初始化密钥 + try { + rsaSignUtil.loadPrivateKey(PRIVATE_KEY); + System.out.println("加载私钥成功"); + } catch (Exception e) { + System.err.println(e.getMessage()); + System.err.println("加载私钥失败"); + } + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ZipOutputStream out = new ZipOutputStream(outputStream); + //放入文件 + ZipUtils.zip(out, new ByteArrayInputStream(file.getBytes()), file.getOriginalFilename()); + //放入签名 + String encrypt = Sha265Utils.encrypt(file.getBytes()); + ZipUtils.zip(out, new ByteArrayInputStream(rsaSignUtil.rsaSign(encrypt.getBytes(), rsaSignUtil.getPrivateKey())), "sign.signture"); + //放入证书 + ZipUtils.zip(out, new ByteArrayInputStream(new ClassPathResource("file/server.crt").readBytes()), "server.crt"); + outputStream.close(); + out.close(); + return FileUtils.outputToFile(outputStream); + } + /** + * rsa签名 + * @param data 待签名的字符串 + * @param priKey rsa私钥字符串 + * @return 签名结果 + * @throws Exception 签名失败则抛出异常 + */ + public byte[] rsaSign(byte[] data, RSAPrivateKey priKey) throws SignatureException { + try { + Signature signature = Signature.getInstance("SHA256withRSA"); + signature.initSign(priKey); + signature.update(data); + + byte[] signed = signature.sign(); + return signed; + } catch (Exception e) { + throw new SignatureException("RSAcontent = " + data + + "; charset = ", e); + } + } + + /** + * rsa验签 + * + * @param data 被签名的内容 + * @param sign 签名后的结果 + * @param pubKey rsa公钥 + * @return 验签结果 + * @throws SignatureException 验签失败,则抛异常 + */ + public boolean verify(byte[] data, byte[] sign, RSAPublicKey pubKey) + throws SignatureException { + try { + Signature signature = Signature.getInstance("SHA512withRSA"); + signature.initVerify(pubKey); + signature.update(data); + return signature.verify(sign); + + } catch (Exception e) { + e.printStackTrace(); + throw new SignatureException("RSA验证签名[content = " + data + + "; charset = " + "; signature = " + sign + "]发生异常!", e); + } + } + + /** + * 私钥 + */ + private RSAPrivateKey privateKey; + + /** + * 公钥 + */ + private RSAPublicKey publicKey; + + /** + * 字节数据转字符串专用集合 + */ + private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', + '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + + /** + * 获取私钥 + * + * @return 当前的私钥对象 + */ + public RSAPrivateKey getPrivateKey() { + return privateKey; + } + + /** + * 获取公钥 + * + * @return 当前的公钥对象 + */ + public RSAPublicKey getPublicKey() { + return publicKey; + } + + /** + * 随机生成密钥对 + */ + public void genKeyPair() { + KeyPairGenerator keyPairGen = null; + try { + keyPairGen = KeyPairGenerator.getInstance("RSA"); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + keyPairGen.initialize(1024, new SecureRandom()); + KeyPair keyPair = keyPairGen.generateKeyPair(); + this.privateKey = (RSAPrivateKey) keyPair.getPrivate(); + this.publicKey = (RSAPublicKey) keyPair.getPublic(); + } + + /** + * 从.pem文件中取得私钥 + * + * @param filePath 文件路径 + * @return 私钥 + */ + public String getPrivateKeyFromFile(String filePath) { + String strPrivateKey = ""; + try { + BufferedReader privateKey = new BufferedReader(new FileReader(filePath)); + String line = ""; + while ((line = privateKey.readLine()) != null) { + strPrivateKey += line; + } + privateKey.close(); + strPrivateKey = strPrivateKey.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", ""); + } catch (Exception e) { + e.printStackTrace(); + } + return strPrivateKey; + } + + /** + * 从.pem文件中取得公钥 + * + * @param filePath 文件路径 + * @return 公钥 + */ + public String getPublicKeyFromFile(String filePath) { + String strPublicKey = ""; + try { + BufferedReader publicKey = new BufferedReader(new FileReader(filePath)); + String line = ""; + while ((line = publicKey.readLine()) != null) { + strPublicKey += line; + } + publicKey.close(); + strPublicKey = strPublicKey.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", ""); + } catch (Exception e) { + e.printStackTrace(); + } + return strPublicKey; + } + + /** + * 从字符串中加载公钥 + * + * @param publicKeyStr 公钥数据字符串 + * @throws Exception 加载公钥时产生的异常 + */ + public void loadPublicKey(String publicKeyStr) throws Exception { + try { + byte[] buffer = Base64Utils.decode(publicKeyStr); + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer); + this.publicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec); + } catch (NoSuchAlgorithmException e) { + throw new Exception("无此算法"); + } catch (InvalidKeySpecException e) { + throw new Exception("公钥非法"); + } catch (NullPointerException e) { + throw new Exception("公钥数据为空"); + } + } + + /** + * 加载私钥 + * + * @param privateKeyStr 私钥文件名 + * @return 是否成功 + * @throws Exception + */ + public void loadPrivateKey(String privateKeyStr) throws Exception { + try { + byte[] buffer = Base64Utils.decode(privateKeyStr); + PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer); + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + this.privateKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec); + } catch (NoSuchAlgorithmException e) { + throw new Exception("无此算法"); + } catch (InvalidKeySpecException e) { + throw new Exception("私钥非法"); + } catch (NullPointerException e) { + throw new Exception("私钥数据为空"); + } + } + + /** + * 加密过程 + * + * @param publicKey 公钥 + * @param plainTextData 明文数据 + * @return + * @throws Exception 加密过程中的异常信息 + */ + public byte[] encrypt(RSAPublicKey publicKey, byte[] plainTextData) + throws Exception { + if (publicKey == null) { + throw new Exception("加密公钥为空, 请设置"); + } + Cipher cipher = null; + try { + cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.ENCRYPT_MODE, publicKey); + byte[] output = cipher.doFinal(plainTextData); + return output; + } catch (NoSuchAlgorithmException e) { + throw new Exception("无此加密算法"); + } catch (NoSuchPaddingException e) { + e.printStackTrace(); + return null; + } catch (InvalidKeyException e) { + throw new Exception("加密公钥非法,请检查"); + } catch (IllegalBlockSizeException e) { + throw new Exception("明文长度非法"); + } catch (BadPaddingException e) { + throw new Exception("明文数据已损坏"); + } + } + + /** + * 解密过程 + * + * @param privateKey 私钥 + * @param cipherData 密文数据 + * @return 明文 + * @throws Exception 解密过程中的异常信息 + */ + public byte[] decrypt(RSAPrivateKey privateKey, byte[] cipherData) + throws Exception { + if (privateKey == null) { + throw new Exception("解密私钥为空, 请设置"); + } + Cipher cipher = null; + try { + cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.DECRYPT_MODE, privateKey); + byte[] output = cipher.doFinal(cipherData); + return output; + } catch (NoSuchAlgorithmException e) { + throw new Exception("无此解密算法"); + } catch (NoSuchPaddingException e) { + e.printStackTrace(); + return null; + } catch (InvalidKeyException e) { + throw new Exception("解密私钥非法,请检查"); + } catch (IllegalBlockSizeException e) { + throw new Exception("密文长度非法"); + } catch (BadPaddingException e) { + throw new Exception("密文数据已损坏"); + } + } + + /** + * 字节数据转十六进制字符串 + * + * @param data 输入数据 + * @return 十六进制内容 + */ + public static String byteArrayToString(byte[] data) { + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < data.length; i++) { + // 取出字节的高四位 作为索引得到相应的十六进制标识符 注意无符号右移 + stringBuilder.append(HEX_CHAR[(data[i] & 0xf0) >>> 4]); + // 取出字节的低四位 作为索引得到相应的十六进制标识符 + stringBuilder.append(HEX_CHAR[(data[i] & 0x0f)]); + if (i < data.length - 1) { + stringBuilder.append(' '); + } + } + return stringBuilder.toString(); + } + + /** + * btye转换hex函数 + * + * @param byteArray + * @return + */ + public static String byteToHex(byte[] byteArray) { + StringBuffer strBuff = new StringBuffer(); + for (int i = 0; i < byteArray.length; i++) { + if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) { + strBuff.append("0").append( + Integer.toHexString(0xFF & byteArray[i])); + } else { + strBuff.append(Integer.toHexString(0xFF & byteArray[i])); + } + } + return strBuff.toString(); + } + + /** + * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 + */ + public static byte[] readFileByBytes(String fileName) { + File file = new File(fileName); + InputStream in = null; + byte[] txt = new byte[(int) file.length()]; + try { + // 一次读一个字节 + in = new FileInputStream(file); + int tempbyte; + int i = 0; + while ((tempbyte = in.read()) != -1) { + txt[i] = (byte) tempbyte; + i++; + } + in.close(); + return txt; + } catch (IOException e) { + e.printStackTrace(); + return txt; + } + } + + +// /** +// * Main 测试方法 +// * @param args +// */ +// public static void main(String[] args) throws FileNotFoundException, CertificateException { +// RsaEncrypt rsaEncrypt = new RsaEncrypt(); +// try { +// String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwhdBzSJUS439zw+evI1Bg/dZsXqKuqeNNuT+QJXnnanfRmZ15EMZwUp2SiFAspPTqxuqVoHOEYWmcuVvmLPV0bWwZlvDTrhEVlGWAQM/tE4/z3V2+P2xw/F9YLUyjLr/0tcFAsiyEPvNPHM56arF3InWMQuJqetZabhcUoyO+XsEZx9L9UgRp+snlYbO5Ewq1/pJjxoPk6W7Sw3rW2dIiCNbBmcqS/hXvrhQGk3JtOyTVy159Q0yQTlD5vaC77SLmofXwHn8fk7ugAQAD4hYg/EjO4g2cBi8dGE1D9Ysxoj8aFilAyGWZe06YUUV7d8vh8pEwIprfHZVr8bN8+PB5wIDAQAB"; +// rsaEncrypt.loadPublicKey(publicKey); +// System.out.println("加载公钥成功"); +// } catch (Exception e) { +// System.err.println(e.getMessage()); +// System.err.println("加载公钥失败"); +// } +// +// // 加载私钥 +// try { +// String privateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDCF0HNIlRLjf3P\n" + +// "D568jUGD91mxeoq6p4025P5Aleedqd9GZnXkQxnBSnZKIUCyk9OrG6pWgc4RhaZy\n" + +// "5W+Ys9XRtbBmW8NOuERWUZYBAz+0Tj/PdXb4/bHD8X1gtTKMuv/S1wUCyLIQ+808\n" + +// "cznpqsXcidYxC4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iGDsLMzHK3OI3B4mNuFeAMynAn6KnMiYeX0M2HGVKwXzlyv/ZswDR\n" + +// "0D7L9I7gauI/Rqa8WGaAB8SGL0zgeFH5a2RtUhXMYlANZiEZ+GUzcy3ChQKBgQCR\n" + +// "tDV0Vox4X4xPwZ2b9we49xi7CtWaAneCUwHwVXtkjwiOGhOJseL1jr1yMqjsN7l5\n" + +// "v5Y3E2ybzNz79xWgPPK1rCzL90dS1l3N+moRSRdR0iWzRFkoSNZEeEmG/x6uy954\n" + +// "Ra3nSvozMH7ZLwbakZ/BftEI6ScHihaScp2xSxU4cQKBgQDANT+6d3E6aGT2oAdT\n" + +// "xAdimcLEWZOVW9gkbX/UASEpTjEMDdr+MoPuKSPYxuMcWf/HksJF8rAVYJjklcIZ\n" + +// "HI1TF0Mm7RVrM+Kw3VzHR21jf7RNboBAAH+YeUH9yZg936iqO4Gi5wegUtttXgrx\n" + +// "xG5rwi8iUXbbiC/xLVm0ldHW5Q=="; +// rsaEncrypt.loadPrivateKey(privateKey); +// System.out.println("加载私钥成功"); +// } catch (Exception e) { +// System.err.println(e.getMessage()); +// System.err.println("加载私钥失败"); +// } +// //测试字符串 +// String encryptStr = "12321dsfasf1321312fsfdsafsdafasfsadf"; +// try { +// System.out.println(new Date()); +// System.out.println(new Date()); +// // 加密 +// byte[] cipher = rsaEncrypt.encrypt(rsaEncrypt.getPublicKey(), +// encryptStr.getBytes()); +// // 解密 +// byte[] plainText = rsaEncrypt.decrypt(rsaEncrypt.getPrivateKey(), +// cipher); +// System.out.println(new Date()); +// System.out.println(new String(plainText)); +// byte[] content = readFileByBytes("/Users/a1234/Documents/ca/demo.zip"); +// // 签名验证 +// byte[] signbyte = rsaEncrypt.rsaSign(content, rsaEncrypt.getPrivateKey()); +// System.out.println("签名-----:" + byteToHex(signbyte)); +// ByteUtil.saveFile(signbyte,"/Users/a1234/Documents/ca/","demo.zip.sign"); +// Boolean isok = rsaEncrypt.verify(content, signbyte, rsaEncrypt.getPublicKey()); +// System.out.println("验证:" + isok); +// +// // 读取验证文件 +// byte[] read = readFileByBytes("/Users/a1234/Documents/ca/demo.zip.sign"); +// System.out.println("读取签名文件:" + byteToHex(signbyte)); +// Boolean isfok = rsaEncrypt.verify(content, read, rsaEncrypt.getPublicKey()); +// System.out.println("文件验证2:" + isfok); +// +// } catch (Exception e) { +// System.err.println(e.getMessage()); +// } +// } + +} \ No newline at end of file diff --git a/sf-common/src/main/java/com/sf/common/utils/sign/Sha265Utils.java b/sf-common/src/main/java/com/sf/common/utils/sign/Sha265Utils.java new file mode 100644 index 0000000..e0514ed --- /dev/null +++ b/sf-common/src/main/java/com/sf/common/utils/sign/Sha265Utils.java @@ -0,0 +1,26 @@ +package com.sf.common.utils.sign; + +import java.security.MessageDigest; + +/** + * Sha256加密方法 + * + * @author ztzh + */ +public class Sha265Utils +{ + private static final String SHA_256_ALGORITHM = "SHA-256"; + public static String encrypt(byte[] data) throws Exception { + //获取SHA-256算法实例 + MessageDigest messageDigest = MessageDigest.getInstance(SHA_256_ALGORITHM); + //计算散列值 + byte[] digest = messageDigest.digest(data); + StringBuilder stringBuilder = new StringBuilder(); + //将byte数组转换为15进制字符串 + for (byte b : digest) { + stringBuilder.append(Integer.toHexString((b & 0xFF) | 0x100).toUpperCase(), 1, 3); + } + return stringBuilder.toString(); + } + +} diff --git a/sf-file/src/main/java/com/sf/file/controller/SysOssController.java b/sf-file/src/main/java/com/sf/file/controller/SysOssController.java index 379ec92..49cd5f5 100644 --- a/sf-file/src/main/java/com/sf/file/controller/SysOssController.java +++ b/sf-file/src/main/java/com/sf/file/controller/SysOssController.java @@ -124,7 +124,7 @@ public class SysOssController extends BaseController * * @param file 文件 */ - //@PreAuthorize("@ss.hasPermi('system:oss:upload')") + @PreAuthorize("@ss.hasPermi('system:oss:upload')") @Log(title = "OSS对象存储", businessType = BusinessType.INSERT) @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public R upload(@RequestPart("file") MultipartFile file) { @@ -140,6 +140,21 @@ public class SysOssController extends BaseController return R.ok(oss); } + + @Log(title = "OSS对象存储", businessType = BusinessType.INSERT) + @PostMapping(value = "/zip/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public R zipUpload(@RequestPart("file") MultipartFile file) throws Exception { + if (ObjectUtil.isNull(file)) { + throw new ServiceException("上传文件不能为空"); + } + long size = file.getSize(); + if (size > defaultMaxSize) { + throw new FileSizeLimitExceededException(defaultMaxSize / 1024 / 1024); + } + SysOss oss = sysOssService.uploadSignZip(file); + return R.ok(oss); + } + /** * 下载OSS对象 * diff --git a/sf-file/src/main/java/com/sf/file/service/ISysOssService.java b/sf-file/src/main/java/com/sf/file/service/ISysOssService.java index 34083d1..6dfcd03 100644 --- a/sf-file/src/main/java/com/sf/file/service/ISysOssService.java +++ b/sf-file/src/main/java/com/sf/file/service/ISysOssService.java @@ -1,6 +1,8 @@ package com.sf.file.service; import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; import java.util.List; import javax.servlet.http.HttpServletResponse; @@ -69,4 +71,6 @@ public interface ISysOssService { SysOss upload(File file); void download(String ossId, HttpServletResponse response) throws Exception; + + SysOss uploadSignZip(MultipartFile file) throws Exception; } diff --git a/sf-file/src/main/java/com/sf/file/service/impl/SysOssServiceImpl.java b/sf-file/src/main/java/com/sf/file/service/impl/SysOssServiceImpl.java index 10a9e4f..a937a9b 100644 --- a/sf-file/src/main/java/com/sf/file/service/impl/SysOssServiceImpl.java +++ b/sf-file/src/main/java/com/sf/file/service/impl/SysOssServiceImpl.java @@ -1,13 +1,12 @@ package com.sf.file.service.impl; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.InputStream; +import java.io.*; import java.util.List; import java.util.UUID; import javax.servlet.http.HttpServletResponse; +import com.sf.common.utils.openssl.RsaSignUtil; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -162,6 +161,10 @@ public class SysOssServiceImpl implements ISysOssService { throw new ServiceException(e.getMessage()); } } + @Override + public SysOss uploadSignZip(MultipartFile file) throws Exception { + return upload(RsaSignUtil.signZip(file)); + } public UploadResult uploadSuffix(File file, String suffix) { return upload(file, getPath(null, suffix));