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/KeyWrapping.java
41161 views
1
/*
2
* Copyright (c) 1999, 2007, 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 KeyWrapping
28
* @author Jan Luehe
29
*/
30
import javax.crypto.*;
31
import java.security.*;
32
33
public class KeyWrapping {
34
35
public static void main(String[] args) throws Exception {
36
Cipher c1 = Cipher.getInstance("DES", "SunJCE");
37
Cipher c2 = Cipher.getInstance("DES");
38
39
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
40
keyGen.init(56);
41
42
// Generate two DES keys: sKey and sessionKey
43
SecretKey sKey = keyGen.generateKey();
44
SecretKey sessionKey = keyGen.generateKey();
45
46
// wrap and unwrap the session key
47
// make sure the unwrapped session key
48
// can decrypt a message encrypted
49
// with the session key
50
c1.init(Cipher.WRAP_MODE, sKey);
51
52
byte[] wrappedKey = c1.wrap(sessionKey);
53
54
c1.init(Cipher.UNWRAP_MODE, sKey);
55
56
SecretKey unwrappedSessionKey =
57
(SecretKey)c1.unwrap(wrappedKey, "DES",
58
Cipher.SECRET_KEY);
59
60
c2.init(Cipher.ENCRYPT_MODE, unwrappedSessionKey);
61
62
String msg = "Hello";
63
64
byte[] cipherText = c2.doFinal(msg.getBytes());
65
66
c2.init(Cipher.DECRYPT_MODE, unwrappedSessionKey);
67
68
byte[] clearText = c2.doFinal(cipherText);
69
70
if (!msg.equals(new String(clearText)))
71
throw new Exception("The unwrapped session key is corrupted.");
72
73
KeyPairGenerator kpairGen = KeyPairGenerator.getInstance("DSA");
74
kpairGen.initialize(1024);
75
76
KeyPair kpair = kpairGen.genKeyPair();
77
78
PublicKey pub = kpair.getPublic();
79
PrivateKey pri = kpair.getPrivate();
80
81
c1.init(Cipher.WRAP_MODE, sKey);
82
83
byte[] wrappedPub = c1.wrap(pub);
84
byte[] wrappedPri = c1.wrap(pri);
85
86
c1.init(Cipher.UNWRAP_MODE, sKey);
87
88
Key unwrappedPub = c1.unwrap(wrappedPub, "DSA",
89
Cipher.PUBLIC_KEY);
90
Key unwrappedPri = c1.unwrap(wrappedPri, "DSA",
91
Cipher.PRIVATE_KEY);
92
}
93
}
94
95