明文加密(密码字符)

本文介绍了一种简单的字母加密方法,通过读取用户输入的密钥,过滤重复字符并将其与完整字母表结合,实现对英文文本的替换加密。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.patience.interview.huawei;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

/**
 * 明文加密
 * @author Green.Gee
 * @date 2022/9/12 20:06
 * @email green.gee.lu@gmail.com
 */
public class LetterEncrypt {

    private static final char[] A = {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    };
    private static final String[] a = {
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
            "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
    };

    /**
     * 密码加密
     *      加密key 过滤重复字符
     *      将过滤后的字符串追加为完整的英文字母表
     *      输入代价密内容对照字母表映射为加密字母表中的字符替换
     *
     */
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String key = br.readLine();
        // repeat
        StringBuffer repeatKey = new StringBuffer(key.charAt(0)+"");
        for(int i = 1; i < key.length(); i++){
            repeatKey(repeatKey,(key.charAt(i)+""));
        }
        // whole letters
        String[] copy_a = {
                "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
                "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
        };
        StringBuffer wholeLetters = new StringBuffer();
        for(int i = 0; i < repeatKey.length(); i++){
            for(int j = 0;j < copy_a.length; j++){
                if(copy_a[j] != null && copy_a[j].equals((repeatKey.charAt(i) + ""))){
                    wholeLetters.append(copy_a[j]);
                    copy_a[j] = null;
                }
            }
        }
        for(int j = 0;j < a.length; j++) {
            if (copy_a[j] != null) {
                wholeLetters.append(copy_a[j]);
            }
        }
        String [] letters = wholeLetters.toString().split("");
        String text = br.readLine();
        String[] tx = text.split("");
        StringBuffer result = new StringBuffer();
        for (String item : tx){
            for(int i = 0; i < a.length; i++){
                if(item.equals(a[i])){
                    result.append(letters[i]);
                }
            }
        }
        System.out.println(result.toString());
    }

    private static StringBuffer repeatKey(StringBuffer repeatKey,String c){
        if(!repeatKey.toString().contains(c)){
            repeatKey.append(c);
        }
        return repeatKey;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

P(DNA)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值