实验3:工厂方法模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解工厂方法模式的动机,掌握该模式的结构;
2、能够利用工厂方法模式解决实际问题。
[实验任务一]:加密算法
目前常用的加密算法有DES(Data
Encryption Standard)和IDEA(International
Data Encryption Algorithm)国际数据加密算法等,请用工厂方法实现加密算法系统。
实验要求:
1. 画出对应的类图;
![]()
2. 提交该系统的代码,该系统务必是一个可以能够直接使用的系统,查阅资料完成相应加密算法的实现;
Encryption:
public interface Encryption
{
String
encrypt(String text);
String
decrypt(String ciphertext);
}
DES:
public class DES implements Encryption
{
private SecretKey
secretKey;
public DES()
{
try
{
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
secretKey
= keyGenerator.generateKey();
}
catch
(Exception e)
{
e.printStackTrace();
}
}
//DES加密
@Override
public String
encrypt(String text)
{
try
{
Cipher
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[]
encryptedBytes = cipher.doFinal(text.getBytes(StandardCharsets.UTF_8));
return
Base64.getEncoder().encodeToString(encryptedBytes);
}
catch
(Exception e)
{
e.printStackTrace();
return
null;
}
}
//DES解密
@Override
public String
decrypt(String ciphertext)
{
try
{
Cipher
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[]
decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(ciphertext));
return
new String(decryptedBytes, StandardCharsets.UTF_8);
}
catch
(Exception e)
{
e.printStackTrace();
return
null;
}
}
}
IDEA:
public class IDEA implements Encryption
{
private SecretKey
secretKey;
static
{
// 添加Bouncy Castle提供程序
Security.addProvider(new BouncyCastleProvider());
}
public IDEA()
{
try
{
KeyGenerator
keyGenerator = KeyGenerator.getInstance("IDEA", "BC");
secretKey
= keyGenerator.generateKey();
}
catch
(Exception e)
{
e.printStackTrace();
}
}
@Override
public String
encrypt(String text)
{
try
{
// 创建一个IDEA加密器
Cipher
cipher = Cipher.getInstance("IDEA/ECB/PKCS5Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[]
encryptedBytes = cipher.doFinal(text.getBytes());
return
Base64.getEncoder().encodeToString(encryptedBytes);
}
catch
(Exception e)
{
e.printStackTrace();
return
null;
}
}
@Override
public String
decrypt(String ciphertext)
{
try
{
Cipher
cipher = Cipher.getInstance("IDEA/ECB/PKCS5Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[]
encryptedBytes = Base64.getDecoder().decode(ciphertext);
return
new String(cipher.doFinal(encryptedBytes), StandardCharsets.UTF_8);
}
catch
(Exception e)
{
e.printStackTrace();
return
null;
}
}
}
EncryptionFactory
public
interface EncryptionFactory
{
Encryption createEncryption();
}
DESFactory:
public class DESFactory implements EncryptionFactory
{
@Override
public Encryption
createEncryption()
{
return new
DES();
}
}
IDEAFactory:
public class IDEAFactory implements EncryptionFactory
{
@Override
public Encryption
createEncryption()
{
return new
IDEA();
}
}
FactoryMethod:
import java.util.Scanner;
public class FactoryMethod
{
public static
void main(String[] args)
{
Scanner sc =
new Scanner(System.in);
System.out.println("请输入要加密的文本:");
String text =
sc.next();
EncryptionFactory factory = null;
System.out.println("请输入加密算法编号:\n" +
"1、DES加密 2、IDEA加密" 3、退出);
int choose =
sc.nextInt();
if (choose ==
1)
{
//DES加密
factory =
new DESFactory();
System.out.println("DES加密");
} else if
(choose == 2)
{
//IDEA加密
factory =
new IDEAFactory();
System.out.println("IDEA加密");
}
Encryption
encryptionAlgorithm = factory.createEncryption();
String
encryptedText = encryptionAlgorithm.encrypt(text);
String
decryptedText = encryptionAlgorithm.decrypt(encryptedText);
System.out.println("原文本:" + text);
System.out.println("加密后:" + encryptedText);
System.out.println("解密后:" + decryptedText);
}
}
运行结果:
![]()
posted @
2024-11-05 21:13
连师傅只会helloword
阅读(
11)
评论()
收藏
举报