Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/DES/PaddingTest.java
41161 views
1
/*
2
* Copyright (c) 1997, 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
/*
25
* @test
26
* @bug 0000000 6296075 6330275
27
* @summary PaddingTest
28
* @author Jan Luehe
29
*/
30
import java.io.*;
31
import java.nio.file.Files;
32
import java.nio.file.Paths;
33
import java.security.spec.*;
34
import javax.crypto.*;
35
import javax.crypto.spec.*;
36
import java.util.Arrays;
37
38
public class PaddingTest {
39
40
Cipher cipher;
41
IvParameterSpec params = null;
42
SecretKey cipherKey = null;
43
String pinfile = null;
44
String cfile = null;
45
String poutfile = null;
46
47
public static byte[] key = {
48
(byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,
49
(byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef
50
};
51
52
public static byte[] key3 = {
53
(byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,
54
(byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef,
55
(byte)0xf0,(byte)0xe1,(byte)0xd2,(byte)0xc3,
56
(byte)0xb4,(byte)0xa5,(byte)0x96,(byte)0x87,
57
(byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,
58
(byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};
59
60
public static byte[] iv = {
61
(byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,
62
(byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};
63
64
static String[] crypts = {"DES", "DESede"};
65
static String[] modes = {"ECB", "CBC", "CFB", "OFB", "PCBC"};
66
static String[] paddings = {"PKCS5Padding", "NoPadding"};
67
static int numFiles = 11;
68
static final String currDir = System.getProperty("test.src", ".");
69
static String dataDir = currDir + "/inputData/";
70
71
private String padding = null;
72
73
public static void main(String argv[]) throws Exception {
74
PaddingTest pt = new PaddingTest();
75
pt.run();
76
}
77
78
public PaddingTest() {
79
}
80
81
public void run() throws Exception {
82
83
for (int l=0; l<numFiles; l++) {
84
pinfile = dataDir + "plain" + l + ".txt";
85
for (int i=0; i<crypts.length; i++) {
86
for (int j=0; j<modes.length; j++) {
87
for (int k=0; k<paddings.length; k++) {
88
System.out.println
89
("===============================");
90
System.out.println
91
(crypts[i]+" "+modes[j]+" " + paddings[k]+ " " +
92
"plain" + l + " test");
93
cfile = "c" + l + "_" +
94
crypts[i] + "_" +
95
modes[j] + "_" +
96
paddings[k] + ".bin";
97
poutfile = "p" + l +
98
"_" + crypts[i] + modes[j] + paddings[k] + ".txt";
99
100
init(crypts[i], modes[j], paddings[k]);
101
padding = paddings[k];
102
runTest();
103
}
104
}
105
}
106
}
107
}
108
109
public void init(String crypt, String mode, String padding)
110
throws Exception {
111
112
KeySpec desKeySpec = null;
113
SecretKeyFactory factory = null;
114
115
StringBuffer cipherName = new StringBuffer(crypt);
116
if (mode.length() != 0)
117
cipherName.append("/" + mode);
118
if (padding.length() != 0)
119
cipherName.append("/" + padding);
120
121
cipher = Cipher.getInstance(cipherName.toString(), "SunJCE");
122
if (crypt.endsWith("ede")) {
123
desKeySpec = new DESedeKeySpec(key3);
124
factory = SecretKeyFactory.getInstance("DESede", "SunJCE");
125
} else {
126
desKeySpec = new DESKeySpec(key);
127
factory = SecretKeyFactory.getInstance("DES", "SunJCE");
128
}
129
130
// retrieve the cipher key
131
cipherKey = factory.generateSecret(desKeySpec);
132
133
// retrieve iv
134
if (!mode.equals("ECB"))
135
params = new IvParameterSpec(iv);
136
else
137
params = null;
138
}
139
140
public void runTest() throws Exception {
141
142
int bufferLen = 512;
143
byte[] input = new byte[bufferLen];
144
int len;
145
int totalInputLen = 0;
146
147
try {
148
try (FileInputStream fin = new FileInputStream(pinfile);
149
BufferedInputStream pin = new BufferedInputStream(fin);
150
FileOutputStream fout = new FileOutputStream(cfile);
151
BufferedOutputStream cout = new BufferedOutputStream(fout)) {
152
cipher.init(Cipher.ENCRYPT_MODE, cipherKey, params);
153
154
while ((len = pin.read(input, 0, bufferLen)) > 0) {
155
totalInputLen += len;
156
byte[] output = cipher.update(input, 0, len);
157
cout.write(output, 0, output.length);
158
}
159
160
len = cipher.getOutputSize(0);
161
162
byte[] out = new byte[len];
163
len = cipher.doFinal(out, 0);
164
cout.write(out, 0, len);
165
}
166
167
try (FileInputStream fin = new FileInputStream(cfile);
168
BufferedInputStream cin = new BufferedInputStream(fin);
169
FileOutputStream fout = new FileOutputStream(poutfile);
170
BufferedOutputStream pout = new BufferedOutputStream(fout)) {
171
cipher.init(Cipher.DECRYPT_MODE, cipherKey, params);
172
173
byte[] output = null;
174
while ((len = cin.read(input, 0, bufferLen)) > 0) {
175
output = cipher.update(input, 0, len);
176
pout.write(output, 0, output.length);
177
}
178
179
len = cipher.getOutputSize(0);
180
byte[] out = new byte[len];
181
len = cipher.doFinal(out, 0);
182
pout.write(out, 0, len);
183
}
184
185
diff(pinfile, poutfile);
186
} catch (IllegalBlockSizeException ex) {
187
if ((totalInputLen % 8 != 0) && (padding.equals("NoPadding"))) {
188
return;
189
} else {
190
System.out.println("Test failed!");
191
throw ex;
192
}
193
}
194
}
195
196
private static void diff(String fname1, String fname2) throws Exception {
197
if (!Arrays.equals(Files.readAllBytes(Paths.get(fname1)),
198
Files.readAllBytes(Paths.get(fname2)))) {
199
throw new Exception(
200
"files " + fname1 + " and " + fname2 + " differ");
201
}
202
}
203
}
204
205