图片加密

    public static void main(String[] args) {
try {
// setImgByte();
resetImg();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void setImgByte() throws FileNotFoundException {
File file = new File("d:\\user\\01411436\\桌面\\tool\\cabinet_green.png");
FileInputStream inputStream = new FileInputStream(file);

String flag = "我是一张图片";
FileOutputStream outputStream = new FileOutputStream(new File("d:\\user\\01411436\\桌面\\tool\\cabinet_green2.png"));
try {
byte[] cs = flag.getBytes("utf-8");
byte[] bytes = IOUtils.toByteArray(inputStream);
byte[] tbytes = new byte[bytes.length + cs.length];
System.arraycopy(cs, 0, tbytes, 0, cs.length);
System.arraycopy(bytes, 0, tbytes, cs.length, bytes.length);

IOUtils.write(tbytes, outputStream);

} catch (IOException e) {
e.printStackTrace();
}
}

public static void resetImg() throws FileNotFoundException {
File file = new File("d:\\user\\01411436\\桌面\\tool\\cabinet_green2.png");
FileInputStream inputStream = new FileInputStream(file);

try {
byte[] bytes = IOUtils.toByteArray(inputStream);
byte[] flag = new byte[18];
System.arraycopy(bytes, 0, flag, 0, 18);
String s = new String(flag, "UTF-8");
if (s.equals("我是一张图片")) {
FileOutputStream outputStream = new FileOutputStream(new File("d:\\user\\01411436\\桌面\\tool\\cabinet_green3.png"));

byte[] tbytes = new byte[bytes.length - flag.length];
System.arraycopy(bytes, 18, tbytes, 0, tbytes.length);
IOUtils.write(tbytes, outputStream);
}
} catch (IOException e) {
e.printStackTrace();
}
}
posted @ 2022-08-10 16:48  霞浦Coder  阅读(85)  评论(0编辑  收藏  举报