encodedkeyfactory.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 454 行 · 第 1/2 页
JAVA
454 行
/* EncodedKeyFactory.java -- JCE Encoded key factory Adapter Copyright (C) 2006 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package gnu.java.security.jce.sig;import gnu.java.security.Registry;import gnu.java.security.key.dss.DSSPrivateKey;import gnu.java.security.key.dss.DSSPublicKey;import gnu.java.security.key.rsa.GnuRSAPrivateKey;import gnu.java.security.key.rsa.GnuRSAPublicKey;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.math.BigInteger;import java.security.InvalidKeyException;import java.security.InvalidParameterException;import java.security.Key;import java.security.KeyFactorySpi;import java.security.PrivateKey;import java.security.PublicKey;import java.security.spec.DSAPrivateKeySpec;import java.security.spec.DSAPublicKeySpec;import java.security.spec.InvalidKeySpecException;import java.security.spec.KeySpec;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.RSAPrivateCrtKeySpec;import java.security.spec.RSAPublicKeySpec;import java.security.spec.X509EncodedKeySpec;import java.util.logging.Level;import java.util.logging.Logger;import javax.crypto.interfaces.DHPrivateKey;import javax.crypto.interfaces.DHPublicKey;import javax.crypto.spec.DHPrivateKeySpec;import javax.crypto.spec.DHPublicKeySpec;/** * A factory for keys encoded in either the X.509 format (for public keys) or * the PKCS#8 format (for private keys). */public class EncodedKeyFactory extends KeyFactorySpi{ private static final Logger log = Logger.getLogger(EncodedKeyFactory.class.getName()); // implicit 0-arguments constructor // Class methods // -------------------------------------------------------------------------- private static Object invokeConstructor(String className, Object[] params) throws InvalidKeySpecException { Class clazz = getConcreteClass(className); try { Constructor ctor = getConcreteCtor(clazz); Object result = ctor.newInstance(params); return result; } catch (InstantiationException x) { InvalidKeySpecException y = new InvalidKeySpecException(); y.initCause(x); throw y; } catch (IllegalAccessException x) { InvalidKeySpecException y = new InvalidKeySpecException(); y.initCause(y); throw y; } catch (InvocationTargetException x) { InvalidKeySpecException y = new InvalidKeySpecException(); y.initCause(x); throw y; } } private static Class getConcreteClass(String className) throws InvalidKeySpecException { try { Class result = Class.forName(className); return result; } catch (ClassNotFoundException x) { InvalidKeySpecException y = new InvalidKeySpecException(); y.initCause(x); throw y; } } private static Constructor getConcreteCtor(Class clazz) throws InvalidKeySpecException { try { Constructor result = clazz.getConstructor(new Class[] {int.class, BigInteger.class, BigInteger.class, BigInteger.class, BigInteger.class}); return result; } catch (NoSuchMethodException x) { InvalidKeySpecException y = new InvalidKeySpecException(); y.initCause(x); throw y; } } private static Object invokeValueOf(String className, byte[] encoded) throws InvalidKeySpecException { Class clazz = getConcreteClass(className); try { Method valueOf = getValueOfMethod(clazz); Object result = valueOf.invoke(null, new Object[] { encoded }); return result; } catch (IllegalAccessException x) { InvalidKeySpecException y = new InvalidKeySpecException(); y.initCause(x); throw y; } catch (InvocationTargetException x) { InvalidKeySpecException y = new InvalidKeySpecException(); y.initCause(x); throw y; } } private static Method getValueOfMethod(Class clazz) throws InvalidKeySpecException { try { Method result = clazz.getMethod("valueOf", new Class[] {byte[].class}); return result; } catch (NoSuchMethodException x) { InvalidKeySpecException y = new InvalidKeySpecException(); y.initCause(x); throw y; } } // Instance methods // -------------------------------------------------------------------------- protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException { log.entering(this.getClass().getName(), "engineGeneratePublic()", keySpec); PublicKey result = null; if (keySpec instanceof DSAPublicKeySpec) result = decodeDSSPublicKey((DSAPublicKeySpec) keySpec); else if (keySpec instanceof RSAPublicKeySpec) result = decodeRSAPublicKey((RSAPublicKeySpec) keySpec); else if (keySpec instanceof DHPublicKeySpec) result = decodeDHPublicKey((DHPublicKeySpec) keySpec); else { if (! (keySpec instanceof X509EncodedKeySpec)) throw new InvalidKeySpecException("Unsupported key specification"); byte[] input = ((X509EncodedKeySpec) keySpec).getEncoded(); boolean ok = false; // try DSS try { result = DSSPublicKey.valueOf(input); ok = true; } catch (InvalidParameterException ignored) { log.log(Level.FINE, "Exception in DSSPublicKey.valueOf(). Ignore", ignored); } if (! ok) // try RSA
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?