实验十八加密处理

题目:

有一种加密方法为:其使用一个字母串(可以含重复字母,字母个数不超过50)作为密钥。假定密钥单词串为feather,则先去掉密钥单词中的重复字母得到单词串feathr,然后将其反序,并将字母表中的其它字母以反序追加到后面:

 r

 h

 t

 a

 e

 f

 z

 y

 x

 w

 v

 u

 s

 q

 p

 o

 n

 m

 l

 k

 j

 i

 g

 d

 c

 b 

加密字母的对应关系如下:

 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

 r

 h

 t

 a

 e

 f

 z

 y

 x

 w

 v

 u

 s

 q

 p

 o

 n

 m

 l

 k

 j

 i

 g

 d

 c

 b

其中第一行为原始英文字母,第二行为对应加密字母。其它字符不进行加密。编写一个程序,用这种密码加密输入的字符串。假定输入的待加密字符串中的字母全为小写字母,并且输入密钥也全为小写字母。

题目翻译:

这个题目要求我们使用一种特定的加密方法对输入的字符串进行加密。加密方法基于一个密钥单词串,该串由字母组成,可以包含重复字母,但字母个数不超过50。

首先,我们需要对密钥单词串进行处理。假设密钥单词串为"feather",处理步骤如下:

  1. 去除密钥单词中的重复字母,得到单词串"feathr"。
  2. 将单词串反序,得到反序后的单词串"rhtaef"。
  3. 将字母表中的其他字母以反序追加到反序后的单词串后面,形成完整的加密密钥。

完成密钥生成后,我们可以使用该加密密钥对输入的字符串进行加密。加密规则如下:

  1. 对于输入字符串中的每个字母,根据加密密钥找到对应位置的加密字母。(创建Hasmap,秘钥作为值,而待加密字母作为键,通过映射关系使得文字加密)
  2. 将加密字母输出作为加密后的结果。
  3. 非字母字符直接输出,不进行加密。(判断Hashmap中有无该字符键,有则输出键对应的值,没有则直接输出字符本身)

import java.util.*;

public class TextEncryption {
    public static void main(String[] args) {
        // 接受用户输入
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入关键词:");
        String keywords = scanner.nextLine();
        System.out.print("请输入要加密的文本:");
        String text = scanner.nextLine();
        scanner.close();

        // 生成加密后的文本
        String encryptedText = encryptText(keywords, text);

        // 输出加密后的文本
        System.out.println("加密后的文本:" + encryptedText);
    }

    // 使用给定的关键词加密文本的方法
    private static String encryptText(String keywords, String text) {
        // 获取英文字母的唯一字符集合
        HashSet<String> alphabetSet = getAlphabetSet();

        // 获取关键词的唯一字符集合
        HashSet<String> keywordSet = getUniqueCharacters(keywords);

        // 从字母字符集合中删除关键词的字符
        alphabetSet.removeAll(keywordSet);

        // 将剩余字符转换为数组
        String[] remainingChars = alphabetSet.toArray(new String[0]);

        // 反转数组
        reverseArray(remainingChars);
        String[] reversedKeywords = reverseArray(getUniqueCharacters(keywords).toArray(new String[0]));

        // 组合数组
        String[] combinedChars = combineArrays(reversedKeywords, remainingChars);

        // 创建映射关系
        HashMap<String, String> map = createCharacterMapping(combinedChars);

        // 加密文本
        StringBuilder encryptedTextBuilder = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            String character = String.valueOf(text.charAt(i));
            if (map.containsKey(character)) {
                encryptedTextBuilder.append(map.get(character));
            } else {
                encryptedTextBuilder.append(character);
            }
        }

        // 返回加密后的文本
        return encryptedTextBuilder.toString();
    }

    // 获取英文字母的唯一字符集合
    private static HashSet<String> getAlphabetSet() {
        HashSet<String> alphabetSet = new HashSet<>();
        String[] alphabetArray = {"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"};
        for (int i = 0; i < alphabetArray.length; i++) {
            alphabetSet.add(alphabetArray[i]);
        }
        return alphabetSet;
    }

    // 获取字符串中的唯一字符集合
    private static HashSet<String> getUniqueCharacters(String s) {
        HashSet<String> uniqueSet = new HashSet<>();
        for (int i = 0; i < s.length(); i++) {
            String character = String.valueOf(s.charAt(i));
            uniqueSet.add(character);
        }
        return uniqueSet;
    }

    // 反转数组
    private static void reverseArray(String[] array) {
        for (int i = 0; i < array.length / 2; i++) {
            String temp = array[i];
            array[i] = array[array.length - i - 1];
            array[array.length - i - 1] = temp;
        }
    }

    // 组合数组
    private static String[] combineArrays(String[] array1, String[] array2) {
        String[] combinedArray = new String[array1.length + array2.length];
        int index = 0;
        for (String element : array1) {
            combinedArray[index] = element;
            index++;
        }
        for (String element : array2) {
            combinedArray[index] = element;
            index++;
        }
        return combinedArray;
    }

    // 创建字符映射关系的方法
    private static HashMap<String, String> createCharacterMapping(String[] characters) {
        HashMap<String, String> characterMap = new HashMap<>();
        String[] alphabetArray = {"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"};
        for (int i = 0; i < characters.length; i++) {
            characterMap.put(alphabetArray[i], characters[i]);
        }
        return characterMap;
    }
}

 

源代码会在5.19号后更新 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值