24.11.16
实验3:工厂方法模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解工厂方法模式的动机,掌握该模式的结构;
2、能够利用工厂方法模式解决实际问题。
[实验任务一]:加密算法
目前常用的加密算法有DES(Data Encryption Standard)和IDEA(International Data Encryption Algorithm)国际数据加密算法等,请用工厂方法实现加密算法系统。
实验要求:
1.画出对应的类图;
2.提交该系统的代码,该系统务必是一个可以能够直接使用的系统,查阅资料完成相应加密算法的实现;
3.注意编程规范。
类图如下:

代码如下:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.SecureRandom;
import java.util.Base64;
// 定义加密算法接口
interface EncryptionAlgorithm {
String encrypt(String data) throws Exception;
}
// 实现 DES 加密算法
class DESEncryption implements EncryptionAlgorithm {
private SecretKey secretKey;
public DESEncryption() throws Exception {
// 生成 DES 密钥
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
keyGen.init(56); // DES 密钥长度为 56 位
this.secretKey = keyGen.generateKey();
}
@Override
public String encrypt(String data) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedData); // 使用 Base64 编码输出密文
}
}
// 实现 IDEA 加密算法
class IDEAEncryption implements EncryptionAlgorithm {
private SecretKey secretKey;
public IDEAEncryption() throws Exception {
// 这里引入了 Bouncy Castle库,注册了它的算法实现
KeyGenerator keyGen = KeyGenerator.getInstance("IDEA", "BC"); // 需要 Bouncy Castle 支持
keyGen.init(128); // IDEA 密钥长度为 128 位
this.secretKey = keyGen.generateKey();
}
@Override
public String encrypt(String data) throws Exception {
Cipher cipher = Cipher.getInstance("IDEA", "BC");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedData);
}
}
// 加密算法工厂接口
interface EncryptionFactory {
EncryptionAlgorithm createEncryption() throws Exception;
}
// DES 加密工厂
class DESFactory implements EncryptionFactory {
@Override
public EncryptionAlgorithm createEncryption() throws Exception {
return new DESEncryption();
}
}
// IDEA 加密工厂
class IDEAFactory implements EncryptionFactory {
@Override
public EncryptionAlgorithm createEncryption() throws Exception {
return new IDEAEncryption();
}
}
// 客户端类
class Client {
private EncryptionAlgorithm encryptionAlgorithm;
public Client(EncryptionFactory factory) throws Exception {
encryptionAlgorithm = factory.createEncryption();
}
public void encryptData(String data) throws Exception {
String encryptedData = encryptionAlgorithm.encrypt(data);
System.out.println("Encrypted Data: " + encryptedData);
}
public static void main(String[] args) {
try {
// 使用 DES 加密
EncryptionFactory desFactory = new DESFactory();
Client desClient = new Client(desFactory);
desClient.encryptData("Hello World using DES");
// 使用 IDEA 加密 (需要 Bouncy Castle 支持)
EncryptionFactory ideaFactory = new IDEAFactory();
Client ideaClient = new Client(ideaFactory);
ideaClient.encryptData("Hello World using IDEA");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Pom.xml里加入以下依赖:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.70</version>
</dependency>
浙公网安备 33010602011771号