Java Code Examples for java.security.spec.InvalidKeySpecException#getMessage()

The following examples show how to use java.security.spec.InvalidKeySpecException#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Client.java    From xipki with Apache License 2.0 6 votes vote down vote up
private EnrolmentResponse enroll(MessageType messageType, CertificationRequest csr,
    PrivateKey identityKey, X509Cert identityCert) throws ScepClientException {
  TransactionId tid;
  try {
    tid = TransactionId.sha1TransactionId(
        csr.getCertificationRequestInfo().getSubjectPublicKeyInfo());
  } catch (InvalidKeySpecException ex) {
    throw new ScepClientException(ex.getMessage(), ex);
  }
  PkiMessage pkiMessage = new PkiMessage(tid, messageType);

  pkiMessage.setMessageData(csr);
  ContentInfo envRequest = encryptThenSign(pkiMessage, identityKey, identityCert);
  ScepHttpResponse httpResp = httpSend(Operation.PKIOperation, envRequest);

  CMSSignedData cmsSignedData = parsePkiMessage(httpResp.getContentBytes());
  DecodedPkiMessage response = decode(cmsSignedData, identityKey, identityCert);
  assertSameNonce(pkiMessage, response);
  return new EnrolmentResponse(response);
}
 
Example 2
Source File: ProxyP11Slot.java    From xipki with Apache License 2.0 6 votes vote down vote up
private PublicKey getPublicKey(P11ObjectIdentifier objectId)
    throws P11UnknownEntityException, P11TokenException {
  ASN1Object req =
      new ProxyMessage.SlotIdAndObjectId(asn1SlotId, new ProxyMessage.ObjectIdentifier(objectId));
  byte[] resp = module.send(P11ProxyConstants.ACTION_GET_PUBLICKEY, req);
  if (resp == null) {
    return null;
  }

  SubjectPublicKeyInfo pkInfo = SubjectPublicKeyInfo.getInstance(resp);
  try {
    return KeyUtil.generatePublicKey(pkInfo);
  } catch (InvalidKeySpecException ex) {
    throw new P11TokenException("could not generate Public Key from SubjectPublicKeyInfo:"
        + ex.getMessage(), ex);
  }
}
 
Example 3
Source File: X509Cert.java    From xipki with Apache License 2.0 6 votes vote down vote up
public PublicKey getPublicKey() {
  if (publicKey == null) {
    synchronized (sync) {
      if (bcInstance != null) {
        try {
          this.publicKey = KeyUtil.generatePublicKey(bcInstance.getSubjectPublicKeyInfo());
        } catch (InvalidKeySpecException ex) {
          throw new IllegalStateException(ex.getMessage(), ex);
        }
      } else {
        publicKey = jceInstance.getPublicKey();
      }
    }
  }

  return publicKey;
}
 
Example 4
Source File: DSAKeyFactory.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key) throws InvalidKeyException {

    try {

        if (key instanceof java.security.interfaces.DSAPublicKey) {
            // Check if key originates from this factory
            if (key instanceof sun.security.provider.DSAPublicKey) {
                return key;
            }
            // Convert key to spec
            DSAPublicKeySpec dsaPubKeySpec
                = engineGetKeySpec(key, DSAPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dsaPubKeySpec);

        } else if (key instanceof java.security.interfaces.DSAPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof sun.security.provider.DSAPrivateKey) {
                return key;
            }
            // Convert key to spec
            DSAPrivateKeySpec dsaPrivKeySpec
                = engineGetKeySpec(key, DSAPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dsaPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key: "
                                      + e.getMessage());
    }
}
 
Example 5
Source File: DSAKeyFactory.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key) throws InvalidKeyException {

    try {

        if (key instanceof java.security.interfaces.DSAPublicKey) {
            // Check if key originates from this factory
            if (key instanceof sun.security.provider.DSAPublicKey) {
                return key;
            }
            // Convert key to spec
            DSAPublicKeySpec dsaPubKeySpec
                = engineGetKeySpec(key, DSAPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dsaPubKeySpec);

        } else if (key instanceof java.security.interfaces.DSAPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof sun.security.provider.DSAPrivateKey) {
                return key;
            }
            // Convert key to spec
            DSAPrivateKeySpec dsaPrivKeySpec
                = engineGetKeySpec(key, DSAPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dsaPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key: "
                                      + e.getMessage());
    }
}
 
Example 6
Source File: PBEKeyFactory.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a <code>SecretKey</code> object, whose provider may be
 * unknown or potentially untrusted, into a corresponding
 * <code>SecretKey</code> object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected SecretKey engineTranslateKey(SecretKey key)
    throws InvalidKeyException
{
    try {
        if ((key != null) &&
            (validTypes.contains(key.getAlgorithm().toUpperCase(Locale.ENGLISH))) &&
            (key.getFormat().equalsIgnoreCase("RAW"))) {

            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.PBEKey) {
                return key;
            }

            // Convert key to spec
            PBEKeySpec pbeKeySpec = (PBEKeySpec)engineGetKeySpec
                (key, PBEKeySpec.class);

            // Create key from spec, and return it
            return engineGenerateSecret(pbeKeySpec);
        } else {
            throw new InvalidKeyException("Invalid key format/algorithm");
        }

    } catch (InvalidKeySpecException ikse) {
        throw new InvalidKeyException("Cannot translate key: "
                                      + ikse.getMessage());
    }
}
 
Example 7
Source File: PBEKeyFactory.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a <code>SecretKey</code> object, whose provider may be
 * unknown or potentially untrusted, into a corresponding
 * <code>SecretKey</code> object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected SecretKey engineTranslateKey(SecretKey key)
    throws InvalidKeyException
{
    try {
        if ((key != null) &&
            (validTypes.contains(key.getAlgorithm().toUpperCase(Locale.ENGLISH))) &&
            (key.getFormat().equalsIgnoreCase("RAW"))) {

            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.PBEKey) {
                return key;
            }

            // Convert key to spec
            PBEKeySpec pbeKeySpec = (PBEKeySpec)engineGetKeySpec
                (key, PBEKeySpec.class);

            // Create key from spec, and return it
            return engineGenerateSecret(pbeKeySpec);
        } else {
            throw new InvalidKeyException("Invalid key format/algorithm");
        }

    } catch (InvalidKeySpecException ikse) {
        throw new InvalidKeyException("Cannot translate key: "
                                      + ikse.getMessage());
    }
}
 
Example 8
Source File: Client.java    From xipki with Apache License 2.0 5 votes vote down vote up
public EnrolmentResponse scepCertPoll(PrivateKey identityKey, X509Cert identityCert,
    CertificationRequest csr, X500Name issuer) throws ScepClientException {
  Args.notNull(csr, "csr");

  TransactionId tid;
  try {
    tid = TransactionId.sha1TransactionId(
        csr.getCertificationRequestInfo().getSubjectPublicKeyInfo());
  } catch (InvalidKeySpecException ex) {
    throw new ScepClientException(ex.getMessage(), ex);
  }

  return scepCertPoll(identityKey, identityCert, tid, issuer,
      csr.getCertificationRequestInfo().getSubject());
}
 
Example 9
Source File: PBEKeyFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a <code>SecretKey</code> object, whose provider may be
 * unknown or potentially untrusted, into a corresponding
 * <code>SecretKey</code> object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected SecretKey engineTranslateKey(SecretKey key)
    throws InvalidKeyException
{
    try {
        if ((key != null) &&
            (validTypes.contains(key.getAlgorithm().toUpperCase(Locale.ENGLISH))) &&
            (key.getFormat().equalsIgnoreCase("RAW"))) {

            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.PBEKey) {
                return key;
            }

            // Convert key to spec
            PBEKeySpec pbeKeySpec = (PBEKeySpec)engineGetKeySpec
                (key, PBEKeySpec.class);

            // Create key from spec, and return it
            return engineGenerateSecret(pbeKeySpec);
        } else {
            throw new InvalidKeyException("Invalid key format/algorithm");
        }

    } catch (InvalidKeySpecException ikse) {
        throw new InvalidKeyException("Cannot translate key: "
                                      + ikse.getMessage());
    }
}
 
Example 10
Source File: InvalidKeySpecExceptionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test for <code>InvalidKeySpecException(String, Throwable)</code>
 * constructor Assertion: constructs InvalidKeySpecException when
 * <code>cause</code> is not null <code>msg</code> is null
 */
public void testInvalidKeySpecException08() {
    InvalidKeySpecException tE = new InvalidKeySpecException(null, tCause);
    if (tE.getMessage() != null) {
        String toS = tCause.toString();
        String getM = tE.getMessage();
        assertTrue("getMessage() must should ".concat(toS), (getM
                .indexOf(toS) != -1));
    }
    assertNotNull("getCause() must not return null", tE.getCause());
    assertEquals("getCause() must return ".concat(tCause.toString()), tE
            .getCause(), tCause);
}
 
Example 11
Source File: PBEKeyFactory.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a <code>SecretKey</code> object, whose provider may be
 * unknown or potentially untrusted, into a corresponding
 * <code>SecretKey</code> object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected SecretKey engineTranslateKey(SecretKey key)
    throws InvalidKeyException
{
    try {
        if ((key != null) &&
            (validTypes.contains(key.getAlgorithm().toUpperCase(Locale.ENGLISH))) &&
            (key.getFormat().equalsIgnoreCase("RAW"))) {

            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.PBEKey) {
                return key;
            }

            // Convert key to spec
            PBEKeySpec pbeKeySpec = (PBEKeySpec)engineGetKeySpec
                (key, PBEKeySpec.class);

            // Create key from spec, and return it
            return engineGenerateSecret(pbeKeySpec);
        } else {
            throw new InvalidKeyException("Invalid key format/algorithm");
        }

    } catch (InvalidKeySpecException ikse) {
        throw new InvalidKeyException("Cannot translate key: "
                                      + ikse.getMessage());
    }
}
 
Example 12
Source File: InvalidKeySpecExceptionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test for <code>InvalidKeySpecException(Throwable)</code> constructor
 * Assertion: constructs InvalidKeySpecException when <code>cause</code>
 * is not null
 */
public void testInvalidKeySpecException05() {
    InvalidKeySpecException tE = new InvalidKeySpecException(tCause);
    if (tE.getMessage() != null) {
        String toS = tCause.toString();
        String getM = tE.getMessage();
        assertTrue("getMessage() should contain ".concat(toS), (getM
                .indexOf(toS) != -1));
    }
    assertNotNull("getCause() must not return null", tE.getCause());
    assertEquals("getCause() must return ".concat(tCause.toString()), tE
            .getCause(), tCause);
}
 
Example 13
Source File: PBEKeyFactory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a <code>SecretKey</code> object, whose provider may be
 * unknown or potentially untrusted, into a corresponding
 * <code>SecretKey</code> object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected SecretKey engineTranslateKey(SecretKey key)
    throws InvalidKeyException
{
    try {
        if ((key != null) &&
            (validTypes.contains(key.getAlgorithm().toUpperCase(Locale.ENGLISH))) &&
            (key.getFormat().equalsIgnoreCase("RAW"))) {

            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.PBEKey) {
                return key;
            }

            // Convert key to spec
            PBEKeySpec pbeKeySpec = (PBEKeySpec)engineGetKeySpec
                (key, PBEKeySpec.class);

            // Create key from spec, and return it
            return engineGenerateSecret(pbeKeySpec);
        } else {
            throw new InvalidKeyException("Invalid key format/algorithm");
        }

    } catch (InvalidKeySpecException ikse) {
        throw new InvalidKeyException("Cannot translate key: "
                                      + ikse.getMessage());
    }
}
 
Example 14
Source File: DSAKeyFactory.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key) throws InvalidKeyException {

    try {

        if (key instanceof java.security.interfaces.DSAPublicKey) {
            // Check if key originates from this factory
            if (key instanceof sun.security.provider.DSAPublicKey) {
                return key;
            }
            // Convert key to spec
            DSAPublicKeySpec dsaPubKeySpec
                = engineGetKeySpec(key, DSAPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dsaPubKeySpec);

        } else if (key instanceof java.security.interfaces.DSAPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof sun.security.provider.DSAPrivateKey) {
                return key;
            }
            // Convert key to spec
            DSAPrivateKeySpec dsaPrivKeySpec
                = engineGetKeySpec(key, DSAPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dsaPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key: "
                                      + e.getMessage());
    }
}
 
Example 15
Source File: PBEKeyFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a <code>SecretKey</code> object, whose provider may be
 * unknown or potentially untrusted, into a corresponding
 * <code>SecretKey</code> object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected SecretKey engineTranslateKey(SecretKey key)
    throws InvalidKeyException
{
    try {
        if ((key != null) &&
            (validTypes.contains(key.getAlgorithm().toUpperCase(Locale.ENGLISH))) &&
            (key.getFormat().equalsIgnoreCase("RAW"))) {

            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.PBEKey) {
                return key;
            }

            // Convert key to spec
            PBEKeySpec pbeKeySpec = (PBEKeySpec)engineGetKeySpec
                (key, PBEKeySpec.class);

            // Create key from spec, and return it
            return engineGenerateSecret(pbeKeySpec);
        } else {
            throw new InvalidKeyException("Invalid key format/algorithm");
        }

    } catch (InvalidKeySpecException ikse) {
        throw new InvalidKeyException("Cannot translate key: "
                                      + ikse.getMessage());
    }
}
 
Example 16
Source File: DSAKeyFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key) throws InvalidKeyException {

    try {

        if (key instanceof java.security.interfaces.DSAPublicKey) {
            // Check if key originates from this factory
            if (key instanceof sun.security.provider.DSAPublicKey) {
                return key;
            }
            // Convert key to spec
            DSAPublicKeySpec dsaPubKeySpec
                = engineGetKeySpec(key, DSAPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dsaPubKeySpec);

        } else if (key instanceof java.security.interfaces.DSAPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof sun.security.provider.DSAPrivateKey) {
                return key;
            }
            // Convert key to spec
            DSAPrivateKeySpec dsaPrivKeySpec
                = engineGetKeySpec(key, DSAPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dsaPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key: "
                                      + e.getMessage());
    }
}
 
Example 17
Source File: JKS.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public Key engineGetKey(String alias, char[] password)
    throws NoSuchAlgorithmException, UnrecoverableKeyException
{
    alias = alias.toLowerCase();

    if (!privateKeys.containsKey(alias))
        return null;
    byte[] key = decryptKey((byte[]) privateKeys.get(alias),
        charsToBytes(password));
    Certificate[] chain = engineGetCertificateChain(alias);
    if (chain.length > 0)
    {
        try
        {
            // Private and public keys MUST have the same algorithm.
            KeyFactory fact = KeyFactory.getInstance(
                chain[0].getPublicKey().getAlgorithm());
            return fact.generatePrivate(new PKCS8EncodedKeySpec(key));
        }
        catch (InvalidKeySpecException x)
        {
            throw new UnrecoverableKeyException(x.getMessage());
        }
    }
    else
        return new SecretKeySpec(key, alias);
}
 
Example 18
Source File: DSAKeyFactory.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key) throws InvalidKeyException {

    try {

        if (key instanceof java.security.interfaces.DSAPublicKey) {
            // Check if key originates from this factory
            if (key instanceof sun.security.provider.DSAPublicKey) {
                return key;
            }
            // Convert key to spec
            DSAPublicKeySpec dsaPubKeySpec
                = engineGetKeySpec(key, DSAPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dsaPubKeySpec);

        } else if (key instanceof java.security.interfaces.DSAPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof sun.security.provider.DSAPrivateKey) {
                return key;
            }
            // Convert key to spec
            DSAPrivateKeySpec dsaPrivKeySpec
                = engineGetKeySpec(key, DSAPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dsaPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key: "
                                      + e.getMessage());
    }
}
 
Example 19
Source File: PBEKeyFactory.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a <code>SecretKey</code> object, whose provider may be
 * unknown or potentially untrusted, into a corresponding
 * <code>SecretKey</code> object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected SecretKey engineTranslateKey(SecretKey key)
    throws InvalidKeyException
{
    try {
        if ((key != null) &&
            (validTypes.contains(key.getAlgorithm().toUpperCase(Locale.ENGLISH))) &&
            (key.getFormat().equalsIgnoreCase("RAW"))) {

            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.PBEKey) {
                return key;
            }

            // Convert key to spec
            PBEKeySpec pbeKeySpec = (PBEKeySpec)engineGetKeySpec
                (key, PBEKeySpec.class);

            // Create key from spec, and return it
            return engineGenerateSecret(pbeKeySpec);
        } else {
            throw new InvalidKeyException("Invalid key format/algorithm");
        }

    } catch (InvalidKeySpecException ikse) {
        throw new InvalidKeyException("Cannot translate key: "
                                      + ikse.getMessage());
    }
}
 
Example 20
Source File: DSAKeyFactory.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key) throws InvalidKeyException {

    try {

        if (key instanceof java.security.interfaces.DSAPublicKey) {
            // Check if key originates from this factory
            if (key instanceof sun.security.provider.DSAPublicKey) {
                return key;
            }
            // Convert key to spec
            DSAPublicKeySpec dsaPubKeySpec
                = engineGetKeySpec(key, DSAPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dsaPubKeySpec);

        } else if (key instanceof java.security.interfaces.DSAPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof sun.security.provider.DSAPrivateKey) {
                return key;
            }
            // Convert key to spec
            DSAPrivateKeySpec dsaPrivKeySpec
                = engineGetKeySpec(key, DSAPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dsaPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key: "
                                      + e.getMessage());
    }
}