-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecryption.java
More file actions
61 lines (53 loc) · 1.83 KB
/
Copy pathDecryption.java
File metadata and controls
61 lines (53 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.decryption;
/**
* Decryption
* Decrypting messages based on a key.
*
* There is no input for this program.
* Given: A message that has to decoded based on a key.
*
* @author Harsh <girdharharsh@gmail.com>
* @since 11/26/2015 5:20PM
*/
public class Decryption {
/**
* decryptMessage
* Returns the decoded message based on the unique key.
* Apply a decrytion scheme for decoding the message.
*
* @param key Unique key to decode the message
* @param msg The message that is to be decoded
* @return String Final decoded string
*/
public String decryptMessage(String key, String msg) {
// Ascii value of CAPS A -> as the output is in CAPS
int asciiA = (int) new Character('A');
// Whatever is decoded is concatenated in this string.
String decoded = "";
// First level of decoding.
// Fetch 2 characters from message string at a time and search in english alphabets.
for (int i = 0, len = msg.length(); i < len; i += 2) {
char messageChar = msg.charAt(i);
if (messageChar == ' ') {
decoded += " ";
i--;
continue;
}
String index = "" + messageChar + msg.charAt(i + 1);
// Searching alphabet in english characters.
char alphabetPos[] = Character.toChars(asciiA + Integer.parseInt(index));
// value associated with that alphabet in given key.
int keyIndex = key.indexOf(alphabetPos[0]);
// Search for alphabet situated at position 'keyIndex'.
char decodedCharacter[] = Character.toChars(asciiA + keyIndex);
decoded += decodedCharacter[0];
}
return decoded;
}
public static void main(String[] args) {
Decryption decryption = new Decryption();
String key = "BHISOECRTMGWYVALUZDNFJKPQX";
String msg = "012222 1114142503 0313012513 03141418192102 0113 2419182119021713 06131715070119";
System.out.println(decryption.decryptMessage(key, msg));
}
}