Sunny 软件公司欲开发一个数据加密模块,可以对字符器进行加密。最简单的加密算法通过对字母进行移位来实现,同时还提供了稍复杂的逆向输出加密和更为高级的求模加密。用户先使用最简单的加密算法对字符串进行加密,如果觉得还不够,可以对加密后的结果使用其他加密算法进行二次加密,当然也可以进行第 3 次加密。试使用装饰模式设计该多重加密系统。
一、类结构图
抽象构件类:设计为名称为 StringEncryptor 抽象类,包含抽象方法 encrypt(String srcStr);
抽象装饰类:设计为抽象构件类 StringEncryptor 的具体子类,设计为名称为 EncryptorDecorator 的具体类,面向抽象构件类编程,持有一个抽象构件类 StringEncryptor 的引用,并实现抽象构件类的抽象方法 encrypt(String srcStr);
具体构件类:设计为抽象构件类的子类,设计为名称为 ShiftStringEncryptor 的具体类,实现抽象构件类的抽象方法 encrypt(String srcStr);
具体装饰类:设计为抽象装饰类 EncryptorDecorator 的具体子类,包括具体类 ReversalEncryptorDecorator、具体类 ModStringEncryptorDecorator,分别覆盖实现抽象装饰类的方法 encrypt(String srcStr)。
二、典型实现代码
字符串加密抽象类:抽象构件类
// 字符串加密抽象类:抽象构件类
public abstract class StringEncryptor {
public abstract String encrypt(String strStr);
}
复制代码
字符串加密装饰类:抽象装饰类
// 字符串加密装饰类:抽象装饰类
public class EncryptorDecorator extends StringEncryptor {
private StringEncryptor stringEncryptor;
public EncryptorDecorator(StringEncryptor stringEncrypt) {
this.stringEncryptor = stringEncrypt;
}
@Override
public String encrypt(String strStr) {
return stringEncryptor.encrypt(strStr);
}
}
复制代码
字符串移位加密类:具体构件类
// 字符串移位加密类:具体构件类
public class ShiftStringEncryptor extends StringEncryptor {
@Override
public String encrypt(String strStr) {
int rightShiftBit = 5;
int leftShiftBit = 26 - rightShiftBit;
StringBuilder result = new StringBuilder();
for (int i = 0; i < strStr.length(); i++) {
char character = strStr.charAt(i);
if (character >= 'a' && character <= 'u') {
character = (char) (character + rightShiftBit);
result.append(character);
continue;
}
if (character >= 'v' && character <= 'z') {
character = (char) (character - leftShiftBit);
result.append(character);
continue;
}
if (character >= 'A' && character <= 'U') {
character = (char) (character + rightShiftBit);
result.append(character);
continue;
}
if (character >= 'v' && character <= 'Z') {
character = (char) (character - leftShiftBit);
result.append(character);
continue;
}
}
return result.toString();
}
}
复制代码
字符串逆向加密类:具体装饰类
// 字符串逆向加密类:具体装饰类
public class ReversalEncryptorDecorator extends EncryptorDecorator {
public ReversalEncryptorDecorator(StringEncryptor stringEncrypt) {
super(stringEncrypt);
}
@Override
public String encrypt(String strStr) {
String encryptStr = super.encrypt(strStr);
StringBuilder result = new StringBuilder(encryptStr);
return result.reverse().toString();
}
}
复制代码
字符串取模加密类:具体装饰类
// 字符串取模加密类:具体装饰类
public class ModEncryptorDecorator extends EncryptorDecorator {
public ModEncryptorDecorator(StringEncryptor stringEncrypt) {
super(stringEncrypt);
}
@Override
public String encrypt(String strStr) {
String encryptStr = super.encrypt(strStr);
StringBuilder result = new StringBuilder();
for (int i = 0; i < encryptStr.length(); i++) {
char c = encryptStr.charAt(i);
int mod = (c - '0') % 10;
char character = (char)('a' + mod);
result.append(character);
}
return result.toString();
}
}
复制代码
客户端代码:
public class Client {
public static void main(String[] args) {
StringEncryptor stringEncryptor, modStringEncryptor, reversalStringEncryptor;
stringEncryptor = new ShiftStringEncryptor();
String encryptStr = stringEncryptor.encrypt("secretPassword");
System.out.println(encryptStr);
reversalStringEncryptor = new ReversalEncryptorDecorator(stringEncryptor);
encryptStr = reversalStringEncryptor.encrypt("secretPassword");
System.out.println(encryptStr);
modStringEncryptor = new ModEncryptorDecorator(reversalStringEncryptor);
encryptStr = modStringEncryptor.encrypt("secretPassword");
System.out.println(encryptStr);
}
}
复制代码
编译并运行程序,输出如下结果:
xjhwjyUfxxbtwi
iwtbxxfUyjwhjx
hbiaccehdibgic
复制代码
评论