当前位置: 首页 > news >正文

11.7日学习笔记

一、类图设计
Mermaid

classDiagram
class EncryptionAlgorithm {
<>
+encrypt(data: String): String
+decrypt(data: String): String
}

class DESAlgorithm {+encrypt(data: String): String+decrypt(data: String): String
}class IDEAAlgorithm {+encrypt(data: String): String+decrypt(data: String): String
}class EncryptionFactory {<<interface>>+createAlgorithm(): EncryptionAlgorithm
}class DESFactory {+createAlgorithm(): EncryptionAlgorithm
}class IDEAFactory {+createAlgorithm(): EncryptionAlgorithm
}class Client {-factory: EncryptionFactory+encryptData(data: String): String+decryptData(data: String): String
}EncryptionFactory ..> EncryptionAlgorithm : creates
DESFactory ..|> EncryptionFactory
IDEAFactory ..|> EncryptionFactory
DESFactory ..> DESAlgorithm : creates
IDEAFactory ..> IDEAAlgorithm : creates
EncryptionAlgorithm <|.. DESAlgorithm
EncryptionAlgorithm <|.. IDEAAlgorithm
Client ..> EncryptionFactory : uses

二、代码实现

  1. 加密算法接口和实现类

// EncryptionAlgorithm.java
public interface EncryptionAlgorithm {
String encrypt(String data) throws Exception;
String decrypt(String data) throws Exception;
}

// DESAlgorithm.java
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class DESAlgorithm implements EncryptionAlgorithm {
private static final String ALGORITHM = "DES";
private SecretKey key;

public DESAlgorithm() throws Exception {// 生成密钥KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);keyGen.init(56); // DES使用56位密钥this.key = keyGen.generateKey();
}@Override
public String encrypt(String data) throws Exception {Cipher cipher = Cipher.getInstance(ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE, key);byte[] encryptedBytes = cipher.doFinal(data.getBytes());return Base64.getEncoder().encodeToString(encryptedBytes);
}@Override
public String decrypt(String data) throws Exception {Cipher cipher = Cipher.getInstance(ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, key);byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(data));return new String(decryptedBytes);
}

}

// IDEAAlgorithm.java
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class IDEAAlgorithm implements EncryptionAlgorithm {
private static final String ALGORITHM = "IDEA";
private SecretKey key;

public IDEAAlgorithm() throws Exception {// 生成密钥KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);keyGen.init(128); // IDEA使用128位密钥this.key = keyGen.generateKey();
}@Override
public String encrypt(String data) throws Exception {Cipher cipher = Cipher.getInstance(ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE, key);byte[] encryptedBytes = cipher.doFinal(data.getBytes());return Base64.getEncoder().encodeToString(encryptedBytes);
}@Override
public String decrypt(String data) throws Exception {Cipher cipher = Cipher.getInstance(ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, key);byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(data));return new String(decryptedBytes);
}

}
2. 工厂接口和实现类

// EncryptionFactory.java
public interface EncryptionFactory {
EncryptionAlgorithm createAlgorithm() throws Exception;
}

// DESFactory.java
public class DESFactory implements EncryptionFactory {
@Override
public EncryptionAlgorithm createAlgorithm() throws Exception {
return new DESAlgorithm();
}
}

// IDEAFactory.java
public class IDEAFactory implements EncryptionFactory {
@Override
public EncryptionAlgorithm createAlgorithm() throws Exception {
return new IDEAAlgorithm();
}
}
3. 客户端类

// Client.java
public class Client {
private EncryptionFactory factory;
private EncryptionAlgorithm algorithm;

public Client(EncryptionFactory factory) throws Exception {this.factory = factory;this.algorithm = factory.createAlgorithm();
}public String encryptData(String data) throws Exception {return algorithm.encrypt(data);
}public String decryptData(String encryptedData) throws Exception {return algorithm.decrypt(encryptedData);
}public void setFactory(EncryptionFactory factory) throws Exception {this.factory = factory;this.algorithm = factory.createAlgorithm();
}

}
4. 测试类

// EncryptionTest.java
import java.util.Scanner;

public class EncryptionTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

    try {System.out.println("=== 加密算法系统 ===");System.out.println("1. DES加密算法");System.out.println("2. IDEA加密算法");System.out.print("请选择加密算法(1-2): ");int choice = scanner.nextInt();scanner.nextLine(); // 消费换行符EncryptionFactory factory = null;String algorithmName = "";switch (choice) {case 1:factory = new DESFactory();algorithmName = "DES";break;case 2:factory = new IDEAFactory();algorithmName = "IDEA";break;default:System.out.println("无效选择!");return;}Client client = new Client(factory);while (true) {System.out.println("\n=== " + algorithmName + "加密系统 ===");System.out.println("1. 加密数据");System.out.println("2. 解密数据");System.out.println("3. 切换加密算法");System.out.println("4. 退出");System.out.print("请选择操作(1-4): ");int operation = scanner.nextInt();scanner.nextLine(); // 消费换行符switch (operation) {case 1:System.out.print("请输入要加密的文本: ");String plainText = scanner.nextLine();String encrypted = client.encryptData(plainText);System.out.println("加密结果: " + encrypted);break;case 2:System.out.print("请输入要解密的文本: ");String encryptedText = scanner.nextLine();String decrypted = client.decryptData(encryptedText);System.out.println("解密结果: " + decrypted);break;case 3:System.out.println("\n=== 切换加密算法 ===");System.out.println("1. DES加密算法");System.out.println("2. IDEA加密算法");System.out.print("请选择加密算法(1-2): ");int newChoice = scanner.nextInt();scanner.nextLine();switch (newChoice) {case 1:client.setFactory(new DESFactory());algorithmName = "DES";System.out.println("已切换到DES算法");break;case 2:client.setFactory(new IDEAFactory());algorithmName = "IDEA";System.out.println("已切换到IDEA算法");break;default:System.out.println("无效选择!");}break;case 4:System.out.println("感谢使用!");return;default:System.out.println("无效选择!");}}} catch (Exception e) {System.err.println("错误: " + e.getMessage());e.printStackTrace();} finally {scanner.close();}
}

}
三、使用说明

javac *.java
java EncryptionTest
功能特点:
支持DES和IDEA两种加密算法
可以动态切换加密算法
提供加密和解密功能
使用Base64编码显示加密结果
注意事项:
需要Java 8或更高版本
IDEA算法可能需要额外的加密库支持(如Bouncy Castle)
每次实例化都会生成新的密钥
四、扩展建议
可以添加AES、RSA等其他加密算法
实现密钥的保存和加载功能
添加文件加密/解密功能
实现加密强度配置

http://www.gsyq.cn/news/43347.html

相关文章:

  • java面试八股 redis篇
  • Python + nano-banana API 批量给图片添加文字
  • AI元人文:当一个系统说“我就是一个人”
  • pip install weditor安装出现报错
  • AI大模型应用开发技术架构和技术选型 - 努力-
  • 一个名为 LVGL for Visual Studio 的项目
  • MySQL 基础架构(一):SQL语句的执行之旅
  • 顺序表练习题
  • 关于一种计算递归次数题的思路
  • 前端框架深度解析:Vue 从入门到实战,掌握渐进式开发核心 - 实践
  • 练习上传
  • 重组蛋白表达技术|HEK293细胞蛋白表达|高效重组蛋白生产服务
  • RK3576在智能工程机械中的应用|三屏八摄AI视觉解决方案
  • 做题笔记23
  • 毒盘未转存仅支持在线观看30s
  • AI元人文:理论自省与客观评估
  • 完整教程:《以 Trae 为桥:高效集成豆包 1.6 API 的实践与思考》
  • 从零开始实现简易版Netty(十) MyNetty 通用编解码器解决TCP黏包/拆包问题
  • 【刷题笔记】AT 经典 90 题
  • CF1758E Tick, Tock
  • javabean和pojo的区别
  • 2025北京一对一辅导/补习/培训/家教/网课推荐榜:金博教育领衔,3家优质机构凭个性化服务出圈,适配多元学习需求
  • Typecho Joe 使用第三方插件开启文章侧边导肮目录 - AutocJS
  • 高级程序语言设计个人作业第四次
  • 什么是 Feed 流?
  • preeee - when
  • 调整 Halo2 Joe 主题友情链接页面样式
  • 基于单片机的元胞自动机仿真系统设计 - 详解
  • (鲜花)万宁五子棋 v0.2
  • 2025年海外仓服务最新推荐企业,欧洲海外仓、美国海外仓、亚马逊海外仓、TEMU海外仓、独立站海外仓服务商解析