简体   繁体   English

如何显示,解密的二进制代码到哈希图中的映射

[英]How to display , decrypted binary code to mappings in hashmap

I have a hash map that contains the following mappings ....我有一个包含以下映射的哈希映射......

    HashMap <String , Integer>hm  = new HashMap <String , Integer> (); 
    hm.put("e", 0);
    hm.put("h",1);
    hm.put("i", 2);
    hm.put("k",3);
    hm.put("l",4);
    hm.put("r",5);
    hm.put("s",6);
    hm.put("t",7);

Along with this i have a binary sequence , which i have obtained from another computation as除此之外,我还有一个二进制序列,这是我从另一个计算中获得的

 1 0 10 100 1 10 111 100 0 101 

My objective is to get the resultant characters that these binary digits display from the hash map above .我的目标是从上面的哈希映射中获取这些二进制数字显示的结果字符。

For example .... 001 = 1 = h 
                 000 = 0 = e
                 010 = 2 = i

This code is part of a program that implements the one time pad in cryptography .此代码是在密码学中实现一次性密码的程序的一部分。 I have performed the encryption as well as the decryption .我已经执行了加密以及解密。

refer answer 3 here for the proof of code :有关代码证明,请参阅此处的答案 3:

storing charcter and binary number in a hash map 在哈希映射中存储字符和二进制数

But am struggling to display the decrypted binary code output , to the letters in my hash map .但是我正在努力将解密的二进制代码输出显示到我的哈希映射中的字母。

Thanks in advance提前致谢

You need to map from the value to the key (which is the reverse of the way a HashMap works).您需要从值映射到键(这与HashMap工作方式相反)。 Build a decryption Map .构建解密Map

Map<Integer, String> dec = new HashMap<>();
for (Map.Entry<String, Integer> kp : hm.entrySet()) {
    dec.put(kp.getValue(), kp.getKey());
}

Then you can iterate that by parsing your input values to int and getting the corresponding value.然后,您可以通过将输入值解析为int并获取相应的值来进行迭代。 Like喜欢

String input = "1 0 10 100 1 10 111 100 0 101";
Stream.of(input.split("\\s+")).map(s -> dec.get(Integer.parseInt(s, 2)))
        .forEachOrdered(s -> System.out.print(s + " "));
System.out.println();

Make a new map with the values and the rows swapped.使用交换的值和行制作新地图。

final Map<String, Integer> hm = ...
final Map<Integer, String> mapping = hm.entrySet().stream()
    .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));

Now you can use this map to do your lookup:现在您可以使用此地图进行查找:

final List<Byte> bytes = ...
final List<String> keys = bytes.stream()
    .map(mapping::get)
    .collect(Collectors.toList());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM