密码加密

/*
    加密办法:
        对消息原文的每个字母
            分别用该字幕之后的第五个字幕替换    例如A——F
            其他字符不变,并且消息原文的所有字母都是大写的
*/

import java.util.*;
 
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            char[] sw=new char[130];
            String str1="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            String str2="VWXYZABCDEFGHIJKLMNOPQRSTU";
            for(int i=0;i<str1.length();i++){
                sw[str1.charAt(i)]=str2.charAt(i);
            }
            String str=sc.nextLine();
            String result="";
            for(int i=0;i<str.length();i++){
                if(str.charAt(i)==' '){
                    result+=" ";
                }else{
                    result+=sw[str.charAt(i)];
                }
            }
            System.out.println(result);
        }
    }
}

/*
    加密办法:
        对消息原文的每个字母
            分别用该字幕之后的第五个字幕替换    例如A——F
            其他字符不变,并且消息原文的所有字母都是大写的
*/

import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String CHTStr = sc.nextLine();
            System.out.println(GetClearCode(CHTStr));
        }
    }
 
    //HELLO WORLD SNHJ  密文
    //CZGGJ RJMGY NICE  明文
    public static String GetClearCode(String str){
        Map<Character,Character> map = new HashMap<>();
        List<Character> chr = new ArrayList<>();
        for (int i = 0; i <26 ; i++) {
            chr.add((char)(65+i));
        }
 
        //将密文与明文之间的对应关系保存在一个Map集合里
        for (int i = 0; i <chr.size() ; i++) {
            if(i <5){
                map.put(chr.get(i),(char)(chr.get(i)+21));
            }else {
                map.put(chr.get(i),(char)(chr.get(i)-5));
            }
        }
 
        char[] ch = str.toCharArray();
        for (int i = 0; i <ch.length; i++) {
            if(map.containsKey(ch[i])){
                ch [i] = map.get(ch [i]);
            }
        }
 
        return new String(ch);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值