-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactoryMethod
More file actions
72 lines (61 loc) · 1.54 KB
/
FactoryMethod
File metadata and controls
72 lines (61 loc) · 1.54 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
62
63
64
65
66
67
68
69
70
71
72
package FactoryMethod;
import java.util.List;
public abstract class Encryptor {
private SecretKey key;
public Encryptor(){
key=createKey();
}
public byte[] encryptor(List<String> lines){
int lengths=0;
for(String line:lines){
lengths+=line.length();
}
byte[] result=new byte[lengths];
int i=0;
for(String line:lines){
byte[] ct=key.encrypt(line.getBytes());
for(byte b:ct){
result[i++]=b;
}
}
return result;
}
public abstract SecretKey createKey();
}
class InsecureEncryptor extends Encryptor{
// private ShortSecretKey key;
@Override
public SecretKey createKey() {
return new ShortSecretKey();
}
}
import java.nio.charset.StandardCharsets;
public abstract class SecretKey {
private byte[] key;
public SecretKey(int n){
key=new byte[n];
}
public byte[] encrypt(byte[] message){
int m=message.length;
int k = key.length;
int j =0;
byte[] result=new byte[m];
for(int i=0;i<m;i++){
result[i] =(byte) (message[i]^key[j]%key.length);
j+=1;
if(j==k){
j=0;
}
}
return result;
}
public String decrypt(byte[] message){
return new String(encrypt(message),StandardCharsets.UTF_8);
}
}
public class ShortSecretKey extends SecretKey{
private static final int MAX_LEN=16;
public ShortSecretKey(){
super(MAX_LEN);
}
}