JAVA练习:密码的加密与解密
题目:实现四位数字密码的加密与解密,加密方式为:每个数字+5然后%10,最后将密码反转。
1.加密实现思路:
2.加密成功截图:
3.加密实现代码:
public class sszjm {
public static void main(String[] args){
int[] mm=new int[4];
System.out.print("请输入四位密码:");
for (int i = 0; i < mm.length; i++) {
Scanner sc=new Scanner(System.in);
mm[i]=sc.nextInt();
mm[i]+=5;
mm[i]%=10;
}
System.out.print("加密后的结果为:");
for (int i = mm.length-1;i >= 0; i--) {
System.out.print(mm[i]+" ");
}
}
}
4.解密实现思路:
5.解密成功截图:6.解密实现代码:
public class jm {
public static void main(String[] args){
int[] mm=new int[4];
System.out.print("请输入需要解密的密码:");
for (int i = 0; i < mm.length; i++) {
Scanner sc=new Scanner(System.in);
mm[i]=sc.nextInt();
}
System.out.print("原密码为:");
for (int i = mm.length-1; i >=0 ; i--) {
if(mm[i]>=5){
mm[i]-=5;
}else if(mm[i]<5){
mm[i]+=5;
}
System.out.print(mm[i]+" ");
}
/*for (int i = 0; i < mm.length; i++) {
System.out.print(mm[i]+" ");
}*/
}
}