Java版本的字符串加密

  • 思路是利用有序的LinkedHashSet集合,先把密钥添加进去,再把剩下的字符添加进去形成完整的密钥;

  • 再根据要加密的字符串字符一个个取出拼接输出,ps:不要忘了大小写和空格

import java.util.Scanner;
import java.util.LinkedHashSet;
import java.util.ArrayList;

public class Main{
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s1 = sc.nextLine().toUpperCase();
            String s2 = sc.nextLine();
            char[] chars1 = s1.toCharArray();
            char[] chars2 = s2.toCharArray();
            LinkedHashSet<Character> set = new LinkedHashSet();
            for (int i = 0; i < chars1.length; i++) {
                set.add(chars1[i]);
            }
            int k = 0;
            while (set.size() < 26) {
                char a = (char) ('A' + k);
                set.add(a);
                k++;
            }
            ArrayList<Character> list = new ArrayList<>(set);
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < chars2.length; i++) {
                if (chars2[i] == ' ') {
                    sb.append(chars2[i]);
                } else if (chars2[i] < 'a') {
                    int n = (int) (chars2[i] - 'A');
                    char c = list.get(n);
                    sb.append(c);
                } else {
                    int n = (int) (chars2[i] - 'a');
                    char c = (char)(list.get(n)+'a'-'A');
                    sb.append(c);
                }

            }

            System.out.println(sb.toString());
        }
    }
}