Example usage for javax.crypto.spec DESedeKeySpec getKey

List of usage examples for javax.crypto.spec DESedeKeySpec getKey

Introduction

In this page you can find the example usage for javax.crypto.spec DESedeKeySpec getKey.

Prototype

public byte[] getKey() 

Source Link

Document

Returns the DES-EDE key.

Usage

From source file:Main.java

/**
 * Creates an encoded DESedeKeySpec from a SecretKey
 * /*from   www  .j a  v  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);//from 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
 * //from w w w .  j a v a 2  s.  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

/**
 * ?   ?. ??  ?  ??  ?  ?.// ww w  . j  a  v a 2  s .com
 *
 * @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:com.vmware.o11n.plugin.crypto.service.CryptoEncryptionService.java

/**
 * TripleDES (EDE) Encryption CBC Mode with PKCS5 padding
 *
 * @param dataB64 Data to encrypt Base64 encoded.
 * @param secretB64 Encryption secret Base64 encoded. Secret must be at least 24 bytes. Only the first 24 bytes will be used.
 * @param ivB64 Initialization Vector Base64 encoded. Only first 8 bytes will be used.
 * @return Encrypted data Base64 encoded.
 * @throws NoSuchAlgorithmException/*from w  w w. j a va  2  s .  co  m*/
 * @throws NoSuchPaddingException
 * @throws InvalidKeyException
 * @throws InvalidAlgorithmParameterException
 * @throws IOException
 * @throws BadPaddingException
 * @throws IllegalBlockSizeException
 */
public String tripleDesEncrypt(String dataB64, String secretB64, String ivB64)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    String encryptedB64 = null;

    final byte[] dataBytes = Base64.decodeBase64(dataB64);
    final byte[] secretBytes = Base64.decodeBase64(secretB64);
    final byte[] ivBytes = Base64.decodeBase64(ivB64);
    final Cipher cipher = Cipher.getInstance(DESEDE_CIPHER);

    DESedeKeySpec keySpec = new DESedeKeySpec(secretBytes);
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keySpec.getKey(), "DESede"),
            new IvParameterSpec(ivBytes, 0, cipher.getBlockSize()));

    encryptedB64 = Base64.encodeBase64String(cipher.doFinal(dataBytes));
    return encryptedB64;
}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncryptionService.java

/**
 * TripleDES (EDE) Decryption CBC Mode with PKCS5 padding
 *
 * @param encryptedB64 Encrypted data Base64 encoded
 * @param secretB64 Encryption secret Base64 encoded. Secret must be at least 24 bytes. Only the first 24 bytes will be used.
 * @param ivB64 Initialization Vector Base64 encoded. Only first 8 bytes will be used.
 * @return Original data Base64 encoded.
 * @throws NoSuchAlgorithmException/* ww  w. j a  va  2 s.co  m*/
 * @throws NoSuchPaddingException
 * @throws InvalidKeyException
 * @throws InvalidAlgorithmParameterException
 * @throws IOException
 * @throws BadPaddingException
 * @throws IllegalBlockSizeException
 */
public String tripleDesDecrypt(String encryptedB64, String secretB64, String ivB64)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    String dataB64 = null;

    final byte[] encryptedBytes = Base64.decodeBase64(encryptedB64);
    final byte[] secretBytes = Base64.decodeBase64(secretB64);
    final byte[] ivBytes = Base64.decodeBase64(ivB64);
    final Cipher cipher = Cipher.getInstance(DESEDE_CIPHER);

    DESedeKeySpec keySpec = new DESedeKeySpec(secretBytes);
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keySpec.getKey(), "DESede"),
            new IvParameterSpec(ivBytes, 0, cipher.getBlockSize()));

    dataB64 = Base64.encodeBase64String(cipher.doFinal(encryptedBytes));
    return dataB64;
}

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

public Properties toProperties(Key key) {
    byte[] data;//from   w  w  w. j av a  2  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.EncryptionServiceImplTest.java

private String generateDESedeKey() throws Exception {
    KeyGenerator keygen = KeyGenerator.getInstance("DESede");
    SecretKey desedeKey = keygen.generateKey();

    SecretKeyFactory desedeFactory = SecretKeyFactory.getInstance("DESede");
    DESedeKeySpec desedeSpec = (DESedeKeySpec) desedeFactory.getKeySpec(desedeKey,
            javax.crypto.spec.DESedeKeySpec.class);
    byte[] rawDesedeKey = desedeSpec.getKey();
    return new String(Base64.encodeBase64(rawDesedeKey));
}