华为在线编程系列-字符串加密解密

  • 题目:

    • 题目描述:
      1、对输入的字符串进行加解密,并输出。
      2加密方法为:
      当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;
      当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;
      其他字符不做变化。
      3、解密方法为加密的逆过程。

    • 输入输出描述:

      输入一串要加密的密码
      输入一串加过密的密码
      
      输出加密后的字符
      输出解密后的字符
      
  • 思路:预先定义好字母和数组的对应,用两个字符串来对应即可,这个思路好像是我之前某篇博客有提到的。然后就无脑变化输出结果了。

  • 代码:

import java.util.*;
public class Main{
     String str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
     String str2 = "bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA1234567890";

    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            String value1 = scan.nextLine();
            String value2 = scan.nextLine();
            char[] result1 = new char[value1.length()];
            char[] result2 = new char[value2.length()];
            Main test = new Main();
            test.Encrypt(value1.toCharArray(),result1);
            test.unEncrypt(value2.toCharArray(),result2);
        }   
    }
    //解密
    int unEncrypt (char result[], char password[]){
        for(int i = 0; i < result.length; i++){
            int index = str2.indexOf(result[i]);
            if(index != -1){
                password[i] = str1.charAt(index);
            }else{
                password[i] = result[i];
            }

            System.out.print(password[i]);
        }
        System.out.println();
        return 0;
    }
    //加密
    void Encrypt (char aucPassword[], char aucResult[]){
        for(int i = 0; i < aucPassword.length; i++){
            int index = str1.indexOf(aucPassword[i]);
            if(index != -1){
                aucResult[i] = str2.charAt(index);
            }else{
                aucResult[i] = aucPassword[i];
            }

            System.out.print(aucResult[i]);
        }
        System.out.println();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值