Java Code Examples for javax.crypto.Cipher#SECRET_KEY

The following examples show how to use javax.crypto.Cipher#SECRET_KEY . 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: ConstructKeys.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
static final Key constructKey(byte[] encoding, String keyAlgorithm,
                              int keyType)
    throws InvalidKeyException, NoSuchAlgorithmException {
    Key result = null;
    switch (keyType) {
    case Cipher.SECRET_KEY:
        result = ConstructKeys.constructSecretKey(encoding,
                                                  keyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = ConstructKeys.constructPrivateKey(encoding,
                                                   keyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = ConstructKeys.constructPublicKey(encoding,
                                                  keyAlgorithm);
        break;
    }
    return result;
}
 
Example 2
Source File: ConstructKeys.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static final Key constructKey(byte[] encoding, String keyAlgorithm,
                              int keyType)
    throws InvalidKeyException, NoSuchAlgorithmException {
    Key result = null;
    switch (keyType) {
    case Cipher.SECRET_KEY:
        result = ConstructKeys.constructSecretKey(encoding,
                                                  keyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = ConstructKeys.constructPrivateKey(encoding,
                                                   keyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = ConstructKeys.constructPublicKey(encoding,
                                                  keyAlgorithm);
        break;
    }
    return result;
}
 
Example 3
Source File: ConstructKeys.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
static final Key constructKey(byte[] encoding, String keyAlgorithm,
                              int keyType)
    throws InvalidKeyException, NoSuchAlgorithmException {
    Key result = null;
    switch (keyType) {
    case Cipher.SECRET_KEY:
        result = ConstructKeys.constructSecretKey(encoding,
                                                  keyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = ConstructKeys.constructPrivateKey(encoding,
                                                   keyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = ConstructKeys.constructPublicKey(encoding,
                                                  keyAlgorithm);
        break;
    }
    return result;
}
 
Example 4
Source File: ConstructKeys.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
static final Key constructKey(byte[] encoding, String keyAlgorithm,
                              int keyType)
    throws InvalidKeyException, NoSuchAlgorithmException {
    Key result = null;
    switch (keyType) {
    case Cipher.SECRET_KEY:
        result = ConstructKeys.constructSecretKey(encoding,
                                                  keyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = ConstructKeys.constructPrivateKey(encoding,
                                                   keyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = ConstructKeys.constructPublicKey(encoding,
                                                  keyAlgorithm);
        break;
    }
    return result;
}
 
Example 5
Source File: ConstructKeys.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
static final Key constructKey(byte[] encoding, String keyAlgorithm,
                              int keyType)
    throws InvalidKeyException, NoSuchAlgorithmException {
    Key result = null;
    switch (keyType) {
    case Cipher.SECRET_KEY:
        result = ConstructKeys.constructSecretKey(encoding,
                                                  keyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = ConstructKeys.constructPrivateKey(encoding,
                                                   keyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = ConstructKeys.constructPublicKey(encoding,
                                                  keyAlgorithm);
        break;
    }
    return result;
}
 
Example 6
Source File: ConstructKeys.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
static final Key constructKey(byte[] encoding, String keyAlgorithm,
                              int keyType)
    throws InvalidKeyException, NoSuchAlgorithmException {
    Key result = null;
    switch (keyType) {
    case Cipher.SECRET_KEY:
        result = ConstructKeys.constructSecretKey(encoding,
                                                  keyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = ConstructKeys.constructPrivateKey(encoding,
                                                   keyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = ConstructKeys.constructPublicKey(encoding,
                                                  keyAlgorithm);
        break;
    }
    return result;
}
 
Example 7
Source File: ConstructKeys.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
static final Key constructKey(byte[] encoding, String keyAlgorithm,
                              int keyType)
    throws InvalidKeyException, NoSuchAlgorithmException {
    Key result = null;
    switch (keyType) {
    case Cipher.SECRET_KEY:
        result = ConstructKeys.constructSecretKey(encoding,
                                                  keyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = ConstructKeys.constructPrivateKey(encoding,
                                                   keyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = ConstructKeys.constructPublicKey(encoding,
                                                  keyAlgorithm);
        break;
    }
    return result;
}
 
Example 8
Source File: ConstructKeys.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
static final Key constructKey(byte[] encoding, String keyAlgorithm,
                              int keyType)
    throws InvalidKeyException, NoSuchAlgorithmException {
    Key result = null;
    switch (keyType) {
    case Cipher.SECRET_KEY:
        result = ConstructKeys.constructSecretKey(encoding,
                                                  keyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = ConstructKeys.constructPrivateKey(encoding,
                                                   keyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = ConstructKeys.constructPublicKey(encoding,
                                                  keyAlgorithm);
        break;
    }
    return result;
}
 
Example 9
Source File: ConstructKeys.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static final Key constructKey(byte[] encoding, String keyAlgorithm,
                              int keyType)
    throws InvalidKeyException, NoSuchAlgorithmException {
    Key result = null;
    switch (keyType) {
    case Cipher.SECRET_KEY:
        result = ConstructKeys.constructSecretKey(encoding,
                                                  keyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = ConstructKeys.constructPrivateKey(encoding,
                                                   keyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = ConstructKeys.constructPublicKey(encoding,
                                                  keyAlgorithm);
        break;
    }
    return result;
}
 
Example 10
Source File: ConstructKeys.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
static final Key constructKey(byte[] encoding, String keyAlgorithm,
                              int keyType)
    throws InvalidKeyException, NoSuchAlgorithmException {
    Key result = null;
    switch (keyType) {
    case Cipher.SECRET_KEY:
        result = ConstructKeys.constructSecretKey(encoding,
                                                  keyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = ConstructKeys.constructPrivateKey(encoding,
                                                   keyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = ConstructKeys.constructPublicKey(encoding,
                                                  keyAlgorithm);
        break;
    }
    return result;
}
 
Example 11
Source File: CipherWithWrappingSpi.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unwrap a previously wrapped key.
 *
 * @param wrappedKey the key to be unwrapped.
 *
 * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
 *
 * @param wrappedKeyType the type of the wrapped key.
 * This is one of <code>Cipher.SECRET_KEY</code>,
 * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
 *
 * @return the unwrapped key.
 *
 * @exception InvalidKeyException if <code>wrappedKey</code> does not
 * represent a wrapped key, or if the algorithm associated with the
 * wrapped key is different from <code>wrappedKeyAlgorithm</code>
 * and/or its key type is different from <code>wrappedKeyType</code>.
 *
 * @exception NoSuchAlgorithmException if no installed providers
 * can create keys for the <code>wrappedKeyAlgorithm</code>.
 */
protected final Key engineUnwrap(byte[] wrappedKey,
                                 String wrappedKeyAlgorithm,
                                 int wrappedKeyType)
    throws InvalidKeyException, NoSuchAlgorithmException
{
    byte[] encodedKey;
    Key result = null;

    try {
        encodedKey = engineDoFinal(wrappedKey, 0,
                                   wrappedKey.length);
    } catch (BadPaddingException ePadding) {
        throw new InvalidKeyException();
    } catch (IllegalBlockSizeException eBlockSize) {
        throw new InvalidKeyException();
    }

    switch (wrappedKeyType) {
    case Cipher.SECRET_KEY:
        result = constructSecretKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = constructPrivateKey(encodedKey,
                                     wrappedKeyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = constructPublicKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    }

    return result;

}
 
Example 12
Source File: CipherWithWrappingSpi.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unwrap a previously wrapped key.
 *
 * @param wrappedKey the key to be unwrapped.
 *
 * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
 *
 * @param wrappedKeyType the type of the wrapped key.
 * This is one of <code>Cipher.SECRET_KEY</code>,
 * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
 *
 * @return the unwrapped key.
 *
 * @exception InvalidKeyException if <code>wrappedKey</code> does not
 * represent a wrapped key, or if the algorithm associated with the
 * wrapped key is different from <code>wrappedKeyAlgorithm</code>
 * and/or its key type is different from <code>wrappedKeyType</code>.
 *
 * @exception NoSuchAlgorithmException if no installed providers
 * can create keys for the <code>wrappedKeyAlgorithm</code>.
 */
protected final Key engineUnwrap(byte[] wrappedKey,
                                 String wrappedKeyAlgorithm,
                                 int wrappedKeyType)
    throws InvalidKeyException, NoSuchAlgorithmException
{
    byte[] encodedKey;
    Key result = null;

    try {
        encodedKey = engineDoFinal(wrappedKey, 0,
                                   wrappedKey.length);
    } catch (BadPaddingException ePadding) {
        throw new InvalidKeyException();
    } catch (IllegalBlockSizeException eBlockSize) {
        throw new InvalidKeyException();
    }

    switch (wrappedKeyType) {
    case Cipher.SECRET_KEY:
        result = constructSecretKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = constructPrivateKey(encodedKey,
                                     wrappedKeyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = constructPublicKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    }

    return result;

}
 
Example 13
Source File: CipherWithWrappingSpi.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unwrap a previously wrapped key.
 *
 * @param wrappedKey the key to be unwrapped.
 *
 * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
 *
 * @param wrappedKeyType the type of the wrapped key.
 * This is one of <code>Cipher.SECRET_KEY</code>,
 * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
 *
 * @return the unwrapped key.
 *
 * @exception InvalidKeyException if <code>wrappedKey</code> does not
 * represent a wrapped key, or if the algorithm associated with the
 * wrapped key is different from <code>wrappedKeyAlgorithm</code>
 * and/or its key type is different from <code>wrappedKeyType</code>.
 *
 * @exception NoSuchAlgorithmException if no installed providers
 * can create keys for the <code>wrappedKeyAlgorithm</code>.
 */
protected final Key engineUnwrap(byte[] wrappedKey,
                                 String wrappedKeyAlgorithm,
                                 int wrappedKeyType)
    throws InvalidKeyException, NoSuchAlgorithmException
{
    byte[] encodedKey;
    Key result = null;

    try {
        encodedKey = engineDoFinal(wrappedKey, 0,
                                   wrappedKey.length);
    } catch (BadPaddingException ePadding) {
        throw new InvalidKeyException();
    } catch (IllegalBlockSizeException eBlockSize) {
        throw new InvalidKeyException();
    }

    switch (wrappedKeyType) {
    case Cipher.SECRET_KEY:
        result = constructSecretKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = constructPrivateKey(encodedKey,
                                     wrappedKeyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = constructPublicKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    }

    return result;

}
 
Example 14
Source File: CipherWithWrappingSpi.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unwrap a previously wrapped key.
 *
 * @param wrappedKey the key to be unwrapped.
 *
 * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
 *
 * @param wrappedKeyType the type of the wrapped key.
 * This is one of <code>Cipher.SECRET_KEY</code>,
 * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
 *
 * @return the unwrapped key.
 *
 * @exception InvalidKeyException if <code>wrappedKey</code> does not
 * represent a wrapped key, or if the algorithm associated with the
 * wrapped key is different from <code>wrappedKeyAlgorithm</code>
 * and/or its key type is different from <code>wrappedKeyType</code>.
 *
 * @exception NoSuchAlgorithmException if no installed providers
 * can create keys for the <code>wrappedKeyAlgorithm</code>.
 */
protected final Key engineUnwrap(byte[] wrappedKey,
                                 String wrappedKeyAlgorithm,
                                 int wrappedKeyType)
    throws InvalidKeyException, NoSuchAlgorithmException
{
    byte[] encodedKey;
    Key result = null;

    try {
        encodedKey = engineDoFinal(wrappedKey, 0,
                                   wrappedKey.length);
    } catch (BadPaddingException ePadding) {
        throw new InvalidKeyException();
    } catch (IllegalBlockSizeException eBlockSize) {
        throw new InvalidKeyException();
    }

    switch (wrappedKeyType) {
    case Cipher.SECRET_KEY:
        result = constructSecretKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = constructPrivateKey(encodedKey,
                                     wrappedKeyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = constructPublicKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    }

    return result;

}
 
Example 15
Source File: CipherWithWrappingSpi.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unwrap a previously wrapped key.
 *
 * @param wrappedKey the key to be unwrapped.
 *
 * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
 *
 * @param wrappedKeyType the type of the wrapped key.
 * This is one of <code>Cipher.SECRET_KEY</code>,
 * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
 *
 * @return the unwrapped key.
 *
 * @exception InvalidKeyException if <code>wrappedKey</code> does not
 * represent a wrapped key, or if the algorithm associated with the
 * wrapped key is different from <code>wrappedKeyAlgorithm</code>
 * and/or its key type is different from <code>wrappedKeyType</code>.
 *
 * @exception NoSuchAlgorithmException if no installed providers
 * can create keys for the <code>wrappedKeyAlgorithm</code>.
 */
protected final Key engineUnwrap(byte[] wrappedKey,
                                 String wrappedKeyAlgorithm,
                                 int wrappedKeyType)
    throws InvalidKeyException, NoSuchAlgorithmException
{
    byte[] encodedKey;
    Key result = null;

    try {
        encodedKey = engineDoFinal(wrappedKey, 0,
                                   wrappedKey.length);
    } catch (BadPaddingException ePadding) {
        throw new InvalidKeyException();
    } catch (IllegalBlockSizeException eBlockSize) {
        throw new InvalidKeyException();
    }

    switch (wrappedKeyType) {
    case Cipher.SECRET_KEY:
        result = constructSecretKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = constructPrivateKey(encodedKey,
                                     wrappedKeyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = constructPublicKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    }

    return result;

}
 
Example 16
Source File: CipherWithWrappingSpi.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Unwrap a previously wrapped key.
 *
 * @param wrappedKey the key to be unwrapped.
 *
 * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
 *
 * @param wrappedKeyType the type of the wrapped key.
 * This is one of <code>Cipher.SECRET_KEY</code>,
 * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
 *
 * @return the unwrapped key.
 *
 * @exception InvalidKeyException if <code>wrappedKey</code> does not
 * represent a wrapped key, or if the algorithm associated with the
 * wrapped key is different from <code>wrappedKeyAlgorithm</code>
 * and/or its key type is different from <code>wrappedKeyType</code>.
 *
 * @exception NoSuchAlgorithmException if no installed providers
 * can create keys for the <code>wrappedKeyAlgorithm</code>.
 */
protected final Key engineUnwrap(byte[] wrappedKey,
                                 String wrappedKeyAlgorithm,
                                 int wrappedKeyType)
    throws InvalidKeyException, NoSuchAlgorithmException
{
    byte[] encodedKey;
    Key result = null;

    try {
        encodedKey = engineDoFinal(wrappedKey, 0,
                                   wrappedKey.length);
    } catch (BadPaddingException ePadding) {
        throw new InvalidKeyException();
    } catch (IllegalBlockSizeException eBlockSize) {
        throw new InvalidKeyException();
    }

    switch (wrappedKeyType) {
    case Cipher.SECRET_KEY:
        result = constructSecretKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = constructPrivateKey(encodedKey,
                                     wrappedKeyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = constructPublicKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    }

    return result;

}
 
Example 17
Source File: CipherWithWrappingSpi.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unwrap a previously wrapped key.
 *
 * @param wrappedKey the key to be unwrapped.
 *
 * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
 *
 * @param wrappedKeyType the type of the wrapped key.
 * This is one of <code>Cipher.SECRET_KEY</code>,
 * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
 *
 * @return the unwrapped key.
 *
 * @exception InvalidKeyException if <code>wrappedKey</code> does not
 * represent a wrapped key, or if the algorithm associated with the
 * wrapped key is different from <code>wrappedKeyAlgorithm</code>
 * and/or its key type is different from <code>wrappedKeyType</code>.
 *
 * @exception NoSuchAlgorithmException if no installed providers
 * can create keys for the <code>wrappedKeyAlgorithm</code>.
 */
protected final Key engineUnwrap(byte[] wrappedKey,
                                 String wrappedKeyAlgorithm,
                                 int wrappedKeyType)
    throws InvalidKeyException, NoSuchAlgorithmException
{
    byte[] encodedKey;
    Key result = null;

    try {
        encodedKey = engineDoFinal(wrappedKey, 0,
                                   wrappedKey.length);
    } catch (BadPaddingException ePadding) {
        throw new InvalidKeyException();
    } catch (IllegalBlockSizeException eBlockSize) {
        throw new InvalidKeyException();
    }

    switch (wrappedKeyType) {
    case Cipher.SECRET_KEY:
        result = constructSecretKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = constructPrivateKey(encodedKey,
                                     wrappedKeyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = constructPublicKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    }

    return result;

}
 
Example 18
Source File: CipherWithWrappingSpi.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unwrap a previously wrapped key.
 *
 * @param wrappedKey the key to be unwrapped.
 *
 * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
 *
 * @param wrappedKeyType the type of the wrapped key.
 * This is one of <code>Cipher.SECRET_KEY</code>,
 * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
 *
 * @return the unwrapped key.
 *
 * @exception InvalidKeyException if <code>wrappedKey</code> does not
 * represent a wrapped key, or if the algorithm associated with the
 * wrapped key is different from <code>wrappedKeyAlgorithm</code>
 * and/or its key type is different from <code>wrappedKeyType</code>.
 *
 * @exception NoSuchAlgorithmException if no installed providers
 * can create keys for the <code>wrappedKeyAlgorithm</code>.
 */
protected final Key engineUnwrap(byte[] wrappedKey,
                                 String wrappedKeyAlgorithm,
                                 int wrappedKeyType)
    throws InvalidKeyException, NoSuchAlgorithmException
{
    byte[] encodedKey;
    Key result = null;

    try {
        encodedKey = engineDoFinal(wrappedKey, 0,
                                   wrappedKey.length);
    } catch (BadPaddingException ePadding) {
        throw new InvalidKeyException();
    } catch (IllegalBlockSizeException eBlockSize) {
        throw new InvalidKeyException();
    }

    switch (wrappedKeyType) {
    case Cipher.SECRET_KEY:
        result = constructSecretKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = constructPrivateKey(encodedKey,
                                     wrappedKeyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = constructPublicKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    }

    return result;

}
 
Example 19
Source File: CipherWithWrappingSpi.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unwrap a previously wrapped key.
 *
 * @param wrappedKey the key to be unwrapped.
 *
 * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
 *
 * @param wrappedKeyType the type of the wrapped key.
 * This is one of <code>Cipher.SECRET_KEY</code>,
 * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
 *
 * @return the unwrapped key.
 *
 * @exception InvalidKeyException if <code>wrappedKey</code> does not
 * represent a wrapped key, or if the algorithm associated with the
 * wrapped key is different from <code>wrappedKeyAlgorithm</code>
 * and/or its key type is different from <code>wrappedKeyType</code>.
 *
 * @exception NoSuchAlgorithmException if no installed providers
 * can create keys for the <code>wrappedKeyAlgorithm</code>.
 */
protected final Key engineUnwrap(byte[] wrappedKey,
                                 String wrappedKeyAlgorithm,
                                 int wrappedKeyType)
    throws InvalidKeyException, NoSuchAlgorithmException
{
    byte[] encodedKey;
    Key result = null;

    try {
        encodedKey = engineDoFinal(wrappedKey, 0,
                                   wrappedKey.length);
    } catch (BadPaddingException ePadding) {
        throw new InvalidKeyException();
    } catch (IllegalBlockSizeException eBlockSize) {
        throw new InvalidKeyException();
    }

    switch (wrappedKeyType) {
    case Cipher.SECRET_KEY:
        result = constructSecretKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = constructPrivateKey(encodedKey,
                                     wrappedKeyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = constructPublicKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    }

    return result;

}
 
Example 20
Source File: CipherWithWrappingSpi.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unwrap a previously wrapped key.
 *
 * @param wrappedKey the key to be unwrapped.
 *
 * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
 *
 * @param wrappedKeyType the type of the wrapped key.
 * This is one of <code>Cipher.SECRET_KEY</code>,
 * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
 *
 * @return the unwrapped key.
 *
 * @exception InvalidKeyException if <code>wrappedKey</code> does not
 * represent a wrapped key, or if the algorithm associated with the
 * wrapped key is different from <code>wrappedKeyAlgorithm</code>
 * and/or its key type is different from <code>wrappedKeyType</code>.
 *
 * @exception NoSuchAlgorithmException if no installed providers
 * can create keys for the <code>wrappedKeyAlgorithm</code>.
 */
protected final Key engineUnwrap(byte[] wrappedKey,
                                 String wrappedKeyAlgorithm,
                                 int wrappedKeyType)
    throws InvalidKeyException, NoSuchAlgorithmException
{
    byte[] encodedKey;
    Key result = null;

    try {
        encodedKey = engineDoFinal(wrappedKey, 0,
                                   wrappedKey.length);
    } catch (BadPaddingException ePadding) {
        throw new InvalidKeyException();
    } catch (IllegalBlockSizeException eBlockSize) {
        throw new InvalidKeyException();
    }

    switch (wrappedKeyType) {
    case Cipher.SECRET_KEY:
        result = constructSecretKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    case Cipher.PRIVATE_KEY:
        result = constructPrivateKey(encodedKey,
                                     wrappedKeyAlgorithm);
        break;
    case Cipher.PUBLIC_KEY:
        result = constructPublicKey(encodedKey,
                                    wrappedKeyAlgorithm);
        break;
    }

    return result;

}