package IO; import org.junit.Test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class PictureTest { //对图片进行加密 @Test public void test1() { FileInputStream fi = null; FileOutputStream fo = null; try { fi = new FileInputStream(new File("香港图书馆.jpeg")); fo = new FileOutputStream(new File("香港图书馆secret.jpeg")); byte[] buffer = new byte[1024]; int len; while ((len = fi.read(buffer)) != -1) { for (int i = 0; i < len; i++) { buffer[i] = (byte) (buffer[i] ^ 5); } fo.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { if (fi != null) { try { fi.close(); } catch (IOException e) { e.printStackTrace(); } } if (fo != null) { try { fo.close(); } catch (IOException e) { e.printStackTrace(); } } } } //对图片进行解密 @Test public void test2() { FileInputStream fi = null; FileOutputStream fo = null; try { fi = new FileInputStream(new File("香港图书馆secret.jpeg")); fo = new FileOutputStream(new File("香港图书馆Nosecret.jpeg")); byte[] buffer = new byte[1024]; int len; while ((len = fi.read(buffer)) != -1) { for (int i = 0; i < len; i++) { buffer[i] = (byte) (buffer[i] ^ 5); } fo.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { if (fi != null) { try { fi.close(); } catch (IOException e) { e.printStackTrace(); } } if (fo != null) { try { fo.close(); } catch (IOException e) { e.printStackTrace(); } } } } }