Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/crypto/SecretKeyFactory/SecKFTranslateTest.java
41149 views
1
/*
2
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.security.InvalidAlgorithmParameterException;
25
import java.security.InvalidKeyException;
26
import java.security.NoSuchAlgorithmException;
27
import java.security.NoSuchProviderException;
28
import java.security.SecureRandom;
29
import java.security.spec.AlgorithmParameterSpec;
30
import java.security.spec.InvalidKeySpecException;
31
import java.util.Arrays;
32
import java.util.Random;
33
34
import javax.crypto.BadPaddingException;
35
import javax.crypto.Cipher;
36
import javax.crypto.IllegalBlockSizeException;
37
import javax.crypto.NoSuchPaddingException;
38
import javax.crypto.SecretKey;
39
import javax.crypto.SecretKeyFactory;
40
import javax.crypto.ShortBufferException;
41
import javax.crypto.spec.PBEKeySpec;
42
import javax.crypto.spec.PBEParameterSpec;
43
import javax.security.auth.DestroyFailedException;
44
45
/*
46
* @test
47
* @bug 8048820
48
* @summary The test verifies SecretKey values should remain the same after
49
* translation with SecretKeyFactory.translateKey().
50
*/
51
52
public class SecKFTranslateTest {
53
private static final String SUN_JCE = "SunJCE";
54
55
public static void main(String[] args) throws Exception {
56
57
SecKFTranslateTest test = new SecKFTranslateTest();
58
test.run();
59
}
60
61
private void run() throws Exception {
62
63
for (Algorithm algorithm : Algorithm.values()) {
64
runTest(algorithm);
65
}
66
}
67
68
private void runTest(Algorithm algo) throws NoSuchAlgorithmException,
69
NoSuchProviderException, InvalidKeyException,
70
InvalidKeySpecException, NoSuchPaddingException,
71
InvalidAlgorithmParameterException, ShortBufferException,
72
IllegalBlockSizeException, BadPaddingException {
73
AlgorithmParameterSpec[] aps = new AlgorithmParameterSpec[1];
74
byte[] plainText = new byte[800];
75
76
SecretKey key1 = algo.intSecurityKey(aps);
77
Random random = new Random();
78
// Initialization
79
SecretKeyFactory skf = SecretKeyFactory.getInstance(algo.toString(),
80
SUN_JCE);
81
82
random.nextBytes(plainText);
83
Cipher ci = Cipher.getInstance(algo.toString(), SUN_JCE);
84
// Encryption
85
ci.init(Cipher.ENCRYPT_MODE, key1, aps[0]);
86
byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
87
int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
88
ci.doFinal(cipherText, offset);
89
// translate key
90
SecretKey key2 = skf.translateKey(key1);
91
92
// Decryption
93
ci.init(Cipher.DECRYPT_MODE, key2, aps[0]);
94
byte[] recoveredText = new byte[ci.getOutputSize(plainText.length)];
95
ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
96
97
// Comparison
98
if (!Arrays.equals(plainText, recoveredText)) {
99
System.out.println("Key1:" + new String(key1.getEncoded())
100
+ " Key2:" + new String(key2.getEncoded()));
101
throw new RuntimeException("Testing translate key failed with "
102
+ algo);
103
}
104
105
}
106
}
107
108
class MyOwnSecKey implements SecretKey {
109
110
private static final String DEFAULT_ALGO = "PBEWithMD5AndDES";
111
private final byte[] key;
112
private final String algorithm;
113
private final int keySize;
114
115
public MyOwnSecKey(byte[] key1, int offset, String algo)
116
throws InvalidKeyException {
117
algorithm = algo;
118
if (algo.equalsIgnoreCase("DES")) {
119
keySize = 8;
120
} else if (algo.equalsIgnoreCase("DESede")) {
121
keySize = 24;
122
} else {
123
throw new InvalidKeyException(
124
"Inappropriate key format and algorithm");
125
}
126
127
if (key1 == null || key1.length - offset < keySize) {
128
throw new InvalidKeyException("Wrong key size");
129
}
130
key = new byte[keySize];
131
System.arraycopy(key, offset, key, 0, keySize);
132
}
133
134
public MyOwnSecKey(PBEKeySpec ks) throws InvalidKeySpecException {
135
algorithm = DEFAULT_ALGO;
136
key = new String(ks.getPassword()).getBytes();
137
keySize = key.length;
138
}
139
140
@Override
141
public String getAlgorithm() {
142
return algorithm;
143
}
144
145
@Override
146
public String getFormat() {
147
return "RAW";
148
}
149
150
@Override
151
public byte[] getEncoded() {
152
byte[] copy = new byte[keySize];
153
System.arraycopy(key, 0, copy, 0, keySize);
154
return copy;
155
}
156
157
@Override
158
public void destroy() throws DestroyFailedException {
159
}
160
161
@Override
162
public boolean isDestroyed() {
163
return false;
164
}
165
}
166
167
enum Algorithm {
168
DES {
169
@Override
170
SecretKey intSecurityKey(AlgorithmParameterSpec[] spec)
171
throws InvalidKeyException {
172
int keyLength = 8;
173
byte[] keyVal = new byte[keyLength];
174
new SecureRandom().nextBytes(keyVal);
175
SecretKey key1 = new MyOwnSecKey(keyVal, 0, this.toString());
176
return key1;
177
}
178
},
179
DESEDE {
180
@Override
181
SecretKey intSecurityKey(AlgorithmParameterSpec[] spec)
182
throws InvalidKeyException {
183
int keyLength = 24;
184
byte[] keyVal = new byte[keyLength];
185
new SecureRandom().nextBytes(keyVal);
186
SecretKey key1 = new MyOwnSecKey(keyVal, 0, this.toString());
187
return key1;
188
}
189
},
190
PBEWithMD5ANDdes {
191
@Override
192
SecretKey intSecurityKey(AlgorithmParameterSpec[] spec)
193
throws InvalidKeySpecException {
194
byte[] salt = new byte[8];
195
int iterCnt = 6;
196
new Random().nextBytes(salt);
197
spec[0] = new PBEParameterSpec(salt, iterCnt);
198
PBEKeySpec pbeKS = new PBEKeySpec(
199
new String("So far so good").toCharArray());
200
SecretKey key1 = new MyOwnSecKey(pbeKS);
201
return key1;
202
}
203
};
204
abstract SecretKey intSecurityKey(AlgorithmParameterSpec[] spec)
205
throws InvalidKeyException, InvalidKeySpecException;
206
}
207
208