任意长度密码(密码可输⼊⼤⼩写字母和数字)的加密与解密密码的加密:
输⼊⼀串任意长度的密码可包含⼤⼩写字母和数字
加密规则如下:
每位数字都加上5,然后⽤和除以10的余数代替该数字,再将第⼀位和最后位交换,第⼆位和倒数第⼆位交换....输⼊⼀个未加密数据,进⾏加密,并解密。
package java_exercise;
/*
密码的加密:
输⼊⼀串任意长度的密码可包含⼤⼩写字母和数字
加密规则如下:
每位数字都加上5,然后⽤和除以10的余数代替该数字,
再将第⼀位和最后位交换,第⼆位和倒数第⼆位交换....
输⼊⼀个未加密数据,进⾏加密,并解密。
*/
import java.util.Arrays;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str;
int temp;
System.out.println("输⼊密码:");
();
int n=str.length();
//字符串转换成字符,存⼊数组
int [] arr = new int[n];
for (int i = 0; i <arr.length ; i++) {
arr[i] = str.charAt(i)-48;
}
//System.out.String(arr));
//加密:每位数字都加上5,然后⽤和除以10的余数代替该数字
for (int i = 0; i <arr.length ; i++) {
arr[i] +=5;
//arr[i]=arr[i]%10;
}
//加密:第⼀位与倒数第⼀位交换,第⼆位与倒数第⼆位交换...
for (int i = 0; i <(arr.length)/2 ; i++) {
temp=arr[i];
arr[i]=arr[arr.length-1-i];
arr[arr.length-1-i]=temp;
}
System.out.println("加密:");
System.out.String(arr));
//解密
//将位置换回
for (int i = 0; i <(arr.length)/2 ; i++) {
temp=arr[i];
arr[i]=arr[arr.length-1-i];
arr[arr.length-1-i]=temp;
}
//
for (int i = 0; i <arr.length ; i++) {
// arr[i]=arr[i]+10; // * ⼀个数除以10的余数代替该数字返回应该+10 再%10 ⽽不是*10
// arr[i]=arr[i]+10; // * ⼀个数除以10的余数代替该数字返回应该+10 再%10 ⽽不是*10
arr[i] -=5;
//arr[i]=arr[i]%10;
}
System.out.println("解密:");
for (int i = 0; i < arr.length; i++) {
char a= (char) arr[i];
switch(a){ //switch/case去区分字母和数字
case 74:
System.out.print('z'+" ");break;
case 73:
System.out.print('y'+" ");break;
case 72:
System.out.print('x'+" ");break;
case 71:
System.out.print('w'+" ");break;
case 70:
System.out.print('v'+" ");break;
case 69:
System.out.print('u'+" ");break;
case 68:
System.out.print('t'+" ");break;
case 67:
System.out.print('s'+" ");break;
case 66:
System.out.print('r'+" ");break;
case 65:
System.out.print('q'+" ");break;
case 64:
System.out.print('p'+" ");break;
case 63:
System.out.print('o'+" ");break;
case 62:
System.out.print('n'+" ");break;
case 61:
System.out.print('m'+" ");break;
case 60:
System.out.print('l'+" ");break;
case 59:
System.out.print('k'+" ");break;
case 58:
System.out.print('j'+" ");break;
case 57:
System.out.print('i'+" ");break;
case 56:
System.out.print('h'+" ");break;
case 55:
System.out.print('g'+" ");break;
case 54:
System.out.print('f'+" ");break;
case 53:
System.out.print('e'+" ");break;
case 52:
System.out.print('d'+" ");break;
case 51:
System.out.print('c'+" ");break;
case 50:
System.out.print('b'+" ");break;
case 49:
System.out.print('a'+" ");break;
case 42:
System.out.print('Z'+" ");break;
case 41:
case 41:
System.out.print('Y'+" ");break;
case 40:
System.out.print('X'+" ");break;
case 39:
System.out.print('W'+" ");break;
case 38:
System.out.print('V'+" ");break;
case 37:
System.out.print('U'+" ");break;
case 36:
System.out.print('T'+" ");break;
case 35:
System.out.print('S'+" ");break;
case 34:
System.out.print('R'+" ");break;
case 33:
System.out.print('Q'+" ");break;
case 32:
System.out.print('P'+" ");break;
case 31:
System.out.print('O'+" ");break;
case 30:
System.out.print('N'+" ");break;
case 29:
System.out.print('M'+" ");break;
case 28:
System.out.print('L'+" ");break;
case 27:
System.out.print('K'+" ");break;
case 26:
System.out.print('J'+" ");break;
case 25:
System.out.print('I'+" ");break;
case 24:
System.out.print('H'+" ");break;
case 23:
System.out.print('G'+" ");break;
case 22:
System.out.print('F'+" ");break;
case 21:
System.out.print('E'+" ");break;
case 20:
System.out.print('D'+" ");break;倒着输入银行卡密码
case 19:
System.out.print('C'+" ");break;
case 18:
System.out.print('B'+" ");break;
case 17:
System.out.print('A'+" ");break;
default:
System.out.print(arr[i]+" ");
break;
}
}
}
}
因为学艺不精,不知道怎么将字母与数字在数组中分离开,所以采⽤了switch/case的⽅法,看着⽐较⿇烦。希望⼤佬们能指点⼀下,能简化⼀下我的代码。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论