1. 程式人生 > >利用BufferStream來進行圖片加密與解密

利用BufferStream來進行圖片加密與解密

加密類:

public static void JiaMi() {
		int a;
		try {
			BufferedInputStream input = new BufferedInputStream(
					new FileInputStream("C:\\Users\\Lee\\Desktop\\001.jpg"));
			BufferedOutputStream output = new BufferedOutputStream(
					new FileOutputStream("C:\\Users\\Lee\\Desktop\\fake.jpg"));

			try {
				while ((a = input.read()) != -1) {
					output.write(a + 1); 
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			System.out.println("找不到檔案");
		}

	}

解密類:

public static void JieMi() {
		int a;
		try {
			BufferedInputStream input = new BufferedInputStream(
					new FileInputStream("C:\\Users\\Lee\\Desktop\\fake.jpg"));
			BufferedOutputStream output = new BufferedOutputStream(
					new FileOutputStream("C:\\Users\\Lee\\Desktop\\real.jpg"));

			try {
				while ((a = input.read()) != -1) {
					output.write(a - 1);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			System.out.println("找不到檔案");
		}

	}

 執行結果: