0N/A/*
2362N/A * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 0000000
0N/A * @summary KeyWrapping
0N/A * @author Jan Luehe
0N/A */
0N/Aimport javax.crypto.*;
0N/Aimport java.security.*;
0N/A
0N/Apublic class KeyWrapping {
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A Cipher c1 = Cipher.getInstance("DES", "SunJCE");
0N/A Cipher c2 = Cipher.getInstance("DES");
0N/A
0N/A KeyGenerator keyGen = KeyGenerator.getInstance("DES");
0N/A keyGen.init(56);
0N/A
0N/A // Generate two DES keys: sKey and sessionKey
0N/A SecretKey sKey = keyGen.generateKey();
0N/A SecretKey sessionKey = keyGen.generateKey();
0N/A
0N/A // wrap and unwrap the session key
0N/A // make sure the unwrapped session key
0N/A // can decrypt a message encrypted
0N/A // with the session key
0N/A c1.init(Cipher.WRAP_MODE, sKey);
0N/A
0N/A byte[] wrappedKey = c1.wrap(sessionKey);
0N/A
0N/A c1.init(Cipher.UNWRAP_MODE, sKey);
0N/A
0N/A SecretKey unwrappedSessionKey =
0N/A (SecretKey)c1.unwrap(wrappedKey, "DES",
0N/A Cipher.SECRET_KEY);
0N/A
0N/A c2.init(Cipher.ENCRYPT_MODE, unwrappedSessionKey);
0N/A
0N/A String msg = "Hello";
0N/A
0N/A byte[] cipherText = c2.doFinal(msg.getBytes());
0N/A
0N/A c2.init(Cipher.DECRYPT_MODE, unwrappedSessionKey);
0N/A
0N/A byte[] clearText = c2.doFinal(cipherText);
0N/A
0N/A if (!msg.equals(new String(clearText)))
0N/A throw new Exception("The unwrapped session key is corrupted.");
0N/A
0N/A KeyPairGenerator kpairGen = KeyPairGenerator.getInstance("DSA");
0N/A kpairGen.initialize(1024);
0N/A
0N/A KeyPair kpair = kpairGen.genKeyPair();
0N/A
0N/A PublicKey pub = kpair.getPublic();
0N/A PrivateKey pri = kpair.getPrivate();
0N/A
0N/A c1.init(Cipher.WRAP_MODE, sKey);
0N/A
0N/A byte[] wrappedPub = c1.wrap(pub);
0N/A byte[] wrappedPri = c1.wrap(pri);
0N/A
0N/A c1.init(Cipher.UNWRAP_MODE, sKey);
0N/A
0N/A Key unwrappedPub = c1.unwrap(wrappedPub, "DSA",
0N/A Cipher.PUBLIC_KEY);
0N/A Key unwrappedPri = c1.unwrap(wrappedPri, "DSA",
0N/A Cipher.PRIVATE_KEY);
0N/A }
0N/A}