Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/Cipher/DES/Sealtest.java
38889 views
1
/*
2
* Copyright (c) 1998, 2011, 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 7055362
27
* @summary Sealtest
28
* @author Jan Luehe
29
*/
30
import java.io.*;
31
import java.security.*;
32
import javax.crypto.*;
33
34
public class Sealtest {
35
36
public static void main(String[] args) throws Exception {
37
38
Security.addProvider(new com.sun.crypto.provider.SunJCE());
39
40
// create DSA keypair
41
KeyPairGenerator kpgen = KeyPairGenerator.getInstance("DSA");
42
kpgen.initialize(512);
43
KeyPair kp = kpgen.generateKeyPair();
44
45
// create DES key
46
KeyGenerator kg = KeyGenerator.getInstance("DES");
47
SecretKey skey = kg.generateKey();
48
49
// create cipher
50
Cipher c = Cipher.getInstance("DES/CFB16/PKCS5Padding");
51
c.init(Cipher.ENCRYPT_MODE, skey);
52
53
// seal the DSA private key
54
SealedObject sealed = new SealedObject(kp.getPrivate(), c);
55
56
// serialize
57
try (FileOutputStream fos = new FileOutputStream("sealed");
58
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
59
oos.writeObject(sealed);
60
}
61
62
// deserialize
63
try (FileInputStream fis = new FileInputStream("sealed");
64
ObjectInputStream ois = new ObjectInputStream(fis)) {
65
sealed = (SealedObject)ois.readObject();
66
}
67
68
System.out.println(sealed.getAlgorithm());
69
70
// compare unsealed private key with original
71
PrivateKey priv = (PrivateKey)sealed.getObject(skey);
72
if (!priv.equals(kp.getPrivate()))
73
throw new Exception("TEST FAILED");
74
75
System.out.println("TEST SUCCEEDED");
76
}
77
}
78
79