public abstract class EncryptionModule {
String str;
int i;
public void encryption(String str) {
this.str=str;
}
public void key(int i) {
this.i=i;
}
public abstract void caesarCall();
public abstract void modCall();
public abstract void show();
}
public class Caesar {
//凯撒加密
public String doCaesarEncrypt(int key,String ps) {
String es="";
for(int i=0;i<ps.length();i++) {
char c=ps.charAt(i);
if(c>=‘a‘ && c<=‘z‘) {
c+=key%26;
if(c>‘z‘)
c-=26;
if(c<‘a‘)
c+=26;
}
if(c>=‘A‘ && c<=‘Z‘) {
c+=key%26;
if(c>‘Z‘)
c-=26;
if(c<‘A‘)
c+=26;
}
es+=c;
}
return es;
}
}
public class Mod {
//取模加密
public String doModEncrypt(int key,String ps) {
String es="";
for(int i=0;i<ps.length();i++) {
String c=String.valueOf(ps.charAt(i)%key);
es+=c;
}
return es;
}
}
import java.util.Scanner;
public class Adapter extends EncryptionModule{
private Caesar caesar;
private Mod mod;
public Adapter() {
caesar=new Caesar();
mod=new Mod();
}
public void caesarCall() {
System.out.println("调用凯撒加密");
String s1=caesar.doCaesarEncrypt(i, str);
System.out.println("凯撒加密的结果为:"+s1);
}
public void modCall() {
System.out.println("调用取模加密");
String s2=mod.doModEncrypt(i, str);
System.out.println("取模加密的结果为:"+s2);
}
public void show() {
EncryptionModule en=new Adapter();
System.out.println("---加密方式有凯撒加密和取模加密---");
Scanner sc=new Scanner(System.in);
System.out.println("请输入要加密的明文:");
String str1=sc.nextLine();
en.encryption(str1);
System.out.println("请输入要选择的加密方式:");
String str2=sc.nextLine();
if(str2.equalsIgnoreCase("凯撒加密")) {
System.out.println("请输入密钥:");
int i=sc.nextInt();
en.key(i);
en.caesarCall();
}
if(str2.equalsIgnoreCase("取模加密")) {
System.out.println("请输入密钥:");
int i=sc.nextInt();
en.key(i);
en.modCall();
}
sc.close();
}
}
public class Client {
public static void main(String[] args) {
EncryptionModule en=new Adapter();
en.show();
}
}
原文:https://www.cnblogs.com/xuhuan0/p/13051642.html