标签:Arrays 方法 algorithmType 模式 工厂 org byte data public
目前常用的加密算法有DES(Data Encryption Standard)和IDEA(International Data Encryption Algorithm)国际数据加密算法等,请用工厂方法实现加密算法系统。
实验要求:
1.画出对应的类图;
2.提交该系统的代码,该系统务必是一个可以能够直接使用的系统,查阅资料完成相应加密算法的实现;
3.注意编程规范。
1.类图
2. 系统代码
package org.example;
public interface EncryptionAlgorithm {
byte[] encrypt(byte[] data);
}
package org.example;
import java.util.Arrays;
public class DESAlgorithm implements EncryptionAlgorithm {
@Override
public byte[] encrypt(byte[] data) {
// 实现DES加密逻辑
return Arrays.copyOfRange(data, 0, data.length);
}
}
package org.example;
import java.util.Arrays;
public class IDEAAlgorithm implements EncryptionAlgorithm {
@Override
public byte[] encrypt(byte[] data) {
// 实现IDEA加密逻辑
return Arrays.copyOfRange(data, 0, data.length);
}
}
package org.example;
public class EncryptionAlgorithmFactory {
public static EncryptionAlgorithm getEncryptionAlgorithm(String algorithmType) {
if ("DES".equalsIgnoreCase(algorithmType)) {
return new DESAlgorithm();
} else if ("IDEA".equalsIgnoreCase(algorithmType)) {
return new IDEAAlgorithm();
} else {
throw new IllegalArgumentException("Unsupported algorithm type: " + algorithmType);
}
}
}
package org.example;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String algorithmType = "DES"; // 或者 "IDEA"
EncryptionAlgorithm encryptionAlgorithm = EncryptionAlgorithmFactory.getEncryptionAlgorithm(algorithmType);
byte[] data = "Hello, World!".getBytes();
byte[] encryptedData = encryptionAlgorithm.encrypt(data);
System.out.println("Encrypted Data: " + Arrays.toString(encryptedData));
}
}
标签:Arrays,
方法,
algorithmType,
模式,
工厂,
org,
byte,
data,
public
From: https://www.cnblogs.com/pinganxile/p/18639500