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); } }