Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/CICO/PBEFunc/AESPBEWrapper.java
41162 views
1
/*
2
* Copyright (c) 2007, 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.AlgorithmParameters;
25
import java.security.GeneralSecurityException;
26
import java.security.Provider;
27
import java.security.Security;
28
import javax.crypto.SecretKey;
29
import javax.crypto.Cipher;
30
import javax.crypto.SecretKeyFactory;
31
import javax.crypto.spec.PBEKeySpec;
32
33
/**
34
* Wrapper class to test a given AES-based PBE algorithm.
35
*/
36
public class AESPBEWrapper extends AbstractPBEWrapper {
37
/**
38
* the algorithm parameters.
39
*/
40
private AlgorithmParameters pbeParams;
41
42
/**
43
* the encryption key.
44
*/
45
private final SecretKey key;
46
47
/**
48
* The Wrapper constructor. Instantiate Cipher using the given AES-based PBE
49
* algorithm.
50
*
51
* @param algo AES-based PBE algorithm.
52
* @param passwd password phrase.
53
* @throws GeneralSecurityException all security exceptions are thrown.
54
*/
55
public AESPBEWrapper(PBEAlgorithm algo, String passwd)
56
throws GeneralSecurityException {
57
// salt and iteration count will be generated during encryption
58
super(algo, passwd, 0);
59
60
// Generate secret key. We expect no mode and padding specified.
61
SecretKeyFactory skf = SecretKeyFactory.getInstance(algo.baseAlgo);
62
key = skf.generateSecret(new PBEKeySpec(passwd.toCharArray()));
63
}
64
65
/**
66
* Initiate the Cipher object using given "mode".
67
* @return a cipher object.
68
* @throws GeneralSecurityException all security exceptions are thrown.
69
*/
70
@Override
71
protected Cipher initCipher(int mode) throws GeneralSecurityException {
72
Provider provider = Security.getProvider("SunJCE");
73
if (provider == null) {
74
throw new RuntimeException("SunJCE provider does not exist.");
75
}
76
// get Cipher instance
77
Cipher ci = Cipher.getInstance(transformation, provider);
78
if (Cipher.ENCRYPT_MODE == mode) {
79
ci.init(Cipher.ENCRYPT_MODE, key);
80
pbeParams = ci.getParameters();
81
} else {
82
ci.init(Cipher.DECRYPT_MODE, key, pbeParams);
83
}
84
return ci;
85
}
86
}
87
88