java


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 :
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). Build a decryption 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. 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());

Related Links

timestamptoken validate java bouncycastle
Get Column width of excel file using SaxParser
The server is sending data through the socket, but the client is not receving it (Java)
Java WebView generate an exception on Windows XP
Backward compatibility in Enum Singleton vs Class
Populate an array with random numbers in the range of 0-4 with an increment of 0.5
Not given permission for contact still display into app permission
Using Java Concurrent Collections in Scala [duplicate]
Java program for detecting server login prompt?
Keep getting java.lang.NoClassDefFoundError in Spring
How to bind to more than 1 ReactFX observable?
How to use custom converters with #DataMongoTest?
Where did the lib project go for google play services?
Get only latest posts of Facebook using RestFB
printf is giving me the wrong output
How to get the path name outside the loop?

Categories

HOME
cakephp
twitter
hive
testng
vbscript
keras
google-play
appx
hashmap
fft
razor
microservices
google-project-tango
opengl-es-2.0
reveal.js
alignment
circuit
workload-scheduler
pc
jsdoc
invantive-sql
oracle-coherence
interop
alpine
cx-freeze
abi
ehcache
sox
tapestry
mmap
autosys
buildbot
chromebook
applozic
minitab
c++-amp
google-cloud-endpoints-v2
http-status-code-503
xquery-3.0
http-digest
vapor
lumberjack
mapzen
core-plot
nand2tetris
modelmapper
executenonquery
osx-mavericks
qsslsocket
hendrix
pnotify
boost-multi-index
setter
lift-json
orthogonal
eventkit
messenger
wdf
vmware-tools
dstu2-fhir
transmitfile
multiple-regression
dlna
suffix-tree
photobucket
reactive-banana
qdialog
django-unittest
citrus-pay
kcachegrind
responsive-images
method-parameters
system32
p2
truevault
c++03
website-monitoring
dd
android-nested-fragment
zend-route
ceil
uitouch
commoncrypto
dir
apc
tws
bulkloader
dataadapter
quickdialog
getmessage
osql
nsmanagedobject
mercurial-server
window-management
gnustep
gcj
tomcat-valve
iphone-web-app
asp.net-mvc-areas
mozilla-prism
gin
forums
user-friendly
backcolor
premature-optimization
pascal-fc
msdev

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App