-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCipherMorse.java
69 lines (63 loc) · 2.33 KB
/
CipherMorse.java
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
62
63
64
65
66
67
68
69
//import javax.crypto.Cipher;
//import javax.crypto.CipherSpi;
//import java.security.Provider;
import java.util.HashMap;
import java.util.Map;
public class CipherMorse extends Cipher {
private static final Map<Character, String> morseCodeMap = new HashMap<>();
static {
morseCodeMap.put('A', ".-");
morseCodeMap.put('B', "-...");
morseCodeMap.put('C', "-.-.");
morseCodeMap.put('D', "-..");
morseCodeMap.put('E', ".");
morseCodeMap.put('F', "..-.");
morseCodeMap.put('G', "--.");
morseCodeMap.put('H', "....");
morseCodeMap.put('I', "..");
morseCodeMap.put('J', ".---");
morseCodeMap.put('K', "-.-");
morseCodeMap.put('L', ".-..");
morseCodeMap.put('M', "--");
morseCodeMap.put('N', "-.");
morseCodeMap.put('O', "---");
morseCodeMap.put('P', ".--.");
morseCodeMap.put('Q', "--.-");
morseCodeMap.put('R', ".-.");
morseCodeMap.put('S', "...");
morseCodeMap.put('T', "-");
morseCodeMap.put('U', "..-");
morseCodeMap.put('V', "...-");
morseCodeMap.put('W', ".--");
morseCodeMap.put('X', "-..-");
morseCodeMap.put('Y', "-.--");
morseCodeMap.put('Z', "--..");
morseCodeMap.put('0', "-----");
morseCodeMap.put('1', ".----");
morseCodeMap.put('2', "..---");
morseCodeMap.put('3', "...--");
morseCodeMap.put('4', "....-");
morseCodeMap.put('5', ".....");
morseCodeMap.put('6', "-....");
morseCodeMap.put('7', "--...");
morseCodeMap.put('8', "---..");
morseCodeMap.put('9', "----.");
}
public CipherMorse() {
}
public String encode(String message) {
StringBuilder encryptedMessage = new StringBuilder();
for (char c : message.toUpperCase().toCharArray()) {
if (Character.isWhitespace(c)) {
// Dodaj spację pomiędzy słowami
encryptedMessage.append(" / ");
} else if (morseCodeMap.containsKey(c)) {
// Pobierz kod Morse'a dla danego znaku
encryptedMessage.append(morseCodeMap.get(c)).append(" ");
} else {
// Zignoruj znaki, które nie mają odpowiednika w kodzie Morse'a
}
}
return encryptedMessage.toString();
}
}