Java 对文件使用二进制加密方式
文件加密算法。key自定义字符串,bytes 为文件二进制流
encryptImg 可加密也可以解密
import lombok.extern.slf4j.Slf4j; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; /** * @Author: Garcia * @CreateDate: 2020/01/10 15:14 * @Description: what it is class? */ @Slf4j public class ImageEncryptUtil { /** * 图片加密&解密方法 * @param bytes * @param key * @return */ public static byte[] encryptImg(byte[] bytes,String key){ BufferedInputStream bis = null; ByteArrayOutputStream bos = null; try { bis = new BufferedInputStream(new ByteArrayInputStream(bytes)); bos = new ByteArrayOutputStream(); int b; while((b = bis.read()) != -1) { char[]ase = key.toCharArray(); int c = -1; for (char ch:ase){ c = (c==-1?b:c)^ch; } bos.write(c); } }catch (Exception e){ log.error("图片加解密失败",e); }finally { try { if (bis!=null){ bis.close(); } if (bos!=null){ bos.close(); } } catch (IOException e) { e.printStackTrace(); } } return bos.toByteArray(); } }
上一篇:
IDEA上Java项目控制台中文乱码