jinlin
2025-03-01 86f02fee03614fef275c6e0c355d73318ca3025e
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.example.server.sysOss.service;
 
import cn.hutool.core.io.IoUtil;
import com.example.client.entity.RenException;
import com.example.client.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
 
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.io.*;
import java.security.Key;
import java.security.SecureRandom;
 
@Primary
@Service
public class OssEncryptService {
 
    private static final String CIPHER_ALGORITHM = "AES";
    private static final String KEY = "zhpt-key#%W";
 
    private final Key secureKey;
 
    @Value("${zt.oss.local-path}")
    private String localPath;
 
    public OssEncryptService() {
        this.secureKey = getKey(KEY);
    }
 
    public Key getKey(String strKey) {
        try {
            if (strKey == null) {
                strKey = "";
            }
            KeyGenerator _generator = KeyGenerator.getInstance(CIPHER_ALGORITHM);
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(strKey.getBytes());
            _generator.init(128, secureRandom);
            return _generator.generateKey();
        } catch (Exception e) {
           throw new RenException("密钥生成异常");
        }
    }
 
    public InputStream encryptStream(InputStream inputStream, Key key) throws Exception {
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        String tempUploadDir = localPath + "TEMP_UPLOAD" + File.separator + "TEMP_FILE";
        File dir = new File(tempUploadDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        dir = new File(tempUploadDir);
        // 创建临时文件来保存加密后的数据
        File tempFile = File.createTempFile(UUIDUtil.generateId().toString(), ".tmp", dir);
        FileOutputStream outputStream = new FileOutputStream(tempFile);
 
        byte[] buffer = new byte[8192];
        int bytesRead;
 
        try {
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                byte[] encryptedBytes = cipher.update(buffer, 0, bytesRead);
                outputStream.write(encryptedBytes);
            }
 
            byte[] finalEncryptedBytes = cipher.doFinal();
            outputStream.write(finalEncryptedBytes);
        } finally {
            // 关闭资源
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
        // 返回临时文件的输入流
        return new FileInputStream(tempFile);
    }
 
    public InputStream decryptStream(File file, Key key) throws Exception {
 
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
 
        // 创建临时文件来保存解密后的数据
        String tempUploadDir = localPath + "TEMP_UPLOAD" + File.separator + "TEMP_FILE";
        File dir = new File(tempUploadDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        dir = new File(tempUploadDir);
        // 创建临时文件来保存加密后的数据
        File tempFile = File.createTempFile(UUIDUtil.generateId().toString(), ".tmp", dir);
        FileOutputStream outputStream = new FileOutputStream(tempFile);
 
        FileInputStream fileInputStream = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
 
        byte[] buffer = new byte[8192];
        int bytesRead;
 
        try {
            while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
                byte[] decryptedBytes = cipher.update(buffer, 0, bytesRead);
                outputStream.write(decryptedBytes);
            }
            byte[] finalDecryptedBytes = cipher.doFinal();
            outputStream.write(finalDecryptedBytes);
        } finally {
            // 关闭资源
            try {
                bufferedInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
        // 返回临时文件的输入流
        return new FileInputStream(tempFile);
    }
}