标签:文件 AES outputStream 加解密 param inputStream cipher file import
package com.yang.ftpdemo.crypt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
import java.util.UUID;
@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {
/**
* 加密接口
*
* @param file 文件本体
* @return
*/
@PostMapping("/encrypt")
public void encrypt(@RequestParam("file") MultipartFile file) throws IOException {
if (file.isEmpty()) {
log.error("【保存失败!文件出错,file={}】", file);
}
// 文件名加上文件后缀名
String originalName = file.getOriginalFilename();
String suffixName = originalName.substring(originalName.lastIndexOf("."));
String filename = UUID.randomUUID() + String.valueOf(new Random().nextInt(1000)) + suffixName;
encrypt(file.getInputStream(), new FileOutputStream("D:\\22222222.exe"), initAESCipher("123", 1));
}
/**
* 加密静态方法
*
* @param inputStream
* @param outputStream
* @param cipher
*/
public static void encrypt(InputStream inputStream, OutputStream outputStream, Cipher cipher) {
try {
// 创建加密流
CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher);
int isread = 0;
byte[] cache = new byte[1024];
// 加密流写入文件
while ((isread = cipherInputStream.read(cache, 0, cache.length)) != -1) {
outputStream.write(cache, 0, isread);
}
cipherInputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 解密接口
*
* @param response
* @param file
* @throws IOException
*/
@PostMapping("/decrypt")
public void decrypt(HttpServletResponse response, @RequestParam("file") MultipartFile file) throws IOException {
response.setHeader("content-disposition", "attachment;filename=1333");
decrypt(file.getInputStream(), response.getOutputStream(), initAESCipher("123", 2));
}
/**
* 解密静态方法
*
* @param inputStream
* @param outputStream
* @param cipher
*/
public static void decrypt(InputStream inputStream, OutputStream outputStream, Cipher cipher) {
try {
// 创建解密流
CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);
int isread = 0;
byte[] cache = new byte[1024];
while ((isread = inputStream.read(cache, 0, cache.length)) != -1) {
cipherOutputStream.write(cache, 0, isread);
}
cipherOutputStream.flush();
cipherOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Cipher初始化静态方法
*
* @param sKey
* @param cipherMode
* @return
*/
public static Cipher initAESCipher(String sKey, int cipherMode) {
Cipher cipher = null;
KeyGenerator generator = null;
try {
generator = KeyGenerator.getInstance("AES");
generator.init(128, new SecureRandom(sKey.getBytes()));
SecretKey secretKey = generator.generateKey();
byte[] codeFormat = secretKey.getEncoded();
cipher = Cipher.getInstance("AES");
SecretKeySpec keySpec = new SecretKeySpec(codeFormat, "AES");
cipher.init(cipherMode, keySpec);
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException e) {
e.printStackTrace();
}
return cipher;
}
}
标签:文件,AES,outputStream,加解密,param,inputStream,cipher,file,import 来源: https://www.cnblogs.com/JaxYoun/p/13156944.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。