目前常用的加密算法有DES(Data Encryption Standard)和IDEA(International Data Encryption Algorithm)国际数据加密算法等,请用工厂方法实现加密算法系统。
实验要求:
1.画出对应的类图;
2.提交该系统的代码,该系统务必是一个可以能够直接使用的系统,查阅资料完成相应加密算法的实现;
3.注意编程规范。
1.类图
2. 系统代码
package org.example;
public interface EncryptionAlgorithm { byte[] encrypt(byte[] data); }
package org.example;
import java.util.Arrays;
public class DESAlgorithm implements EncryptionAlgorithm { @Override public byte[] encrypt(byte[] data) { // 实现DES加密逻辑 return Arrays.copyOfRange(data, 0, data.length); } }
package org.example;
import java.util.Arrays;
public class IDEAAlgorithm implements EncryptionAlgorithm { @Override public byte[] encrypt(byte[] data) { // 实现IDEA加密逻辑 return Arrays.copyOfRange(data, 0, data.length); } }
package org.example;
public class EncryptionAlgorithmFactory { public static EncryptionAlgorithm getEncryptionAlgorithm(String algorithmType) { if ("DES".equalsIgnoreCase(algorithmType)) { return new DESAlgorithm(); } else if ("IDEA".equalsIgnoreCase(algorithmType)) { return new IDEAAlgorithm(); } else { throw new IllegalArgumentException("Unsupported algorithm type: " + algorithmType); } } }
package org.example;
import java.util.Arrays;
public class Main { public static void main(String[] args) { String algorithmType = "DES"; // 或者 "IDEA" EncryptionAlgorithm encryptionAlgorithm = EncryptionAlgorithmFactory.getEncryptionAlgorithm(algorithmType);
byte[] data = "Hello, World!".getBytes(); byte[] encryptedData = encryptionAlgorithm.encrypt(data);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下