In this paper, the content

  • Java uses IO streams for batch (a column) encryption and decryption widgets
  • This tool has many shortcomings and can be optimized, self-use small tools, so the expansion needs to be improved.
  • Possible directions for optimization: flexible Settings for character encoding sets, etc

Front knowledge

  • IO stream
  • Exception
  • Scanner
  • The Eclipse tools/IDEA

Have been implemented

  • The value entered through the console returns the encrypted and decrypted strings, corresponding to the encryption and decryption of a single string
  • Through the IO stream, input the result file and the path and file name of the file to be added/decrypted to get the text of the string after being added/decrypted.
  • Shamefully implemented as a jar package, it’s as crude as your first college course design, but it works (under certain circumstances)

Matters needing attention

  • Encryption and decryption This section is only a demo and does not involve specific encryption and decryption operations.
  • Welcome to leave better solutions and valuable suggestions
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;

public class BatchEnDecryptTool {
	public static String ENTER="\n";
	public static void main(String[] args) {
		Scanner scanner=new java.util.Scanner(System.in);
		while(true) {
			System.out.println("Welcome to xx encryption and decryption tool version1.1");
			System.out.println("Please select");
			System.out.println("1. Encryption");
			System.out.println("2. Decryption");
			System.out.println("3. Batch encryption");
			System.out.println("4. Batch decryption");
			System.out.println(9. "" help");
			System.out.println("0. Quit");
			System.out.println("Please enter:");
			int menuSelect=scanner.nextInt();
			if(! ((menuSelect==1)||(menuSelect==2)||(menuSelect==3)||(menuSelect==4)||(menuSelect==9)||(menuSelect==0))) {
				System.out.println("Input error, please re-enter!");
				continue;
			}
			if(menuSelect==1) {
				System.out.println("Please enter what you want to encrypt:");
				String beforeEncryptStr=scanner.next();
				System.out.println("After encryption:");
				System.out.println(encrypt(beforeEncryptStr));
			}else if(menuSelect==2) {
				System.out.println("Please enter what you want to decrypt:");
				String beforeDecryptStr=scanner.next();
				System.out.println("After decryption:");
				System.out.println(decrypt(beforeDecryptStr));
			}else if(menuSelect==3) {
				System.out.println("Please enter the encrypted file storage path and file name:");
				String outPathAndFileName=scanner.next();
				System.out.println("Please enter the file storage path and file name before encryption:");
				String inPathAndFileName=scanner.next();
				try {
					batchEncrypt(outPathAndFileName, inPathAndFileName);
				} catch(IOException e) { e.printStackTrace(); }}else if(menuSelect==4) {
				System.out.println("Please enter the decrypted file storage path and file name:");
				String outPathAndFileName=scanner.next();
				System.out.println("Please enter the file storage path and file name before decryption:");
				String inPathAndFileName=scanner.next();
				try {
					batchDecrypt(outPathAndFileName, inPathAndFileName);
				} catch(IOException e) { e.printStackTrace(); }}else if(menuSelect==9) {
				System.out.println("Help content is as follows:");
			}else if(menuSelect==0) {
				System.out.println("Thank you for using.);
				break; }}}/** * decrypt, abbreviated * here@param beforeDecryptStr
	 * @return* /
	private static  String decrypt(String beforeDecryptStr) {
		return "DECRYPT----"+beforeDecryptStr;
	}
	/** * encryption, abbreviated * here@param beforeEncryptStr
	 * @return* /
	private static String encrypt(String beforeEncryptStr) {
		return "ENCRYPT----"+beforeEncryptStr;
	}
	/** * batch encryption *@param outPathAndFileName
	 * @param inPathAndFileName
	 * @throws IOException 
	 */
	public static void batchEncrypt(String outPathAndFileName,String inPathAndFileName) throws IOException {
		int count=0;
		File fileIn=new File(inPathAndFileName);
		File fileOut=new File(outPathAndFileName);
		InputStreamReader inputStreamReader=new InputStreamReader(new FileInputStream(fileIn));
		OutputStreamWriter outputStreamWriter=new OutputStreamWriter(new FileOutputStream(fileOut));
		BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
		String line_record=bufferedReader.readLine();
		while(null! =line_record&&line_record.length()>0) {
			String encrypted=encrypt(line_record);
			outputStreamWriter.append(encrypted);
			line_record=bufferedReader.readLine();
			count++;
			if(null! =line_record&&line_record.length()>0) {
				outputStreamWriter.append(ENTER);
			}
		}
		System.out.println("This work encryption"+count+"Article");
		System.out.println("Encrypted file path is:"+outPathAndFileName);
		inputStreamReader.close();
		outputStreamWriter.close();
	}
	/** * batch decryption *@param outPathAndFileName
	 * @param inPathAndFileName
	 * @throws IOException 
	 */
	public static void batchDecrypt(String outPathAndFileName,String inPathAndFileName) throws IOException {
		int count=0;
		File fileIn=new File(inPathAndFileName);
		File fileOut=new File(outPathAndFileName);
		InputStreamReader inputStreamReader=new InputStreamReader(new FileInputStream(fileIn));
		OutputStreamWriter outputStreamWriter=new OutputStreamWriter(new FileOutputStream(fileOut));
		BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
		String line_record=bufferedReader.readLine();
		while(null! =line_record&&line_record.length()>0) {
			String decrypted=decrypt(line_record);
			outputStreamWriter.append(decrypted);
			line_record=bufferedReader.readLine();
			count++;
			if(null! =line_record&&line_record.length()>0) {
				outputStreamWriter.append(ENTER);
			}
		}
		System.out.println("This work is decrypted."+count+"Article");
		System.out.println("The decrypted file path is:"+outPathAndFileName); inputStreamReader.close(); outputStreamWriter.close(); }}Copy the code

The results

1. Encrypt a single string

2. Decrypt a single string

3. Encrypt strings in batches

4. Decrypt strings in batches

5. Help and quit

6. File result after operations 3 and 4