encodedkeyfactory.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 454 行 · 第 1/2 页
JAVA
454 行
try { result = GnuRSAPublicKey.valueOf(input); ok = true; } catch (InvalidParameterException ignored) { log.log(Level.FINE, "Exception in GnuRSAPublicKey.valueOf(). Ignore", ignored); } if (! ok) // try DH result = decodeDHPublicKey(input); } log.exiting(this.getClass().getName(), "engineGeneratePublic()", result); return result; } protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException { log.entering(this.getClass().getName(), "engineGeneratePrivate()", keySpec); PrivateKey result = null; if (keySpec instanceof DSAPrivateKeySpec) result = decodeDSSPrivateKey((DSAPrivateKeySpec) keySpec); else if (keySpec instanceof RSAPrivateCrtKeySpec) result = decodeRSAPrivateKey((RSAPrivateCrtKeySpec) keySpec); else if (keySpec instanceof DHPrivateKeySpec) result = decodeDHPrivateKey((DHPrivateKeySpec) keySpec); else { if (! (keySpec instanceof PKCS8EncodedKeySpec)) throw new InvalidKeySpecException("Unsupported key specification"); byte[] input = ((PKCS8EncodedKeySpec) keySpec).getEncoded(); boolean ok = false; // try DSS try { result = DSSPrivateKey.valueOf(input); ok = true; } catch (InvalidParameterException ignored) { log.log(Level.FINE, "Exception in DSSPrivateKey.valueOf(). Ignore", ignored); } if (! ok) // try RSA try { result = GnuRSAPrivateKey.valueOf(input); ok = true; } catch (InvalidParameterException ignored) { log.log(Level.FINE, "Exception in GnuRSAPrivateKey.valueOf(). Ignore", ignored); } if (! ok) // try DH result = decodeDHPrivateKey(input); } log.exiting(this.getClass().getName(), "engineGeneratePrivate()", result); return result; } protected KeySpec engineGetKeySpec(Key key, Class keySpec) throws InvalidKeySpecException { if (key instanceof PublicKey && Registry.X509_ENCODING_SORT_NAME.equalsIgnoreCase(key.getFormat()) && keySpec.isAssignableFrom(X509EncodedKeySpec.class)) return new X509EncodedKeySpec(key.getEncoded()); if (key instanceof PrivateKey && Registry.PKCS8_ENCODING_SHORT_NAME.equalsIgnoreCase(key.getFormat()) && keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class)) return new PKCS8EncodedKeySpec(key.getEncoded()); throw new InvalidKeySpecException("Unsupported format or invalid key spec class"); } protected Key engineTranslateKey(Key key) throws InvalidKeyException { throw new InvalidKeyException("Key translation not supported"); } /** * @param spec an instance of {@link DSAPublicKeySpec} to decode. * @return an instance of {@link DSSPublicKey} constructed from the * information in the designated key-specification. */ private DSSPublicKey decodeDSSPublicKey(DSAPublicKeySpec spec) { BigInteger p = spec.getP(); BigInteger q = spec.getQ(); BigInteger g = spec.getG(); BigInteger y = spec.getY(); return new DSSPublicKey(Registry.X509_ENCODING_ID, p, q, g, y); } /** * @param spec an instance of {@link RSAPublicKeySpec} to decode. * @return an instance of {@link GnuRSAPublicKey} constructed from the * information in the designated key-specification. */ private GnuRSAPublicKey decodeRSAPublicKey(RSAPublicKeySpec spec) { BigInteger n = spec.getModulus(); BigInteger e = spec.getPublicExponent(); return new GnuRSAPublicKey(Registry.X509_ENCODING_ID, n, e); } /** * @param spec an instance of {@link DHPublicKeySpec} to decode. * @return an instance of a {@link DHPublicKey} constructed from the * information in the designated key-specification. * @throws InvalidKeySpecException if no concrete implementation of the * {@link DHPublicKey} interface exists at run-time, or if an * exception occurs during its instantiation. */ private DHPublicKey decodeDHPublicKey(DHPublicKeySpec spec) throws InvalidKeySpecException { BigInteger p = spec.getP(); BigInteger g = spec.getG(); BigInteger y = spec.getY(); Object[] params = new Object[] {new Integer(Registry.X509_ENCODING_ID), null, p, g, y}; Object obj = invokeConstructor("gnu.javax.crypto.key.dh.GnuDHPublicKey", params); return (DHPublicKey) obj; } /** * @param encoded the bytes to decode. * @return an instance of a {@link DHPublicKey} constructed from the * information in the designated key-specification. * @throws InvalidKeySpecException if no concrete implementation of the * {@link DHPublicKey} interface exists at run-time, or if an * exception occurs during its instantiation. */ private DHPublicKey decodeDHPublicKey(byte[] encoded) throws InvalidKeySpecException { Object obj = invokeValueOf("gnu.javax.crypto.key.dh.GnuDHPublicKey", encoded); return (DHPublicKey) obj; } /** * @param spec an instance of {@link DSAPrivateKeySpec} to decode. * @return an instance of {@link DSSPrivateKey} constructed from the * information in the designated key-specification. */ private PrivateKey decodeDSSPrivateKey(DSAPrivateKeySpec spec) { BigInteger p = spec.getP(); BigInteger q = spec.getQ(); BigInteger g = spec.getG(); BigInteger x = spec.getX(); return new DSSPrivateKey(Registry.PKCS8_ENCODING_ID, p, q, g, x); } /** * @param spec an instance of {@link RSAPrivateCrtKeySpec} to decode. * @return an instance of {@link GnuRSAPrivateKey} constructed from the * information in the designated key-specification. */ private PrivateKey decodeRSAPrivateKey(RSAPrivateCrtKeySpec spec) { BigInteger n = spec.getModulus(); BigInteger e = spec.getPublicExponent(); BigInteger d = spec.getPrivateExponent(); BigInteger p = spec.getPrimeP(); BigInteger q = spec.getPrimeQ(); BigInteger dP = spec.getPrimeExponentP(); BigInteger dQ = spec.getPrimeExponentQ(); BigInteger qInv = spec.getCrtCoefficient(); return new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID, n, e, d, p, q, dP, dQ, qInv); } /** * @param spec an instance of {@link DHPrivateKeySpec} to decode. * @return an instance of a {@link DHPrivateKey} constructed from the * information in the designated key-specification. * @throws InvalidKeySpecException if no concrete implementation of the * {@link DHPrivateKey} interface exists at run-time, or if an * exception occurs during its instantiation. */ private DHPrivateKey decodeDHPrivateKey(DHPrivateKeySpec spec) throws InvalidKeySpecException { BigInteger p = spec.getP(); BigInteger g = spec.getG(); BigInteger x = spec.getX(); Object[] params = new Object[] {new Integer(Registry.PKCS8_ENCODING_ID), null, p, g, x}; Object obj = invokeConstructor("gnu.javax.crypto.key.dh.GnuDHPrivateKey", params); return (DHPrivateKey) obj; } /** * @param encoded the bytes to decode. * @return an instance of a {@link DHPrivateKey} constructed from the * information in the designated key-specification. * @throws InvalidKeySpecException if no concrete implementation of the * {@link DHPrivateKey} interface exists at run-time, or if an * exception occurs during its instantiation. */ private DHPrivateKey decodeDHPrivateKey(byte[] encoded) throws InvalidKeySpecException { Object obj = invokeValueOf("gnu.javax.crypto.key.dh.GnuDHPrivateKey", encoded); return (DHPrivateKey) obj; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?