wente
2023-12-18 bdfbb741d767690ea05036a3116e63d06d6bb61b
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.zt.life.oss;
 
import cn.hutool.core.io.IoUtil;
import com.zt.common.utils.UUIDUtil;
import com.zt.core.oss.encry.IOssEncryptService;
import com.zt.modules.oss.cloud.AbstractStorageService;
import org.apache.poi.hpsf.GUID;
import org.springframework.beans.factory.annotation.Autowired;
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 implements IOssEncryptService {
 
    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);
    }
 
    @Override
    public InputStream encrypt(InputStream inputStream) {
        try {
            return encryptStream(inputStream, secureKey);
        } catch (Exception e) {
            e.printStackTrace(); // 或者使用日志记录
            return inputStream;
        }
    }
 
    @Override
    public InputStream decrypt(File file) {
        try {
            return decryptStream(file, secureKey);
        } catch (Exception e) {
            e.printStackTrace(); // 或者使用日志记录
            return IoUtil.toStream(file);
        }
    }
 
    private 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 RuntimeException("密钥生成异常");
        }
    }
 
    private 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);
    }
 
    private 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);
    }
}