7、设计模式应用举例:策略模式:加密、解密文件

目录

一、关于设计模式

二、设计要求

三、设计实现

1.策略

2.具体策略

3.上下文

四、应用程序

五、执行结果


一、关于策略模式

设计模式:7、策略模式(政策)_策略类图-CSDN博客

二、设计要求

用户要对已有的文件进行加密处理,请提供几种加密方案。

三、设计实现

针对上述问题,使用策略模式设计若干类。

1.策略

本问题策略接口提供2个方法,分别实现加密和解密,代码如下:

java 复制代码
package xyz.jangle.design.app.strategy;

import java.io.File;

/**
 * 	策略
 * @author jangle
 * @email jangle@jangle.xyz
 * @time 2026年10月13日 上午8:01:06
 * 
 */
public interface EncryptStrategy {
	
	void encryptFile(File file);	//加密
	
	String decryptFile(File file);	//解密

}
2.具体策略

对于本问题,有两个具体策略StrategyOne和StrategyTwo。

StrategyOne使用加减法进行加解密,StrategyTwo使用异或运算进行加解密。具体代码如下:

StrategyOne:

java 复制代码
package xyz.jangle.design.app.strategy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 具体策略1,使用数组进行加法加密
 * @author jangle
 * @email jangle@jangle.xyz
 * @time 2026年10月13日 上午8:02:54
 * 
 */
public class StrategyOne implements EncryptStrategy {
	
	private String password;
	
	public StrategyOne() {
		this.password = "I love this game";
	}

	public StrategyOne(String password) {
		super();
		if(password == null || password.length()==0) {
			this.password = "I love this game";
		}
		this.password = password;
	}

	@Override
	public void encryptFile(File file) {
		byte[] a = password.getBytes();
		byte[] c =new byte[0];
		int m = 0;
		try(FileInputStream in = new FileInputStream(file)){
			long length = file.length();
			c = new byte[(int)length];
			m = in.read(c);
			for(int k=0;k<m;k++) {
				int n = c[k]+a[k % a.length];	// 加密
				c[k] = (byte) n;
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try(FileOutputStream out = new FileOutputStream(file)){
			out.write(c, 0, m);
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public String decryptFile(File file) {
		
		byte[] a = password.getBytes();
		long length = file.length();
		try (FileInputStream in = new FileInputStream(file)) {
			byte[] c = new byte[(int) length];
			int m = in.read(c);
			for (int k = 0; k < m; k++) {
				int n = c[k] - a[k % a.length];
				c[k] = (byte) n;
			}
			return new String(c,0,m);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}

StrategyTwo:

java 复制代码
package xyz.jangle.design.app.strategy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 具体策略2,使用数组进行异或运算加密
 * @author jangle
 * @email jangle@jangle.xyz
 * @time 2026年10月13日 上午8:23:13
 * 
 */
public class StrategyTwo implements EncryptStrategy {
	
	private String password;
	
	public StrategyTwo() {
		this.password = "I love this game";
	}

	public StrategyTwo(String password) {
		super();
		if(password == null || password.length()==0) {
			this.password = "I love this game";
		}
		this.password = password;
	}

	@Override
	public void encryptFile(File file) {
		byte[] fileByte =null;
		int readLength=0;
		byte[] pwdByte = password.getBytes();
		int pwdLength = pwdByte.length;
		try(FileInputStream in = new FileInputStream(file)){
			fileByte = new byte[(int)file.length()];
			readLength = in.read(fileByte);
			
			for(int k =0;k< readLength;k++) {
				int mm = fileByte[k] ^ pwdByte[k % pwdLength];
				fileByte[k]= (byte) mm;
			}
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try(FileOutputStream out = new FileOutputStream(file)){
			if(fileByte ==null || readLength ==0) {
				System.out.println("文件未加密成功");
				return;
			}
			out.write(fileByte, 0, readLength);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public String decryptFile(File file) {
		
		byte[] pwdBytes = password.getBytes();
		try(FileInputStream in = new FileInputStream(file)){
			byte[] fileBytes = new byte[(int) file.length()];
			int fileLength = in.read(fileBytes);
			
			for(int k = 0;k<fileLength;k++) {
				int mm = fileBytes[k] ^ pwdBytes[k % pwdBytes.length];
				fileBytes[k] = (byte) mm;
			}
			
			return new String(fileBytes,0,fileLength);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}

}
3.上下文

上下文是EncodeContext类,该类包含策略声明用于保存具体策略的引用的变量(strategy)(通过set方法进行注入具体策略):

代码如下:

java 复制代码
package xyz.jangle.design.app.strategy;

import java.io.File;

/**
 * 上下文
 * @author jangle
 * @email jangle@jangle.xyz
 * @time 2026年10月13日 上午8:42:49
 * 
 */
public class EncodeContext {
	
	EncryptStrategy strategy;

	public void setStrategy(EncryptStrategy strategy) {
		this.strategy = strategy;
	}
	
	public void encryptFile(File file) {
		strategy.encryptFile(file);
	}
	
	public String decryptFile(File file) {
		return strategy.decryptFile(file);
	}

}

四、应用程序

下列应用程序中,Application.java使用策略模式中所涉及的类,使用两种策略分别加密、解密文件A.txt和B.txt。

java 复制代码
package xyz.jangle.design.app.strategy;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author jangle
 * @email jangle@jangle.xyz
 * @time 2026年10月13日 上午8:45:21
 * 
 */
public class Application {

	public static void main(String[] args) {
		String filePath = "src/xyz/jangle/design/app/strategy";
		File fileA = new File(filePath+"/A.txt");
		EncodeContext context = new EncodeContext();
		context.setStrategy(new StrategyOne("123"));	//策略1,使用123作为密钥
		context.encryptFile(fileA);
		String s;
		try(BufferedReader in = new BufferedReader(new FileReader(fileA))){
			System.out.println("A文件加密后的内容:");
			while((s=in.readLine()) != null){
				System.out.println(s);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("A文件解密后的内容:");
		System.out.println(context.decryptFile(fileA));
		
		File fileB = new File(filePath + "/B.txt");
		context.setStrategy(new StrategyTwo("321"));	//策略2,使用321作为密钥
		context.encryptFile(fileB);
		try(BufferedReader in = new BufferedReader(new FileReader(fileB))){
			System.out.println("B文件加密后的内容:");
			while((s=in.readLine()) != null){
				System.out.println(s);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("B文件解密后的内容:");
		System.out.println(context.decryptFile(fileB));
	}

}

A.txt :

java 复制代码
123456789

B.txt:

java 复制代码
987654321

五、执行结果

相关推荐
mmsx1 小时前
Android 架构分层:MVVM 与 MVP 共存,网络层怎么切
android·策略模式·mvvm·mvp
4SAPI2 小时前
Provider Adapter设计模式:如何让AI应用支持多个模型
网络·人工智能·设计模式
mmsx3 小时前
Android 本地优先的三级语义理解降级链
android·数码相机·策略模式
geovindu4 小时前
rust: pool Pattern
开发语言·后端·设计模式·rust·对象池简写模式
高晶4 小时前
ARM 嵌入式 Linux 开发:从体系结构到 LED 点亮实战
设计模式·程序员
VimorphTech5 小时前
接入两套 TTS 接口后,业务代码如何避免堆满 if/else?
设计模式
早点睡觉好了6 小时前
LangChain 源码入门 02:消息、ChatPromptTemplate 与 Chat Model 调用链
ai·langchain·策略模式
geovindu7 小时前
rust: pool Pattern (续)
开发语言·后端·设计模式·rust·对象池简写模式
2302_805771071 天前
Day23:POM设计模式(封装)
java·开发语言·设计模式