Example usage for javax.crypto SecretKeyFactory translateKey

List of usage examples for javax.crypto SecretKeyFactory translateKey

Introduction

In this page you can find the example usage for javax.crypto SecretKeyFactory translateKey.

Prototype

public final SecretKey translateKey(SecretKey key) throws InvalidKeyException 

Source Link

Document

Translates a key object, whose provider may be unknown or potentially untrusted, into a corresponding key object of this secret-key factory.

Usage

From source file:it.scoppelletti.security.keygen.DESKeyToPropertySetProvider.java

public Properties toProperties(Key key) {
    byte[] data;//  ww  w .j a  v  a  2s . c o m
    SecretKey desKey;
    SecretKeyFactory keyFactory;
    DESKeySpec param;
    Properties props;

    if (!(key instanceof SecretKey)) {
        return null;
    }

    try {
        keyFactory = SecretKeyFactory.getInstance(DESKeyFactory.ALGORITHM);
    } catch (NoSuchAlgorithmException ex) {
        return null;
    }

    try {
        desKey = keyFactory.translateKey((SecretKey) key);
    } catch (InvalidKeyException ex) {
        return null;
    }

    try {
        param = (DESKeySpec) keyFactory.getKeySpec(desKey, DESKeySpec.class);
    } catch (InvalidKeySpecException ex) {
        return null;
    }

    props = new Properties();
    props.setProperty(CryptoUtils.PROP_KEYFACTORY, DESKeyFactory.class.getName());
    data = param.getKey();
    props.setProperty(DESKeyFactory.PROP_KEY, Hex.encodeHexString(data));
    Arrays.fill(data, Byte.MIN_VALUE);

    return props;
}

From source file:it.scoppelletti.security.keygen.DESedeKeyToPropertySetProvider.java

public Properties toProperties(Key key) {
    byte[] data;//from  ww  w . j a va2 s  .  c o m
    SecretKey desKey;
    SecretKeyFactory keyFactory;
    DESedeKeySpec param;
    Properties props;

    if (!(key instanceof SecretKey)) {
        return null;
    }

    try {
        keyFactory = SecretKeyFactory.getInstance(DESedeKeyFactory.ALGORITHM);
    } catch (NoSuchAlgorithmException ex) {
        return null;
    }

    try {
        desKey = keyFactory.translateKey((SecretKey) key);
    } catch (InvalidKeyException ex) {
        return null;
    }

    try {
        param = (DESedeKeySpec) keyFactory.getKeySpec(desKey, DESedeKeySpec.class);
    } catch (InvalidKeySpecException ex) {
        return null;
    }

    props = new Properties();
    props.setProperty(CryptoUtils.PROP_KEYFACTORY, DESedeKeyFactory.class.getName());
    data = param.getKey();
    props.setProperty(DESedeKeyFactory.PROP_KEY, Hex.encodeHexString(data));
    Arrays.fill(data, Byte.MIN_VALUE);

    return props;
}