Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/TextLength/DESCipherWrapper.java
41161 views
1
/*
2
* Copyright (c) 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 static java.lang.System.out;
25
26
import java.security.InvalidAlgorithmParameterException;
27
import java.security.InvalidKeyException;
28
import java.security.NoSuchAlgorithmException;
29
import java.security.spec.AlgorithmParameterSpec;
30
import java.util.ArrayList;
31
import java.util.Arrays;
32
import java.util.List;
33
import javax.crypto.BadPaddingException;
34
import javax.crypto.IllegalBlockSizeException;
35
import javax.crypto.SecretKey;
36
import javax.crypto.ShortBufferException;
37
import javax.crypto.Cipher;
38
import javax.crypto.KeyGenerator;
39
import javax.crypto.NoSuchPaddingException;
40
import javax.crypto.spec.IvParameterSpec;
41
42
/**
43
* Wrapper class to test a given DES algorithm.
44
*/
45
public class DESCipherWrapper {
46
47
private final Cipher ci;
48
private final byte[] iv;
49
private final SecretKey key;
50
private final String algo;
51
private final String mode;
52
private final String pad;
53
private final int keyStrength;
54
private byte[] resultText = null;
55
56
public DESCipherWrapper(String algo, String mode, String pad)
57
throws NoSuchAlgorithmException, NoSuchPaddingException {
58
ci = Cipher.getInstance(algo + "/" + mode + "/" + pad);
59
60
iv = new byte[8];
61
for (int i = 0; i < 8; i++) {
62
iv[i] = (byte) (i & 0xff);
63
}
64
65
KeyGenerator kg = KeyGenerator.getInstance(algo);
66
key = kg.generateKey();
67
keyStrength = algo.equalsIgnoreCase("DESede") ? 112
68
: key.getEncoded().length * 8;
69
70
this.algo = algo;
71
this.mode = mode;
72
this.pad = pad;
73
}
74
75
public byte[] getResult() {
76
return resultText.clone();
77
}
78
79
public void execute(int edMode, byte[] inputText)
80
throws InvalidKeyException, InvalidAlgorithmParameterException,
81
IllegalBlockSizeException, BadPaddingException,
82
ShortBufferException, NoSuchAlgorithmException {
83
AlgorithmParameterSpec aps = null;
84
85
try {
86
if (!mode.equalsIgnoreCase("ECB")) {
87
aps = new IvParameterSpec(iv);
88
}
89
ci.init(edMode, key, aps);
90
91
// Generate a resultText using a single-part enc/dec
92
resultText = ci.doFinal(inputText);
93
94
// Generate outputText for each multi-part en/de-cryption
95
/* Combination #1:
96
update(byte[], int, int)
97
doFinal(byte[], int, int)
98
*/
99
byte[] part11 = ci.update(inputText, 0, inputText.length);
100
byte[] part12 = ci.doFinal();
101
byte[] outputText1 = new byte[part11.length + part12.length];
102
System.arraycopy(part11, 0, outputText1, 0, part11.length);
103
System.arraycopy(part12, 0, outputText1, part11.length,
104
part12.length);
105
106
List<byte[]> outputTexts = new ArrayList<>(4);
107
outputTexts.add(outputText1);
108
109
/* Combination #2:
110
update(byte[], int, int)
111
doFinal(byte[], int, int, byte[], int)
112
*/
113
byte[] part21 = ci.update(inputText, 0, inputText.length - 5);
114
byte[] part22 = new byte[ci.getOutputSize(inputText.length)];
115
int len2 = ci
116
.doFinal(inputText, inputText.length - 5, 5, part22, 0);
117
byte[] outputText2 = new byte[part21.length + len2];
118
System.arraycopy(part21, 0, outputText2, 0, part21.length);
119
System.arraycopy(part22, 0, outputText2, part21.length, len2);
120
121
outputTexts.add(outputText2);
122
123
/* Combination #3:
124
update(byte[], int, int, byte[], int)
125
doFinal(byte[], int, int)
126
*/
127
byte[] part31 = new byte[ci.getOutputSize(inputText.length)];
128
int len3 = ci.update(inputText, 0, inputText.length - 8, part31, 0);
129
byte[] part32 = ci.doFinal(inputText, inputText.length - 8, 8);
130
byte[] outputText3 = new byte[len3 + part32.length];
131
System.arraycopy(part31, 0, outputText3, 0, len3);
132
System.arraycopy(part32, 0, outputText3, len3, part32.length);
133
134
outputTexts.add(outputText3);
135
136
/* Combination #4:
137
update(byte[], int, int, byte[], int)
138
doFinal(byte[], int, int, byte[], int)
139
*/
140
byte[] part41 = new byte[ci.getOutputSize(inputText.length)];
141
int len4 = ci.update(inputText, 0, inputText.length - 8, part41, 0);
142
int rest4 = ci.doFinal(inputText, inputText.length - 8, 8, part41,
143
len4);
144
byte[] outputText4 = new byte[len4 + rest4];
145
System.arraycopy(part41, 0, outputText4, 0, outputText4.length);
146
147
outputTexts.add(outputText4);
148
149
// Compare results
150
for (int k = 0; k < outputTexts.size(); k++) {
151
if (!Arrays.equals(resultText, outputTexts.get(k))) {
152
out.println(" Testing: " + algo + "/" + mode + "/" + pad);
153
throw new RuntimeException(
154
"Compare value of resultText and combination " + k
155
+ " are not same. Test failed.");
156
}
157
}
158
if (keyStrength > Cipher.getMaxAllowedKeyLength(algo)) {
159
throw new RuntimeException(
160
"Expected exception uncaught, keyStrength "
161
+ keyStrength);
162
}
163
} catch (InvalidKeyException ex) {
164
if (keyStrength <= Cipher.getMaxAllowedKeyLength(algo)) {
165
out.println("Unexpected exception in " + algo + "/" + mode
166
+ "/" + pad + " , KeySize " + keyStrength);
167
throw ex;
168
}
169
out.println("Caught InvalidKeyException as expected");
170
}
171
}
172
}
173
174