Example usage for javax.crypto SecretKeyFactory getKeySpec

List of usage examples for javax.crypto SecretKeyFactory getKeySpec

Introduction

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

Prototype

public final KeySpec getKeySpec(SecretKey key, Class<?> keySpec) throws InvalidKeySpecException 

Source Link

Document

Returns a specification (key material) of the given key object in the requested format.

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {

    KeyGenerator keyGen = KeyGenerator.getInstance("DES");
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
    random.setSeed(101L);/* w  w  w  .  j a  va  2s . co m*/
    keyGen.init(56, random);
    SecretKey sKey = keyGen.generateKey();
    SecretKeyFactory kfactory = SecretKeyFactory.getInstance("DES");

    DESKeySpec kspec = (DESKeySpec) kfactory.getKeySpec(sKey, DESKeySpec.class);

    System.out.println(sKey);
    FileOutputStream fos = new FileOutputStream("secretKeys");
    ObjectOutputStream oos = new ObjectOutputStream(fos);

    oos.writeObject(kspec.getKey());

    FileInputStream fin = new FileInputStream("secretKeys");
    ObjectInputStream ois = new ObjectInputStream(fin);

    byte[] kMaterial = (byte[]) ois.readObject();

    DESKeySpec keyspec = new DESKeySpec(kMaterial);
    SecretKey newKey = kfactory.generateSecret(keyspec);
    System.out.println(newKey);
    System.out.println("Do the keys equal :" + newKey.equals(sKey));

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    kg.init(new SecureRandom());
    SecretKey key = kg.generateKey();
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    Class spec = Class.forName("javax.crypto.spec.DESKeySpec");
    DESKeySpec ks = (DESKeySpec) skf.getKeySpec(key, spec);
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("keyfile"));
    oos.writeObject(ks.getKey());//from   w w  w  .ja  v  a2  s . c  o  m

    Cipher c = Cipher.getInstance("DES/CFB8/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, key);
    CipherOutputStream cos = new CipherOutputStream(new FileOutputStream("ciphertext"), c);
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(cos));
    pw.println("Stand and unfold yourself");
    pw.close();
    oos.writeObject(c.getIV());
    oos.close();
}

From source file:Main.java

/**
 * Creates an encoded DESedeKeySpec from a SecretKey
 * //  w  ww  .jav a 2  s .co m
 * @param key
 * @return
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 */
public static byte[] createDESedeKeySpec(SecretKey key)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
    DESedeKeySpec keyspec = (DESedeKeySpec) keyfactory.getKeySpec(key, DESedeKeySpec.class);
    return keyspec.getKey();
}

From source file:TripleDES.java

/** Save the specified TripleDES SecretKey to the specified file */
public static void writeKey(SecretKey key, File f)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    // Convert the secret key to an array of bytes like this
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
    DESedeKeySpec keyspec = (DESedeKeySpec) keyfactory.getKeySpec(key, DESedeKeySpec.class);
    byte[] rawkey = keyspec.getKey();

    // Write the raw key to the file
    FileOutputStream out = new FileOutputStream(f);
    out.write(rawkey);/*  ww  w .j a  v  a2  s  .c  o m*/
    out.close();
}

From source file:com.hernandez.rey.crypto.TripleDES.java

/**
 * Save the specified TripleDES SecretKey to the specified file
 * /* w w  w. j  ava2s  . c  o m*/
 * @param key the key to write
 * @param keyFile
 * @throws IOException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 */
public static void writeKey(final SecretKey key, final File keyFile)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    // Convert the secret key to an array of bytes like this
    final SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
    final DESedeKeySpec keyspec = (DESedeKeySpec) keyfactory.getKeySpec(key, DESedeKeySpec.class);
    final byte[] rawkey = keyspec.getKey();

    final byte[] encodedKey = Base64.encodeBase64(rawkey);

    // Write the raw key to the file
    FileOutputStream out = new FileOutputStream(keyFile);
    out.write(encodedKey);
    out.close();

    out = new FileOutputStream(new File(keyFile.toString().concat("-raw")));
    out.write(rawkey);
    out.close();
}

From source file:io.manasobi.utils.CryptoUtils.java

/**
 * ?   ?. ??  ?  ??  ?  ?.//w w w.  j  ava 2 s  .  c  om
 *
 * @return ?? ??  Hex ?  
 */
public static String generateHexDESKey() {

    byte[] rawKey = null;

    try {

        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);

        SecretKey secretKey = keyGenerator.generateKey();

        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM);

        DESedeKeySpec desEdeSpec = (DESedeKeySpec) secretKeyFactory.getKeySpec(secretKey,
                javax.crypto.spec.DESedeKeySpec.class);

        rawKey = desEdeSpec.getKey();

    } catch (Exception e) {

        throw new CryptoUtilsException(e.getMessage());
    }

    return new String(Hex.encodeHex(rawKey));
}

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

public Properties toProperties(Key key) {
    byte[] data;// ww  w . j  ava2s .  co  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. ja v a2 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;
}

From source file:org.kuali.rice.core.impl.encryption.DemonstrationGradeEncryptionServiceImpl.java

/**
 * /*w  ww. ja v  a 2 s  .c  om*/
 * This method generates keys. This method is implementation specific and should not be present in any general purpose interface
 * extracted from this class.
 * 
 * @return
 * @throws Exception
 */
public static String generateEncodedKey() throws Exception {
    KeyGenerator keygen = KeyGenerator.getInstance("DES");
    SecretKey desKey = keygen.generateKey();

    // Create the cipher
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init((Cipher.WRAP_MODE), desKey);

    SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES");
    DESKeySpec desSpec = (DESKeySpec) desFactory.getKeySpec(desKey, javax.crypto.spec.DESKeySpec.class);
    byte[] rawDesKey = desSpec.getKey();

    return new String(Base64.encodeBase64(rawDesKey));
}

From source file:org.kuali.rice.core.impl.encryption.EncryptionServiceImplTest.java

private String generateDESKey() throws Exception {
    KeyGenerator keygen = KeyGenerator.getInstance("DES");
    SecretKey desKey = keygen.generateKey();
    SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES");
    DESKeySpec desSpec = (DESKeySpec) desFactory.getKeySpec(desKey, javax.crypto.spec.DESKeySpec.class);
    byte[] rawDesKey = desSpec.getKey();
    return new String(Base64.encodeBase64(rawDesKey));
}