Java Code Examples for java.security.InvalidKeyException.getMessage()

The following are Jave code examples for showing how to use getMessage() of the java.security.InvalidKeyException class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: openjdk-jdk10   File: DSAKeyFactory.java   View Source Code Vote up 6 votes
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
throws InvalidKeySpecException {
    try {
        if (keySpec instanceof DSAPrivateKeySpec) {
            DSAPrivateKeySpec dsaPrivKeySpec = (DSAPrivateKeySpec)keySpec;
            return new DSAPrivateKey(dsaPrivKeySpec.getX(),
                                     dsaPrivKeySpec.getP(),
                                     dsaPrivKeySpec.getQ(),
                                     dsaPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DSAPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification: " + e.getMessage());
    }
}
 
Example 2
Project: openjdk-jdk10   File: DESKeyFactory.java   View Source Code Vote up 6 votes
/**
 * Generates a <code>SecretKey</code> object from the provided key
 * specification (key material).
 *
 * @param keySpec the specification (key material) of the secret key
 *
 * @return the secret key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected SecretKey engineGenerateSecret(KeySpec keySpec)
    throws InvalidKeySpecException {

    try {
        if (keySpec instanceof DESKeySpec) {
            return new DESKey(((DESKeySpec)keySpec).getKey());
        }

        if (keySpec instanceof SecretKeySpec) {
            return new DESKey(((SecretKeySpec)keySpec).getEncoded());
        }

        throw new InvalidKeySpecException(
                "Inappropriate key specification");

    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}
 
Example 3
Project: openjdk-jdk10   File: DESedeKeyFactory.java   View Source Code Vote up 6 votes
/**
 * Generates a <code>SecretKey</code> object from the provided key
 * specification (key material).
 *
 * @param keySpec the specification (key material) of the secret key
 *
 * @return the secret key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected SecretKey engineGenerateSecret(KeySpec keySpec)
    throws InvalidKeySpecException {

    try {
        if (keySpec instanceof DESedeKeySpec) {
            return new DESedeKey(((DESedeKeySpec)keySpec).getKey());
        }
        if (keySpec instanceof SecretKeySpec) {
            return new DESedeKey(((SecretKeySpec)keySpec).getEncoded());

        }
        throw new InvalidKeySpecException
            ("Inappropriate key specification");
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}
 
Example 4
Project: OpenJSharp   File: DESKeyFactory.java   View Source Code Vote up 6 votes
/**
 * Generates a <code>SecretKey</code> object from the provided key
 * specification (key material).
 *
 * @param keySpec the specification (key material) of the secret key
 *
 * @return the secret key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected SecretKey engineGenerateSecret(KeySpec keySpec)
    throws InvalidKeySpecException {

    try {
        if (keySpec instanceof DESKeySpec) {
            return new DESKey(((DESKeySpec)keySpec).getKey());
        }

        if (keySpec instanceof SecretKeySpec) {
            return new DESKey(((SecretKeySpec)keySpec).getEncoded());
        }

        throw new InvalidKeySpecException(
                "Inappropriate key specification");

    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}
 
Example 5
Project: OpenJSharp   File: DESedeKeyFactory.java   View Source Code Vote up 6 votes
/**
 * Generates a <code>SecretKey</code> object from the provided key
 * specification (key material).
 *
 * @param keySpec the specification (key material) of the secret key
 *
 * @return the secret key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected SecretKey engineGenerateSecret(KeySpec keySpec)
    throws InvalidKeySpecException {

    try {
        if (keySpec instanceof DESedeKeySpec) {
            return new DESedeKey(((DESedeKeySpec)keySpec).getKey());
        }
        if (keySpec instanceof SecretKeySpec) {
            return new DESedeKey(((SecretKeySpec)keySpec).getEncoded());

        }
        throw new InvalidKeySpecException
            ("Inappropriate key specification");
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}
 
Example 6
Project: jdk8u-jdk   File: X509Key.java   View Source Code Vote up 6 votes
/**
 * Construct X.509 subject public key from a DER value.  If
 * the runtime environment is configured with a specific class for
 * this kind of key, a subclass is returned.  Otherwise, a generic
 * X509Key object is returned.
 *
 * <P>This mechanism gurantees that keys (and algorithms) may be
 * freely manipulated and transferred, without risk of losing
 * information.  Also, when a key (or algorithm) needs some special
 * handling, that specific need can be accomodated.
 *
 * @param in the DER-encoded SubjectPublicKeyInfo value
 * @exception IOException on data format errors
 */
public static PublicKey parse(DerValue in) throws IOException
{
    AlgorithmId     algorithm;
    PublicKey       subjectKey;

    if (in.tag != DerValue.tag_Sequence)
        throw new IOException("corrupt subject key");

    algorithm = AlgorithmId.parse(in.data.getDerValue());
    try {
        subjectKey = buildX509Key(algorithm,
                                  in.data.getUnalignedBitString());

    } catch (InvalidKeyException e) {
        throw new IOException("subject key, " + e.getMessage(), e);
    }

    if (in.data.available() != 0)
        throw new IOException("excess subject key");
    return subjectKey;
}
 
Example 7
Project: jdk8u-jdk   File: DSAKeyFactory.java   View Source Code Vote up 6 votes
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
throws InvalidKeySpecException {
    try {
        if (keySpec instanceof DSAPrivateKeySpec) {
            DSAPrivateKeySpec dsaPrivKeySpec = (DSAPrivateKeySpec)keySpec;
            return new DSAPrivateKey(dsaPrivKeySpec.getX(),
                                     dsaPrivKeySpec.getP(),
                                     dsaPrivKeySpec.getQ(),
                                     dsaPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DSAPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification: " + e.getMessage());
    }
}
 
Example 8
Project: jdk8u-jdk   File: DESedeKeyFactory.java   View Source Code Vote up 6 votes
/**
 * Generates a <code>SecretKey</code> object from the provided key
 * specification (key material).
 *
 * @param keySpec the specification (key material) of the secret key
 *
 * @return the secret key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected SecretKey engineGenerateSecret(KeySpec keySpec)
    throws InvalidKeySpecException {

    try {
        if (keySpec instanceof DESedeKeySpec) {
            return new DESedeKey(((DESedeKeySpec)keySpec).getKey());
        }
        if (keySpec instanceof SecretKeySpec) {
            return new DESedeKey(((SecretKeySpec)keySpec).getEncoded());

        }
        throw new InvalidKeySpecException
            ("Inappropriate key specification");
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}
 
Example 9
Project: openjdk-jdk10   File: X509Key.java   View Source Code Vote up 6 votes
/**
 * Construct X.509 subject public key from a DER value.  If
 * the runtime environment is configured with a specific class for
 * this kind of key, a subclass is returned.  Otherwise, a generic
 * X509Key object is returned.
 *
 * <P>This mechanism gurantees that keys (and algorithms) may be
 * freely manipulated and transferred, without risk of losing
 * information.  Also, when a key (or algorithm) needs some special
 * handling, that specific need can be accomodated.
 *
 * @param in the DER-encoded SubjectPublicKeyInfo value
 * @exception IOException on data format errors
 */
public static PublicKey parse(DerValue in) throws IOException
{
    AlgorithmId     algorithm;
    PublicKey       subjectKey;

    if (in.tag != DerValue.tag_Sequence)
        throw new IOException("corrupt subject key");

    algorithm = AlgorithmId.parse(in.data.getDerValue());
    try {
        subjectKey = buildX509Key(algorithm,
                                  in.data.getUnalignedBitString());

    } catch (InvalidKeyException e) {
        throw new IOException("subject key, " + e.getMessage(), e);
    }

    if (in.data.available() != 0)
        throw new IOException("excess subject key");
    return subjectKey;
}
 
Example 10
Project: openjdk-jdk10   File: DESedeKeyGenerator.java   View Source Code Vote up 5 votes
/**
 * Generates the Triple DES key.
 *
 * @return the new Triple DES key
 */
protected SecretKey engineGenerateKey() {
    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    byte[] rawkey = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];

    if (keysize == 168) {
        // 3 intermediate keys
        this.random.nextBytes(rawkey);

        // Do parity adjustment for each intermediate key
        DESKeyGenerator.setParityBit(rawkey, 0);
        DESKeyGenerator.setParityBit(rawkey, 8);
        DESKeyGenerator.setParityBit(rawkey, 16);
    } else {
        // 2 intermediate keys
        byte[] tmpkey = new byte[16];
        this.random.nextBytes(tmpkey);
        DESKeyGenerator.setParityBit(tmpkey, 0);
        DESKeyGenerator.setParityBit(tmpkey, 8);
        System.arraycopy(tmpkey, 0, rawkey, 0, tmpkey.length);
        // Copy the first 8 bytes into the last
        System.arraycopy(tmpkey, 0, rawkey, 16, 8);
        java.util.Arrays.fill(tmpkey, (byte)0x00);
    }

    DESedeKey desEdeKey = null;
    try {
        desEdeKey = new DESedeKey(rawkey);
    } catch (InvalidKeyException ike) {
        // this never happens
        throw new RuntimeException(ike.getMessage());
    }

    java.util.Arrays.fill(rawkey, (byte)0x00);

    return desEdeKey;
}
 
Example 11
Project: fernet-java8   File: Key.java   View Source Code Vote up 5 votes
/**
 * Generate an HMAC SHA-256 signature from the components of a Fernet token.
 *
 * @param version
 *            the Fernet version number
 * @param timestamp
 *            the seconds after the epoch that the token was generated
 * @param initializationVector
 *            the encryption and decryption initialization vector
 * @param cipherText
 *            the encrypted content of the token
 * @return the HMAC signature
 */
public byte[] sign(final byte version, final Instant timestamp, final IvParameterSpec initializationVector,
        final byte[] cipherText) {
    try (final ByteArrayOutputStream byteStream = new ByteArrayOutputStream(
            getTokenPrefixBytes() + cipherText.length)) {
        try (final DataOutputStream dataStream = new DataOutputStream(byteStream)) {
            dataStream.writeByte(version);
            dataStream.writeLong(timestamp.getEpochSecond());
            dataStream.write(initializationVector.getIV());
            dataStream.write(cipherText);

            try {
                final Mac mac = Mac.getInstance(getSigningAlgorithm());
                mac.init(getSigningKeySpec());
                return mac.doFinal(byteStream.toByteArray());
            } catch (final InvalidKeyException ike) {
                // this should not happen because we control the signing key
                // algorithm and pre-validate the length
                throw new RuntimeException("Unable to initialise HMAC with shared secret: " + ike.getMessage(),
                        ike);
            } catch (final NoSuchAlgorithmException nsae) {
                // this should not happen as implementors are required to
                // provide the HmacSHA256 algorithm.
                throw new RuntimeException(nsae.getMessage(), nsae);
            }
        }
    } catch (final IOException e) {
        // this should not happen as I/O is to memory only
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example 12
Project: OpenJSharp   File: X509Key.java   View Source Code Vote up 5 votes
/**
 * Serialization read ... X.509 keys serialize as
 * themselves, and they're parsed when they get read back.
 */
private void readObject(ObjectInputStream stream) throws IOException {
    try {
        decode(stream);
    } catch (InvalidKeyException e) {
        e.printStackTrace();
        throw new IOException("deserialized key is invalid: " +
                              e.getMessage());
    }
}
 
Example 13
Project: OpenJSharp   File: PKCS8Key.java   View Source Code Vote up 5 votes
/**
 * Serialization read ... PKCS#8 keys serialize as
 * themselves, and they're parsed when they get read back.
 */
private void readObject (ObjectInputStream stream)
throws IOException {

    try {
        decode(stream);

    } catch (InvalidKeyException e) {
        e.printStackTrace();
        throw new IOException("deserialized key is invalid: " +
                              e.getMessage());
    }
}
 
Example 14
Project: OpenJSharp   File: DESedeKeyGenerator.java   View Source Code Vote up 5 votes
/**
 * Generates the Triple DES key.
 *
 * @return the new Triple DES key
 */
protected SecretKey engineGenerateKey() {
    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    byte[] rawkey = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];

    if (keysize == 168) {
        // 3 intermediate keys
        this.random.nextBytes(rawkey);

        // Do parity adjustment for each intermediate key
        DESKeyGenerator.setParityBit(rawkey, 0);
        DESKeyGenerator.setParityBit(rawkey, 8);
        DESKeyGenerator.setParityBit(rawkey, 16);
    } else {
        // 2 intermediate keys
        byte[] tmpkey = new byte[16];
        this.random.nextBytes(tmpkey);
        DESKeyGenerator.setParityBit(tmpkey, 0);
        DESKeyGenerator.setParityBit(tmpkey, 8);
        System.arraycopy(tmpkey, 0, rawkey, 0, tmpkey.length);
        // Copy the first 8 bytes into the last
        System.arraycopy(tmpkey, 0, rawkey, 16, 8);
        java.util.Arrays.fill(tmpkey, (byte)0x00);
    }

    DESedeKey desEdeKey = null;
    try {
        desEdeKey = new DESedeKey(rawkey);
    } catch (InvalidKeyException ike) {
        // this never happens
        throw new RuntimeException(ike.getMessage());
    }

    java.util.Arrays.fill(rawkey, (byte)0x00);

    return desEdeKey;
}
 
Example 15
Project: jdk8u-jdk   File: X509Key.java   View Source Code Vote up 5 votes
/**
 * Serialization read ... X.509 keys serialize as
 * themselves, and they're parsed when they get read back.
 */
private void readObject(ObjectInputStream stream) throws IOException {
    try {
        decode(stream);
    } catch (InvalidKeyException e) {
        e.printStackTrace();
        throw new IOException("deserialized key is invalid: " +
                              e.getMessage());
    }
}
 
Example 16
Project: openjdk-jdk10   File: DSAKeyFactory.java   View Source Code Vote up 5 votes
/**
 * Generates a public key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the public key
 *
 * @return the public key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected PublicKey engineGeneratePublic(KeySpec keySpec)
throws InvalidKeySpecException {
    try {
        if (keySpec instanceof DSAPublicKeySpec) {
            DSAPublicKeySpec dsaPubKeySpec = (DSAPublicKeySpec)keySpec;
            if (SERIAL_INTEROP) {
                return new DSAPublicKey(dsaPubKeySpec.getY(),
                                    dsaPubKeySpec.getP(),
                                    dsaPubKeySpec.getQ(),
                                    dsaPubKeySpec.getG());
            } else {
                return new DSAPublicKeyImpl(dsaPubKeySpec.getY(),
                                    dsaPubKeySpec.getP(),
                                    dsaPubKeySpec.getQ(),
                                    dsaPubKeySpec.getG());
            }
        } else if (keySpec instanceof X509EncodedKeySpec) {
            if (SERIAL_INTEROP) {
                return new DSAPublicKey
                    (((X509EncodedKeySpec)keySpec).getEncoded());
            } else {
                return new DSAPublicKeyImpl
                    (((X509EncodedKeySpec)keySpec).getEncoded());
            }
        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification: " + e.getMessage());
    }
}
 
Example 17
Project: jdk8u-jdk   File: DSAKeyFactory.java   View Source Code Vote up 5 votes
/**
 * Generates a public key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the public key
 *
 * @return the public key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected PublicKey engineGeneratePublic(KeySpec keySpec)
throws InvalidKeySpecException {
    try {
        if (keySpec instanceof DSAPublicKeySpec) {
            DSAPublicKeySpec dsaPubKeySpec = (DSAPublicKeySpec)keySpec;
            if (SERIAL_INTEROP) {
                return new DSAPublicKey(dsaPubKeySpec.getY(),
                                    dsaPubKeySpec.getP(),
                                    dsaPubKeySpec.getQ(),
                                    dsaPubKeySpec.getG());
            } else {
                return new DSAPublicKeyImpl(dsaPubKeySpec.getY(),
                                    dsaPubKeySpec.getP(),
                                    dsaPubKeySpec.getQ(),
                                    dsaPubKeySpec.getG());
            }
        } else if (keySpec instanceof X509EncodedKeySpec) {
            if (SERIAL_INTEROP) {
                return new DSAPublicKey
                    (((X509EncodedKeySpec)keySpec).getEncoded());
            } else {
                return new DSAPublicKeyImpl
                    (((X509EncodedKeySpec)keySpec).getEncoded());
            }
        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification: " + e.getMessage());
    }
}
 
Example 18
Project: jdk8u-jdk   File: DESedeKeyGenerator.java   View Source Code Vote up 5 votes
/**
 * Generates the Triple DES key.
 *
 * @return the new Triple DES key
 */
protected SecretKey engineGenerateKey() {
    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    byte[] rawkey = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];

    if (keysize == 168) {
        // 3 intermediate keys
        this.random.nextBytes(rawkey);

        // Do parity adjustment for each intermediate key
        DESKeyGenerator.setParityBit(rawkey, 0);
        DESKeyGenerator.setParityBit(rawkey, 8);
        DESKeyGenerator.setParityBit(rawkey, 16);
    } else {
        // 2 intermediate keys
        byte[] tmpkey = new byte[16];
        this.random.nextBytes(tmpkey);
        DESKeyGenerator.setParityBit(tmpkey, 0);
        DESKeyGenerator.setParityBit(tmpkey, 8);
        System.arraycopy(tmpkey, 0, rawkey, 0, tmpkey.length);
        // Copy the first 8 bytes into the last
        System.arraycopy(tmpkey, 0, rawkey, 16, 8);
        java.util.Arrays.fill(tmpkey, (byte)0x00);
    }

    DESedeKey desEdeKey = null;
    try {
        desEdeKey = new DESedeKey(rawkey);
    } catch (InvalidKeyException ike) {
        // this never happens
        throw new RuntimeException(ike.getMessage());
    }

    java.util.Arrays.fill(rawkey, (byte)0x00);

    return desEdeKey;
}
 
Example 19
Project: openjdk-jdk10   File: PKCS8Key.java   View Source Code Vote up 5 votes
/**
 * Serialization read ... PKCS#8 keys serialize as
 * themselves, and they're parsed when they get read back.
 */
private void readObject (ObjectInputStream stream)
throws IOException {

    try {
        decode(stream);

    } catch (InvalidKeyException e) {
        e.printStackTrace();
        throw new IOException("deserialized key is invalid: " +
                              e.getMessage());
    }
}
 
Example 20
Project: openjdk-jdk10   File: X509Key.java   View Source Code Vote up 5 votes
/**
 * Serialization read ... X.509 keys serialize as
 * themselves, and they're parsed when they get read back.
 */
private void readObject(ObjectInputStream stream) throws IOException {
    try {
        decode(stream);
    } catch (InvalidKeyException e) {
        e.printStackTrace();
        throw new IOException("deserialized key is invalid: " +
                              e.getMessage());
    }
}