软件设计 石家庄铁道大学信息学院
实验3:工厂方法模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解工厂方法模式的动机,掌握该模式的结构;
2、能够利用工厂方法模式解决实际问题。
[实验任务一]:加密算法
目前常用的加密算法有DES(Data Encryption Standard)和IDEA(International Data Encryption Algorithm)国际数据加密算法等,请用工厂方法实现加密算法系统。
实验要求:
1. 画出对应的类图;
2. 提交该系统的代码,该系统务必是一个可以能够直接使用的系统,查阅资料完成相应加密算法的实现;
代码:
Client:
package factoryMethod;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
System.out.println("DES:1 IDEA:2");
System.out.print("请输入要加密类型:");
Scanner sc = new Scanner(System.in);
String type = sc.nextLine();
System.out.print("请输入要加密数据:");
String context = sc.nextLine();
if ("1".equals(type)){
Method method;
MethodFactory methodFactory = new DESFactory();
method = methodFactory.UseMethod();
method.Use(context);
}else if ("2".equals(type))
{
Method method;
MethodFactory methodFactory = new IDEAFactory();
method = methodFactory.UseMethod();
method.Use(context);
}
}
}
DES:
package factoryMethod;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class DES implements Method{
@Override
public void Use(String context) {
String data = context;
String key = "12345678"; // 密钥,长度必须是8个字符
// 加密
String encryptedData = null;
try {
encryptedData = encrypt(data, key);
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("加密后的数据: " + encryptedData);
// 解密
String decryptedData = null;
try {
decryptedData = decrypt(encryptedData, key);
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("解密后的数据: " + decryptedData);
}
/* public static void main(String[] args) throws Exception {
String data = "这是一个待加密的字符串";
String key = "12345678"; // 密钥,长度必须是8个字符
// 加密
String encryptedData = encrypt(data, key);
System.out.println("加密后的数据: " + encryptedData);
// 解密
String decryptedData = decrypt(encryptedData, key);
System.out.println("解密后的数据: " + decryptedData);
}*/
public static String encrypt(String data, String key) throws Exception {
if (key == null || key.length() != 8) {
throw new IllegalArgumentException("密钥长度必须是8个字符");
}
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedData, String key) throws Exception {
if (key == null || key.length() != 8) {
throw new IllegalArgumentException("密钥长度必须是8个字符");
}
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
}
DESFactory:
package factoryMethod;
public class DESFactory implements MethodFactory{
@Override
public Method UseMethod() {
System.out.println("进行DES加密");
return new DES();
}
}
IDEA:
package factoryMethod;
import javax.crypto.*;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class IDEA implements Method{
@Override
public void Use(String context) {
// 生成密钥
KeyGenerator keyGenerator = null;
try {
keyGenerator = KeyGenerator.getInstance("IDEA");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
// 加密
String plainText = context;
Cipher cipher = null;
try {
cipher = Cipher.getInstance("IDEA/ECB/PKCS5Padding");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (NoSuchPaddingException e) {
throw new RuntimeException(e);
}
try {
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
}
byte[] encryptedBytes = new byte[0];
try {
encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
}
String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
System.out.println("加密后的文本: " + encryptedText);
// 解密
try {
cipher.init(Cipher.DECRYPT_MODE, secretKey);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
}
byte[] decryptedBytes = new byte[0];
try {
decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
}
String decryptedText = new String(decryptedBytes, StandardCharsets.UTF_8);
System.out.println("解密后的文本: " + decryptedText);
}
}
IDEAFactory:
package factoryMethod;
public class IDEAFactory implements MethodFactory{
@Override
public Method UseMethod() {
System.out.println("进行IDEA加密");
return new IDEA();
}
}
Method:
package factoryMethod;
public interface Method {
public void Use(String context);
}
MethodFactory:
package factoryMethod;
public interface MethodFactory {
public Method UseMethod();
}
运行结果:


浙公网安备 33010602011771号