1 package org.jastacry.layer;
2
3 import java.nio.charset.StandardCharsets;
4 import java.security.MessageDigest;
5 import java.security.NoSuchAlgorithmException;
6 import java.util.Arrays;
7
8 import javax.crypto.spec.SecretKeySpec;
9
10 import org.jastacry.JastacryException;
11
12
13
14
15
16
17
18
19 public class AesEcbLayer extends AbstractCipherLayer
20 {
21
22
23
24 public static final String LAYERNAME = "AES ECB Layer";
25
26
27
28
29 private static final String MYALG = "AES/ECB/PKCS5Padding";
30
31
32
33
34 private static final String MYKEYALG = "AES";
35
36
37
38
39 private static final String MYHASHALG = "SHA-256";
40
41
42
43
44 private static final int IVLEN = 0;
45
46
47
48
49 private static final int SALTLEN = 0;
50
51
52
53
54 private static final int COUNT = 0;
55
56
57
58
59 private static final int KEYSIZE = 128;
60
61
62
63
64 public AesEcbLayer()
65 {
66 super(AesEcbLayer.class, LAYERNAME);
67 }
68
69
70
71
72
73
74 @Override
75 protected final void setupPbe() throws JastacryException
76 {
77 MessageDigest sha;
78 try
79 {
80 sha = MessageDigest.getInstance(MYHASHALG);
81 final byte[] keyPlaintext = new String(chPasswd).getBytes(StandardCharsets.UTF_8);
82 final byte[] keyDigest = sha.digest(keyPlaintext);
83 final byte[] keyDigestTrimmed = Arrays.copyOf(keyDigest, KEYSIZE / BITSPERBYTE);
84
85 pbeSecretKeySpec = new SecretKeySpec(keyDigestTrimmed, MYKEYALG);
86 }
87 catch (NoSuchAlgorithmException e)
88 {
89 throw (JastacryExceptionl#JastacryException">JastacryException) new JastacryException("Setup PBE failed").initCause(e);
90 }
91
92 }
93
94
95
96
97
98
99 @Override
100 public final void init(final String data)
101 {
102 super.init();
103
104 this.chPasswd = data.toCharArray();
105 }
106
107 @Override
108 protected final String getMyAlg()
109 {
110 return MYALG;
111 }
112
113 @Override
114 protected final String getMyKeyAlg()
115 {
116 return MYKEYALG;
117 }
118
119 @Override
120 protected int getMySaltLen()
121 {
122 return SALTLEN;
123 }
124
125 @Override
126 protected int getMyIvLen()
127 {
128 return IVLEN;
129 }
130
131 @Override
132 protected int getMyCount()
133 {
134 return COUNT;
135 }
136
137 @Override
138 protected int getMyKeysize()
139 {
140 return KEYSIZE;
141 }
142
143
144
145
146
147
148 @Override
149 public boolean equals(final Object o)
150 {
151 if (o == this)
152 {
153 return true;
154 }
155 if (!(o instanceof AesEcbLayer))
156 {
157 return false;
158 }
159
160 final AesEcbLayer./../org/jastacry/layer/AesEcbLayer.html#AesEcbLayer">AesEcbLayer layer = (AesEcbLayer) o;
161 return Arrays.equals(layer.chPasswd, this.chPasswd);
162 }
163
164
165
166
167
168 @Override
169 public int hashCode()
170 {
171 return Arrays.hashCode(chPasswd);
172 }
173 }