CryptoSupportImpl.java
5.9 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.granite.crypto.internal;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import java.util.Arrays;
public abstract class CryptoSupportImpl
implements CryptoSupport {
private static final String CHARACTER_SET = "UTF-8";
protected CryptoSupportImpl() {
}
protected abstract void init(byte[] var1) throws Exception;
protected abstract void init_hmac(byte[] var1) throws Exception;
protected abstract void dispose(byte[] var1);
protected abstract void dispose();
protected abstract byte[] init() throws Exception;
protected abstract byte[] init_hmac() throws Exception;
protected abstract byte[] getCipherText(byte[] var1) throws Exception;
protected abstract byte[] getCipherText(byte[] var1, byte[] var2) throws Exception;
protected abstract byte[] getPlainText(byte[] var1) throws Exception;
protected abstract byte[] getPlainText(byte[] var1, byte[] var2) throws Exception;
@Override
public final byte[] encrypt(byte[] plainText) throws CryptoException {
try {
return this.getCipherText(plainText);
}
catch (Exception e) {
throw new CryptoException("Failed encrypting plain text", e);
}
}
@Override
public final byte[] decrypt(byte[] cipherText) throws CryptoException {
try {
return this.getPlainText(cipherText);
}
catch (Exception e) {
throw new CryptoException("Failed decrypting cipher text", e);
}
}
@Override
public final byte[] encrypt(byte[] key, byte[] plainText) throws CryptoException {
try {
return this.getCipherText(key, plainText);
}
catch (Exception e) {
throw new CryptoException("Failed encrypting plain text", e);
}
}
@Override
public final byte[] decrypt(byte[] key, byte[] cipherText) throws CryptoException {
try {
return this.getPlainText(key, cipherText);
}
catch (Exception e) {
throw new CryptoException("Failed decrypting cipher text", e);
}
}
@Override
public final boolean isProtected(String text) {
return text != null && text.length() > 2 && text.charAt(0) == '{' && text.charAt(text.length() - 1) == '}' && text.length() % 2 == 0;
}
@Override
public final String protect(String plainText) throws CryptoException {
byte[] datumBytes = null;
byte[] cipherBytes = null;
try {
datumBytes = plainText.getBytes("UTF-8");
cipherBytes = this.encrypt(datumBytes);
String string = CryptoSupportImpl.toString(cipherBytes);
return string;
}
catch (Exception e) {
throw new CryptoException("Cannot encrypt plain text", e);
}
finally {
CryptoSupportImpl.clear(datumBytes);
CryptoSupportImpl.clear(cipherBytes);
}
}
@Override
public final String protect(byte[] key, String plainText) throws CryptoException {
byte[] datumBytes = null;
byte[] cipherBytes = null;
try {
datumBytes = plainText.getBytes("UTF-8");
cipherBytes = this.encrypt(key, datumBytes);
String string = CryptoSupportImpl.toString(cipherBytes);
return string;
}
catch (Exception e) {
throw new CryptoException("Cannot encrypt plain text", e);
}
finally {
CryptoSupportImpl.clear(datumBytes);
CryptoSupportImpl.clear(cipherBytes);
}
}
@Override
public final String unprotect(String cipherText) throws CryptoException {
byte[] datumBytes = null;
byte[] cipherBytes = null;
try {
cipherBytes = this.toByteArray(cipherText);
datumBytes = this.decrypt(cipherBytes);
String string = new String(datumBytes, "UTF-8");
return string;
}
catch (Exception e) {
throw new CryptoException("Cannot convert byte data", e);
}
finally {
CryptoSupportImpl.clear(datumBytes);
CryptoSupportImpl.clear(cipherBytes);
}
}
@Override
public final String unprotect(byte[] key, String cipherText) throws CryptoException {
byte[] datumBytes = null;
byte[] cipherBytes = null;
try {
cipherBytes = this.toByteArray(cipherText);
datumBytes = this.decrypt(key, cipherBytes);
String string = new String(datumBytes, "UTF-8");
return string;
}
catch (Exception e) {
throw new CryptoException("Cannot convert byte data", e);
}
finally {
CryptoSupportImpl.clear(datumBytes);
CryptoSupportImpl.clear(cipherBytes);
}
}
protected static void clear(byte[] data) {
if (data != null) {
Arrays.fill(data, 0);
}
}
private static String toString(byte[] binaryData) {
StringBuilder res = new StringBuilder(binaryData.length * 2 + 2);
res.append('{');
for (int i = 0; i < binaryData.length; ++i) {
String hex = Integer.toHexString(binaryData[i] & 255);
if (hex.length() < 2) {
res.append('0');
}
res.append(hex);
}
res.append('}');
return res.toString();
}
private byte[] toByteArray(String text) {
if (!this.isProtected(text)) {
throw new IllegalArgumentException("Cannot decrypt obviously unprotected data");
}
byte[] data = new byte[text.length() / 2 - 1];
for (int i = 1; i < text.length() - 1; i += 2) {
String hex = text.substring(i, i + 2);
int val = Integer.parseInt(hex, 16);
data[i / 2] = (byte)val;
}
return data;
}
}