10.15日报
今天进行了mes系统的开发的学习,了解了mes系统开发的主要步骤,要明确目标,弄清楚需求分析报告的要求,并且设置对应的数据库,并且了解了如何绘制上下文图。
下午完成软件设计实验以下为实验内容
实验3:工厂方法模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解工厂方法模式的动机,掌握该模式的结构;
2、能够利用工厂方法模式解决实际问题。
[实验任务一]:加密算法
目前常用的加密算法有DES(Data Encryption Standard)和IDEA(International Data Encryption Algorithm)国际数据加密算法等,请用工厂方法实现加密算法系统。
实验要求:
1. 画出对应的类图;
2.提交该系统的代码,该系统务必是一个可以能够直接使用的系统,查阅资料完成相应加密算法的实现;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;
public class DESEncryption {
// 生成DES密钥
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
keyGenerator.init(56, SecureRandom.getInstanceStrong());
return keyGenerator.generateKey();
}
// DES加密
public static String encrypt(String data, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedData = cipher.doFinal(data.getBytes("UTF-8"));
return Base64.getEncoder().encodeToString(encryptedData);
}
// DES解密
public static String decrypt(String encryptedData, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] data = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
return new String(data);
}
public static void main(String[] args) {
try {
// 生成密钥
SecretKey key = generateKey();
String originalText = "Hello, World!";
// 加密
String encryptedText = encrypt(originalText, key);
System.out.println("Encrypted Text: " + encryptedText);
// 解密
String decryptedText = decrypt(encryptedText, key);
System.out.println("Decrypted Text: " + decryptedText);
} catch (Exception e) {
e.printStackTrace();
}
}
}

浙公网安备 33010602011771号