主程序
public class Main {
private static Scanner in = new Scanner ( System. in) ;
public static void main ( String[ ] args) {
while ( true ) {
System. out. println ( "实现的密码算法" ) ;
System. out. println ( "\t1.替代算法" ) ;
System. out. println ( "\t2.置换算法" ) ;
System. out. println ( "\t3.退出" ) ;
System. out. print ( "请输入需要的密码算法:" ) ;
int choice = in. nextInt ( ) ;
switch ( choice) {
case 1 :
chooseFun ( new SubstitutePassword ( ) ) ;
break ;
case 2 :
chooseFun ( new ReplacementPassword ( ) ) ;
break ;
case 3 :
in. close ( ) ;
return ;
default :
System. out. println ( "选择有误~请重新输入..." ) ;
}
}
}
private static void chooseFun ( AbstractPassword abstractPassword) {
System. out. println ( "密码算法功能:" ) ;
System. out. println ( "\t1.加密" ) ;
System. out. println ( "\t2.解密" ) ;
System. out. print ( "请输入需要的密码算法功能:" ) ;
int fun = in. nextInt ( ) ;
if ( fun == 1 ) {
System. out. print ( "请输入明文:" ) ;
in. nextLine ( ) ;
String plaintext = in. nextLine ( ) ;
System. out. println ( "明文加密后的密文:" + abstractPassword. encryption ( plaintext) ) ;
}
if ( fun == 2 ) {
System. out. print ( "请输入密文:" ) ;
in. nextLine ( ) ;
String ciphertext = in. nextLine ( ) ;
System. out. println ( "密文解密后的明文:" + abstractPassword. decryption ( ciphertext) ) ;
}
}
}
密码加密解密接口
public interface FindPassword {
String encryption ( String plaintext) ;
String decryption ( String ciphertext) ;
}
密码加密解密抽象类
public abstract class AbstractPassword implements FindPassword {
public static String key;
public static int k;