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/TestCipherTextLength.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.NoSuchAlgorithmException;
27
import java.security.spec.InvalidKeySpecException;
28
import java.util.Arrays;
29
import javax.crypto.Cipher;
30
import javax.crypto.NoSuchPaddingException;
31
32
/*
33
* @test
34
* @bug 8048601
35
* @summary Performs multiple-part encryption/decryption depending on the
36
* specified encryption mode and check if the results obtained by
37
* different ways are the same.
38
*/
39
public class TestCipherTextLength {
40
41
/* Algorithms tested by DESCipherWrapper */
42
private static final String[] DES_ALGORITHMS = {"DES", "DESede",
43
"Blowfish"};
44
private static final String[] DES_MODES = {"ECB", "CBC", "PCBC"};
45
private static final String[] DES_PADDING = {"PKCS5Padding"};
46
47
/* Algorithms tested by PBECipherWrapper */
48
private static final String[] PBE_ALGORITHMS = {"PBEWithMD5AndDES",
49
"PBEWithMD5AndDES/CBC/PKCS5Padding", "PBEWithMD5ANDTripleDES",
50
"PBEWithMD5AndTripleDES/CBC/PKCS5Padding", "PBEwithSHA1AndDESede",
51
"PBEwithSHA1AndDESede/CBC/PKCS5Padding", "PBEwithSHA1AndRC2_40",
52
"PBEwithSHA1Andrc2_40/CBC/PKCS5Padding", "PBEWithSHA1AndRC2_128",
53
"PBEWithSHA1andRC2_128/CBC/PKCS5Padding", "PBEWithSHA1AndRC4_40",
54
"PBEWithsha1AndRC4_40/ECB/NoPadding", "PBEWithSHA1AndRC4_128",
55
"PBEWithSHA1AndRC4_128/ECB/NoPadding", "PBEWithHmacSHA1AndAES_128",
56
"PBEWithHmacSHA224AndAES_128", "PBEWithHmacSHA256AndAES_128",
57
"PBEWithHmacSHA384AndAES_128", "PBEWithHmacSHA512AndAES_128",
58
"PBEWithHmacSHA1AndAES_256", "PBEWithHmacSHA224AndAES_256",
59
"PBEWithHmacSHA256AndAES_256", "PBEWithHmacSHA384AndAES_256",
60
"PBEWithHmacSHA512AndAES_256", "PBKDF2WithHmacSHA1",
61
"PBKDF2WithHmacSHA224", "PBKDF2WithHmacSHA256",
62
"PBKDF2WithHmacSHA384", "PBKDF2WithHmacSHA512"};
63
private static final String PBE_PASSWORD = "Hush, it's a secret!!";
64
65
// Algorithm tested by PBKDF2Wrappter
66
private static final String PBKDF2 = "PBKDF2";
67
68
// Algorithm tested by AESPBEWrapper
69
private static final String AES = "AES";
70
71
public static void main(String[] args) throws Exception {
72
byte[] plainText = new byte[64];
73
for (int i = 0; i < 64; i++) {
74
plainText[i] = (byte) (i & 0xff);
75
}
76
77
new TestCipherTextLength().runAll(plainText);
78
}
79
80
public void runAll(byte[] plainText) throws Exception {
81
82
// Testing DES/Blowfish Cipher
83
for (String algorithm : DES_ALGORITHMS) {
84
for (String desMode : DES_MODES) {
85
for (String padding : DES_PADDING) {
86
out.println("=>Testing: " + algorithm + "/" + desMode
87
+ "/" + padding);
88
DESCipherWrapper desCi = new DESCipherWrapper(algorithm,
89
desMode, padding);
90
desCi.execute(Cipher.ENCRYPT_MODE, plainText);
91
desCi.execute(Cipher.DECRYPT_MODE, desCi.getResult());
92
if (!Arrays.equals(plainText, desCi.getResult())) {
93
throw new RuntimeException(
94
"Plain and recovered texts are not same for:"
95
+ algorithm + "/" + desMode + "/"
96
+ padding);
97
}
98
}
99
}
100
}
101
102
// Testing PBE Cipher
103
for (String algorithm : PBE_ALGORITHMS) {
104
int maxKeyLen = Cipher.getMaxAllowedKeyLength(algorithm);
105
boolean isUnlimited = maxKeyLen == Integer.MAX_VALUE;
106
if (!isUnlimited
107
&& (algorithm.contains("TripleDES") || algorithm
108
.contains("AES_256"))) {
109
out.println("Test " + algorithm + " will be ignored");
110
continue;
111
}
112
113
out.println("=>Testing: " + algorithm);
114
PBECipherWrapper pbeCi = createWrapper(algorithm, PBE_PASSWORD);
115
pbeCi.execute(Cipher.ENCRYPT_MODE, plainText);
116
pbeCi.execute(Cipher.DECRYPT_MODE, pbeCi.getResult());
117
if (!Arrays.equals(plainText, pbeCi.getResult())) {
118
throw new RuntimeException(
119
"Plain and recovered texts are not same for:"
120
+ algorithm);
121
}
122
}
123
}
124
125
private PBECipherWrapper createWrapper(String algo, String passwd)
126
throws InvalidKeySpecException, NoSuchAlgorithmException,
127
NoSuchPaddingException {
128
if (algo.contains(PBKDF2)) {
129
return new PBECipherWrapper.PBKDF2(algo, passwd);
130
} else if (algo.contains(AES)) {
131
return new PBECipherWrapper.AES(algo, passwd);
132
} else {
133
return new PBECipherWrapper.Legacy(algo, passwd);
134
}
135
}
136
}
137
138