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

五、执行结果

相关推荐
AI酒旅实战开发16 小时前
旅行 AI Agent 的 7 种设计模式:从工具调用到交易闭环
人工智能·设计模式
geovindu2 天前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
geovindu3 天前
rust: Timing Functions Pattern
开发语言·后端·设计模式·rust·计时函数模式·性能分析型模式
Mh8 天前
我是如何设计品牌站组件库的?
前端·css·设计模式
是2的10次方啊9 天前
设计模式与生活·总论:AI 能替你砌墙,替不了你打地基
设计模式
dahaihua3339 天前
用 SysML 表达航电系统架构(附建模示例)
设计模式·重构·系统架构·集成测试·需求分析·学习方法
麻瓜code10 天前
用策略模式写一个可断点续传的状态机
策略模式
wy_hhxx10 天前
《Agent设计模式 图解可复用智能体架构》上篇内容整理
设计模式·架构·ai agent
AAA修煤气灶刘哥10 天前
DSH Super-agent 双层架构:如何兼顾后台执行与持续响应
人工智能·后端·设计模式