-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubstitutionScheme.java
More file actions
45 lines (37 loc) · 892 Bytes
/
SubstitutionScheme.java
File metadata and controls
45 lines (37 loc) · 892 Bytes
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
package DiffieHellman;
public class SubstitutionScheme
{
private static final int SMALLEST_CHAR_DECIMAL_VALUE = 32;
/**
*Key required to encrypt and decrypt the message.
*/
private final String Key;
public SubstitutionScheme(String Key)
{
this.Key = Key;
}
/**
*Encrypts the plaintext by substitution.
*/
public String encrypt(String plaintext)
{
String cipher = "";
for (char c : plaintext.toCharArray())
{
cipher += Key.charAt((int) c - SMALLEST_CHAR_DECIMAL_VALUE);
}
return cipher;
}
/**
*Decrypts the ciphertext by reversing the process of the encryption.
*/
public String decrypt(String ciphertext)
{
String text = "";
for (char c : ciphertext.toCharArray())
{
text += (char) (Key.indexOf((int) c) + SMALLEST_CHAR_DECIMAL_VALUE);
}
return text;
}
}