题目
密码加密
基础作业
张三的加密方式是这样的:
对于每个字母和数字, 按字母表/数字顺序往后挪5位
符号不变
如密码: AaZz09!!!
加密后: FfEe54!!!
-----------------------------------------------------------
进阶作业
张三的加密方式是这样的:
对位于每个字母, 按字母表往后挪x
x为该字母在字母表中的位置(如a 加密后为b, b加密后为d, y加密后为x, z加密后为z)
对于每个数字, 取其平方的个位(如4 加密后为6, 8 加密后为4)
符号不变
如密码: AaZz09!!!
加密后: BbZz01!!!
分析
大写字母 A-Z:65-90,
小写字母 a-z:97-122,
数字 0-9:48-57
细节:每个字符列表自成闭环
代码
public class Homework0618 {
public static void main(String[] args) {
Scanner sc = null;
int num = 0;
while (true){
System.out.println("\n************************************************************");
System.out.println("1、标准作业\t\t2、进阶作业\t\t3、退出");
System.out.print("请输入对应编号,查看对应答案: ");
sc = new Scanner(System.in);
num = sc.nextInt();
if (num == 1){
System.out.println("\n******************标准作业******************");
basics();
}else if (num == 2){
System.out.println("\n******************进阶作业******************");
advanced();
}else if (num == 3){
System.out.println("\n******************系统退出******************");
break;
}
else{
System.out.println("输入错误,请重新输入");
}
}
}
/**
* 方法--进阶作业
*/
public static void advanced() {
String psw = input();
String encryption = method2(psw);
System.out.println( "\n初始密码: " + psw + "\t\t加密后为: " + encryption);
if( judge() == true ){
advanced();
}
}
public static String method2(String psw) {
char[] ch = psw.toCharArray();
char temp = ' ';
int num = 0;
for(int i = 0; i < ch.length; i++){
if (65 <= ch[i] && ch[i] <= 90){
temp = (char)(ch[i] - 64 + ch[i]);
ch[i] = temp > 90 ? (temp -= 26) : temp;
}else if(97 <= ch[i] && ch[i] <= 122) {
temp = (char)(ch[i] - 96 + ch[i]);
ch[i] = temp > 122 ? (temp -= 26) : temp;
}else if (48 <= ch[i] && ch[i] <= 57){
num = (int) Math.pow(ch[i] - 48, 2) % 10;
ch[i] = (char) (num + 48);
}else {
ch[i] = ch[i];
}
}
String encryption = String.valueOf(ch);
return encryption;
}
/**
* 方法--标准作业
*/
public static void basics() {
String psw = input();
String encryption = method1(psw);
System.out.println( "\n初始密码: " + psw + "\t\t加密后为: " + encryption);
if( judge() == true ){
basics();
}
}
public static String method1(String psw) {
char[] ch = psw.toCharArray();
char temp = ' ';
for(int i = 0; i < ch.length; i++){
temp = ch[i];
if (65 <= temp && temp <= 90){
ch[i] = temp > 85 ? temp -= 21 : (temp += 5);
}else if(97 <= temp && temp <= 122) {
ch[i] = temp > 117 ? temp -= 21 : (temp += 5);
}else if (48 <= temp && temp <= 57){
ch[i] = temp > 52 ? temp -= 5 : (temp += 5);
}else {
ch[i] = ch[i];
}
}
String encryption = String.valueOf(ch);
return encryption;
}
/**
* 是否继续计算
*/
public static boolean judge() {
System.out.print("\n是否继续?输入y继续计算,其他则退出: ");
Scanner sc = new Scanner(System.in);
if ("y".equalsIgnoreCase(sc.next())){
System.out.print("继续判断,");
return true;
}else{
System.out.println("退出计算!");
return false;
}
}
public static String input() {
Scanner sc = null;
String psw = null;
while(true){
sc = new Scanner(System.in);
System.out.print("请输入密码:");
psw = sc.nextLine().trim();
if (psw.length() != 0){
return psw;
}else{
System.out.print("请输入非空密码: ");
}
}
}
}
运行结果
************************************************************
1、标准作业 2、进阶作业 3、退出
请输入对应编号,查看对应答案: 1
******************标准作业******************
请输入密码:AaZz09!!!
初始密码: AaZz09!!! 加密后为: FfEe54!!!
是否继续?输入y继续计算,其他则退出: n
退出计算!
************************************************************
1、标准作业 2、进阶作业 3、退出
请输入对应编号,查看对应答案: 2
******************进阶作业******************
请输入密码:AaZz09!!!
初始密码: AaZz09!!! 加密后为: BbZz01!!!
是否继续?输入y继续计算,其他则退出: n
退出计算!
************************************************************
1、标准作业 2、进阶作业 3、退出
请输入对应编号,查看对应答案: 3
******************系统退出******************