|
DES1KeySecretKeyFactoryImpl |
|
1 /* $RCSfile: DES1KeySecretKeyFactoryImpl.java,v $ 2 * $Revision: 1.3 $ 3 * $Date: 2002/11/23 11:09:56 $ 4 * $Author: uwe_guenther $ 5 * $State: Exp $ 6 * 7 * Created on August 13, 2001 9:45 PM 8 * 9 * Copyright (C) 2001 Uwe Guenther <uwe@cscc.de> 10 * 11 * This file is part of the jhbci JCE-ServiceProvider. The jhbci JCE- 12 * ServiceProvider is a library, written in JavaTM, that should be 13 * used in HBCI banking applications (clients and may be servers), 14 * to do cryptographic operations. 15 * 16 * The jhbci library is free software; you can redistribute it and/or 17 * modify it under the terms of the GNU Lesser General Public 18 * License as published by the Free Software Foundation; either 19 * version 2.1 of the License, or (at your option) any later version. 20 * 21 * The jhbci library is distributed in the hope that it will be useful, 22 * but WITHOUT ANY WARRANTY; without even the implied warranty of 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 * Lesser General Public License for more details. 25 * 26 * You should have received a copy of the GNU Lesser General Public 27 * License along with this library; if not, write to the Free Software 28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 29 * 30 */ 31 32 package de.cscc.crypto.provider; 33 34 import java.security.InvalidKeyException; 35 import java.security.spec.InvalidKeySpecException; 36 import java.security.spec.KeySpec; 37 38 import javax.crypto.SecretKey; 39 40 import de.cscc.crypto.provider.spec.DES1KeySpec; 41 42 43 /** 44 * DES1KeySecretKeyFactoryImpl Class. 45 * 46 * @author <a href=mailto:uwe@cscc.de>Uwe Günther</a> 47 * @version $Revision: 1.3 $ 48 */ 49 final class DES1KeySecretKeyFactoryImpl { 50 51 /** Creates new DES1KeySecretKeyFactoryImpl.*/ 52 DES1KeySecretKeyFactoryImpl() { 53 } 54 55 /** 56 * Returns a string representation of the object. 57 * 58 * @return a string representation of the object. 59 */ 60 public String toString() { 61 62 return "[DES1KeySecretKeyFactory]"; 63 } 64 65 /** 66 * Generates a <code>SecretKey</code> object from the 67 * provided key specification (key material). 68 * 69 * @param keySpec the specification (key material) of the secret key. 70 * @return the secret key. 71 * @throws NullPointerException if keySpec is null. 72 * @throws InvalidKeySpecException if the given key specification 73 * is inappropriate for this secret-key factory to produce a secret key. 74 */ 75 SecretKey generateSecret(KeySpec keySpec) throws InvalidKeySpecException { 76 if (keySpec == null) { 77 throw new NullPointerException("Parameter keySpec is null."); 78 } 79 if (keySpec instanceof DES1KeySpec) { 80 //Downcast the KeySpec to a congret DES1KeySpec. 81 DES1KeySpec spec = (DES1KeySpec)keySpec; 82 //Construct and return to an SecretKey upcasted DES1KeySecretKeyImpl. 83 return /*(SecretKey)*/ new DES1KeySecretKeyImpl(spec); 84 85 } else { 86 //If the KeySpec is any other type than DES1KeySpec 87 //throw a InvalidKeySpecException 88 throw new InvalidKeySpecException("Inapproptiate KeySpec." 89 + "KeySpec have to be a instance of DES1KeySpec"); 90 } 91 92 } 93 94 /** 95 * Returns a specification (key material) of the given key object in the 96 * requested format. 97 * 98 * @param key the key. 99 * @param keySpec the requested format in which the key material shall be 100 * returned. 101 * @return the underlying key specification (key material) in the requested 102 * format. 103 * @throws NullPointerException if key or keySpec is null. 104 * @throws InvalidKeySpecException if the requested key specification is 105 * inappropriate for the given key (e.g., the algorithms associated with 106 * <code>key</code> and <code>keySpec</code> do not match, or 107 * <code>key</code> references a key on a cryptographic hardware device 108 * whereas <code>keySpec</code> is the specification of a software-based 109 * key), or the given key cannot be dealt with (e.g., the given key has 110 * an algorithm or format not supported by this secret-key factory). 111 */ 112 KeySpec getKeySpec(SecretKey key, Class keySpec) 113 throws InvalidKeySpecException { 114 if (key == null) { 115 throw new NullPointerException("Parameter key is null."); 116 } 117 if (keySpec == null) { 118 throw new NullPointerException("Parameter keySpec is null."); 119 } 120 if (key.getAlgorithm().equalsIgnoreCase("DES1Key") && 121 key.getFormat().equalsIgnoreCase("RAW")) { 122 123 if (keySpec == DES1KeySpec.class) { 124 DES1KeySpec spec = null; 125 try{ 126 spec = new DES1KeySpec(key.getEncoded()); 127 } catch (InvalidKeyException e) { 128 //this should never happends, because secretKey.getEncoded() 129 //should always return a byte[16]. 130 throw (InvalidKeySpecException) new InvalidKeySpecException( 131 "Inapproptiate SecretKey.").initCause(e); 132 133 } 134 //Return to an KeySpec upcasted DES1KeySpec. 135 return spec; 136 137 } else { 138 //If Class keySpec is any other type than DES1KeySpec, 139 //throw a InvalidKeySpecException. 140 throw new InvalidKeySpecException("Inapproptiate KeySpec." 141 + "KeySpec have to be DES1KeySpec.class"); 142 } 143 144 } else { 145 //If the SecretKey key is any other type than DES1KeySecretKey, 146 //throw a InvalidKeySpecException. 147 throw new InvalidKeySpecException("Inapproptiate SecretKey." 148 + "SecretKey must have Algorithm=\"DES1Key\" and " 149 + "Format=\"RAW\"."); 150 } 151 152 } 153 154 /** 155 * Translates a key object, whose provider may be unknown or potentially 156 * untrusted, into a corresponding key object of this secret-key factory. 157 * 158 * @return they key whose provider is unknown or untrusted. 159 * @param key the key whose provider is unknown or untrusted. 160 * @throws NullPointerException if key is null. 161 * @throws InvalidKeyException if the given key cannot be processed by this 162 * secret-key factory. 163 */ 164 SecretKey translateKey(SecretKey key) throws InvalidKeyException { 165 if (key == null) { 166 throw new NullPointerException("Parameter key is null."); 167 } 168 if (key.getAlgorithm().equalsIgnoreCase("DES1Key") && 169 key.getFormat().equalsIgnoreCase("RAW")) { 170 //Throws also InvalidKeyException, if key.getEncoded() 171 //returns a byte array, with a byte length shorter than 16 bytes. 172 DES1KeySpec spec = new DES1KeySpec(key.getEncoded()); 173 //Construct and return to an SecretKey upcasted DES1KeySecretKeyImpl. 174 return new DES1KeySecretKeyImpl(spec); 175 176 } else { 177 throw new InvalidKeyException("Inapproptiate SecretKey. SecretKey " 178 + "must have Algorithm=\"DES1Key\" and Format=\"RAW\"."); 179 } 180 } 181 } 182
|
DES1KeySecretKeyFactoryImpl |
|