2023/9/24 每日总结
今天完成了设计模式实验三
实验3:工厂方法模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解工厂方法模式的动机,掌握该模式的结构;
2、能够利用工厂方法模式解决实际问题。
[实验任务一]:加密算法
目前常用的加密算法有DES(Data Encryption Standard)和IDEA(International Data Encryption Algorithm)国际数据加密算法等,请用工厂方法实现加密算法系统。
类图
Encryption.java
package org.test3; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; public interface Encryption { void encrytion(String s) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException; }
Factory.java
package org.test3; public interface Factory { Encryption produce(); }
IDEA.java
package org.test3; import org.bouncycastle.crypto.BlockCipher; import org.bouncycastle.crypto.BufferedBlockCipher; import org.bouncycastle.crypto.engines.IDEAEngine; import org.bouncycastle.crypto.modes.CFBBlockCipher; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.ParametersWithIV; import org.bouncycastle.util.encoders.Hex; import java.nio.charset.StandardCharsets; import java.security.SecureRandom; public class IDEA implements Encryption { private String plainText="helloworld"; @Override public void encrytion(String s) { try { this.plainText=s; // 创建IDEA加密引擎 BlockCipher engine = new IDEAEngine(); // 初始化加密模式(这里使用CFB模式,也可以选择其他模式) BufferedBlockCipher cipher = new BufferedBlockCipher(new CFBBlockCipher(engine, engine.getBlockSize() * 8)); // 创建随机生成的密钥 byte[] keyBytes = new byte[16]; new SecureRandom().nextBytes(keyBytes); KeyParameter key = new KeyParameter(keyBytes); // 初始化加密器 cipher.init(true, new ParametersWithIV(key, new byte[engine.getBlockSize()])); // 明文 byte[] input = plainText.getBytes(StandardCharsets.UTF_8); // 加密 byte[] output = new byte[cipher.getOutputSize(input.length)]; int bytesProcessed = cipher.processBytes(input, 0, input.length, output, 0); cipher.doFinal(output, bytesProcessed); // 将加密后的字节数组转换为十六进制字符串 String encryptedText = new String(Hex.encode(output), StandardCharsets.UTF_8); System.out.println("加密后的文本: " + encryptedText); // 解密 cipher.init(false, new ParametersWithIV(key, new byte[engine.getBlockSize()])); byte[] decryptedOutput = new byte[cipher.getOutputSize(output.length)]; int decryptedBytesProcessed = cipher.processBytes(output, 0, output.length, decryptedOutput, 0); cipher.doFinal(decryptedOutput, decryptedBytesProcessed); // 明文 String decryptedText = new String(decryptedOutput, StandardCharsets.UTF_8); System.out.println("解密后的文本: " + decryptedText); } catch (Exception e) { e.printStackTrace(); } } }
IDEAFactory.java
package org.test3; public class IDEAFactory implements Factory{ @Override public Encryption produce() { System.out.println("使用IDEA算法"); return new IDEA(); } }
DES.java
package org.test3; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Base64; public class DES implements Encryption{ private String plainText = "Hello, World!"; @Override public void encrytion(String s) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { this.plainText=s; // 生成DES密钥 KeyGenerator keyGenerator = KeyGenerator.getInstance("DES"); SecretKey secretKey = keyGenerator.generateKey(); // 明文 byte[] plainTextBytes = plainText.getBytes(StandardCharsets.UTF_8); // 创建DES加密器 Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); // 初始化加密器 cipher.init(Cipher.ENCRYPT_MODE, secretKey); // 加密 byte[] encryptedBytes = cipher.doFinal(plainTextBytes); // 将加密后的字节数组转换为Base64字符串以便存储或传输 String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes); System.out.println("加密后的文本: " + encryptedText); // 初始化解密器 cipher.init(Cipher.DECRYPT_MODE, secretKey); // 解密 byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText)); String decryptedText = new String(decryptedBytes, StandardCharsets.UTF_8); System.out.println("解密后的文本: " + decryptedText); } }
DESFactory.java
package org.test3; public class DESFactory implements Factory{ @Override public Encryption produce() { System.out.println("使用DES算法"); return new DES(); } }
Client.java
package org.test3; import java.util.Scanner; public class Client { public static void main(String[] args) { System.out.println("请你输入选用的加密算法1.DES 2.IDEA"); Scanner scanner=new Scanner(System.in); String next = scanner.next(); try{ if(next.equals("1")){ Factory factory = new DESFactory(); Encryption produce = factory.produce(); produce.encrytion("qiaoqian111"); } if(next.equals("2")){ Factory factory = new IDEAFactory(); Encryption produce = factory.produce(); produce.encrytion("qiaoqian111"); } }catch (Exception e){ e.printStackTrace(); } } }