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/DesAPITest.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
27
* @summary DesAPITest
28
* @author Jan Luehe
29
*/
30
import java.io.*;
31
import java.security.*;
32
import java.security.spec.*;
33
import javax.crypto.*;
34
import javax.crypto.spec.*;
35
36
public class DesAPITest {
37
38
Cipher cipher;
39
IvParameterSpec params = null;
40
SecretKey cipherKey = null;
41
42
public static byte[] key = {
43
(byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,
44
(byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef
45
};
46
47
public static byte[] key3 = {
48
(byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,
49
(byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef,
50
(byte)0xf0,(byte)0xe1,(byte)0xd2,(byte)0xc3,
51
(byte)0xb4,(byte)0xa5,(byte)0x96,(byte)0x87,
52
(byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,
53
(byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};
54
55
public static byte[] iv = {
56
(byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,
57
(byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};
58
59
static String[] crypts = {"DES", "DESede"};
60
//static String[] modes = {"ECB", "CBC", "CFB", "OFB", "PCBC"};
61
static String[] modes = {"CFB24"};
62
//static String[] paddings = {"PKCS5Padding", "NoPadding"};
63
static String[] paddings = {"PKCS5Padding"};
64
65
public static void main(String[] args) throws Exception {
66
DesAPITest test = new DesAPITest();
67
test.run();
68
}
69
70
public void run() throws Exception {
71
72
for (int i=0; i<crypts.length; i++) {
73
for (int j=0; j<modes.length; j++) {
74
for (int k=0; k<paddings.length; k++) {
75
System.out.println
76
("===============================");
77
System.out.println
78
(crypts[i]+" "+modes[j]+" " + paddings[k]);
79
init(crypts[i], modes[j], paddings[k]);
80
runTest();
81
}
82
}
83
}
84
}
85
86
public void init(String crypt, String mode, String padding)
87
throws Exception {
88
89
KeySpec desKeySpec = null;
90
SecretKeyFactory factory = null;
91
92
StringBuffer cipherName = new StringBuffer(crypt);
93
if (mode.length() != 0)
94
cipherName.append("/" + mode);
95
if (padding.length() != 0)
96
cipherName.append("/" + padding);
97
98
cipher = Cipher.getInstance(cipherName.toString(), "SunJCE");
99
if (crypt.endsWith("ede")) {
100
desKeySpec = new DESedeKeySpec(key3);
101
factory = SecretKeyFactory.getInstance("DESede", "SunJCE");
102
}
103
else {
104
desKeySpec = new DESKeySpec(key);
105
factory = SecretKeyFactory.getInstance("DES", "SunJCE");
106
}
107
108
// retrieve the cipher key
109
cipherKey = factory.generateSecret(desKeySpec);
110
111
// retrieve iv
112
if ( !mode.equals("ECB"))
113
params = new IvParameterSpec(iv);
114
else
115
params = null;
116
}
117
118
public void runTest() throws Exception {
119
120
int bufferLen = 512;
121
byte[] input = new byte[bufferLen];
122
int len;
123
124
// encrypt test
125
cipher.init(Cipher.ENCRYPT_MODE, cipherKey, params);
126
127
// getIV
128
System.out.println("getIV, " + cipher.getIV());
129
byte[] output = null;
130
boolean thrown = false;
131
try {
132
input = null;
133
output = cipher.update(input, 0, -1);
134
} catch (IllegalArgumentException ex) {
135
thrown = true;
136
}
137
if (!thrown) {
138
throw new Exception("Expected IAE not thrown!");
139
}
140
byte[] inbuf = "itaoti7890123456".getBytes();
141
System.out.println("inputLength: " + inbuf.length);
142
output = cipher.update(inbuf);
143
144
len = cipher.getOutputSize(16);
145
byte[] out = new byte[len];
146
output = cipher.doFinal();
147
System.out.println(len + " " + TestUtility.hexDump(output));
148
}
149
}
150
151