- import java.io.*<span style="line-height: 30.8px;">;</span>
- import java.util.Scanner;
- /**
- *
- * @author Wei_John
- * @version 自用图片加密 2015.11.26
- */
- public class Locker {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- System.out.println("*****************************************");
- System.out.println("* 自用图片加密 *");
- System.out.println("* 1.加密 *");
- System.out.println("* 2.解锁 *");
- System.out.println("* by Wei_John *");
- System.out.println("*****************************************");
- Scanner sc = new Scanner(System.in);
- System.out.print("请输入: ");
- String line = sc.nextLine();
- //判断输入是否为1 和 2
- while( ! line.matches("[12]")){
- System.out.print("请输入: ");
- line = sc.nextLine();
- }
- int x = Integer.valueOf(line);
- if (x == 1) {
- // 加密
- Locked(getFile(), Lock(),0);
- System.out.println("加密成功");
- } else if (x == 2) {
- // 解锁
- UnLocked(getFile(), Lock());
- System.out.println("解锁成功");
- }
- }
-
- /**
- * UnLocked
- * @param file
- * @param password
- * @throws FileNotFoundException
- * @throws IOException
- */
-
- private static void UnLocked(File file, int password) throws FileNotFoundException, IOException {
- Locked(file,password,1);
- }
- /**
- * Locked
- * @param file 需要加密文件
- * @param password 密码
- * @throws FileNotFoundException
- * @throws IOException
- */
- private static void Locked(File file, int password, int state)
- throws FileNotFoundException, IOException {
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
- file));
- BufferedOutputStream bos;
- if (state == 0) {
- // 加密副本
- StringBuilder sb = new StringBuilder(file.getAbsolutePath());
- StringBuilder sb2 = sb.replace(file.getAbsolutePath().length()-file.getName().length(),
- file.getAbsolutePath().length(),"加密_"+file.getName());
- bos = new BufferedOutputStream(new FileOutputStream(
- sb2.toString()));
- } else {
- // 解锁副本
- StringBuilder sb = new StringBuilder(file.getAbsolutePath());
- StringBuilder sb2 = sb.replace(file.getAbsolutePath().length()-file.getName().length(),
- file.getAbsolutePath().length(),"解锁_"+file.getName());
-
- bos = new BufferedOutputStream(new FileOutputStream(sb2.toString()));
- }
- int b = -1;
- int count =0;
- while ((b = bis.read()) != -1) {
- count ++;
- bos.write(b ^ password);
- if (count % 1000 == 0) {
- bos.flush();
- }
- }
- bis.close();
- bos.close();
- }
- /**
- * 输入密码
- * @return password
- * @throws IOException
- */
- private static int Lock() throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("请输入密码(学号):");
- String pass = br.readLine();
- char[] ch = pass.toCharArray();
- int password = 0;
- for (char c : ch) {
- password += (int) c;
- }
- return password;
- }
-
- /**
- * 获取加密文件
- * @return 需要加密文件
- * @throws IOException
- */
-
- private static File getFile() throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- while (true) {
- System.out.println("请输入加密文件路径:");
- String path = br.readLine();
- File file = new File(path);
- if (file.isFile()) {
- System.out.println("获取文件成功");
- return file;
- } else {
- System.out.println("您输入的文件路径有误,请重新输入");
- }
- }
- }
- }
复制代码
|