-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdditive.java
More file actions
58 lines (47 loc) · 1.53 KB
/
Copy pathAdditive.java
File metadata and controls
58 lines (47 loc) · 1.53 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
/**
* Additive Encryption Process
* @author Urjeet Deshmukh - November 18th, 2020
*/
import java.io.*;
import java.util.*;
public class Additive implements SymmetricCipher
{
private byte[] key;
public Additive() // Constructor with no parameters
{
this.key = new byte[128]; // Create array of bytes
Random random = new Random(); // Create Random generator
random.nextBytes(key); // Store random 128 byte additive key in array of bytes
}
public Additive(byte[] key) // Constructor that takes byte array as parameter
{
if(key.length != 128){
return;
}else{
this.key = key.clone(); // Use byte array parameter as its key
}
}
@Override
public byte[] encode(String strParam)
{
byte[] stringByte = strParam.getBytes(); // Convert string parameter to array of bytes
for(int i = 0; i < stringByte.length; i++){ // Add corresponding byte of the key to each index in the array of bytes
stringByte[i] = (byte)(stringByte[i] + key[i % key.length]); // If at end of key, start at front again
}
return stringByte; // Return encrypted array of bytes
}
@Override
public String decode(byte[] byteArray)
{
for(int i = 0; i < byteArray.length; i++){ // Subtract corresponding byte of the key from each index of the array of bytes
byteArray[i] = (byte)(byteArray[i] - key[i % key.length]); // If at end of key, start at front again
}
String stringDecode = new String(byteArray); // Convert byte array to String
return stringDecode;
}
@Override
public byte[] getKey() // Used in SecureChatClient
{
return key;
}
}