Example usage for javax.crypto.spec DESedeKeySpec DESedeKeySpec

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

Introduction

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

Prototype

public DESedeKeySpec(byte[] key) throws InvalidKeyException 

Source Link

Document

Creates a DESedeKeySpec object using the first 24 bytes in key as the key material for the DES-EDE key.

Usage

From source file:com.servoy.j2db.util.SecuritySupport.java

public static Key getCryptKey(Settings settings) throws Exception {
    initKeyStoreAndPassphrase(settings);

    Enumeration e = keyStore.aliases();
    while (e.hasMoreElements()) {
        String alias = (String) e.nextElement();
        if (keyStore.isKeyEntry(alias)) {
            return new SecretKeySpec(
                    new DESedeKeySpec(keyStore.getKey(alias, passphrase).getEncoded()).getKey(), "DESede");
        }/*www. j  a  v a 2 s  . co  m*/
    }
    return null;
}

From source file:egovframework.com.ext.jfile.security.service.CipherServiceImpl.java

/**
 *   ?./*from  w  ww .  ja  v a  2  s  .c  o  m*/
 * @param keyAlgorithm  .
 * @param algorithm .
 * @param keyData  ??.
 * @return Key  .
 * @throws NoSuchAlgorithmException   ?  ?  .
 * @throws InvalidKeyException  ? key ?  
 * @throws InvalidKeySpecException  ? keySpec ?  
 */
private static Key generateKey(String keyAlgorithm, String algorithm, byte[] keyData)
        throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException {
    if (keyAlgorithm == null || "".equals(keyAlgorithm))
        throw new NoSuchAlgorithmException("algorithm is nessary");
    String upper = keyAlgorithm.toUpperCase();
    if ("DES".equals(upper)) {
        KeySpec keySpec = new DESKeySpec(keyData);
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(keyAlgorithm);

        SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
        return secretKey;

    } else if (upper.indexOf("DESEDE") > -1 || upper.indexOf("TRIPLEDES") > -1) {
        KeySpec keySpec = new DESedeKeySpec(keyData);

        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(keyAlgorithm);
        SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
        return secretKey;
    } else {
        SecretKeySpec keySpec = new SecretKeySpec(keyData, keyAlgorithm);
        return keySpec;
    }
}

From source file:com.waveerp.desEncryption.java

public void Encrypter(String keyString, String ivString) {
    try {/*from w  w w  .  java2  s. com*/

        keyString = "J3SuSChRiSt";
        ivString = "Pr0V3rBs";

        final MessageDigest msgDigest = MessageDigest.getInstance("md5");
        final byte[] digestOfPassword = msgDigest.digest(Base64.decodeBase64(keyString.getBytes("utf-8")));
        final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        for (int j = 0, k = 16; j < 8;) {
            keyBytes[k++] = keyBytes[j++];
        }

        kSpec = new DESedeKeySpec(keyBytes);

        sKey = SecretKeyFactory.getInstance("DESede").generateSecret(kSpec);

        ivParSpec = new IvParameterSpec(ivString.getBytes());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:TripleDES.java

/** Read a TripleDES secret key from the specified file */
public static SecretKey readKey(File f)
        throws IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException {
    // Read the raw bytes from the keyfile
    DataInputStream in = new DataInputStream(new FileInputStream(f));
    byte[] rawkey = new byte[(int) f.length()];
    in.readFully(rawkey);/* w  w w .  j a  v a 2  s  . c o m*/
    in.close();

    // Convert the raw bytes to a secret key like this
    DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
    SecretKey key = keyfactory.generateSecret(keyspec);
    return key;
}

From source file:com.anteam.demo.codec.cipher.symmetric.DESTest.java

License:asdf

public byte[] testDESedeEn(String plainText) {
    try {// ww  w. j  a  v a  2  s. c om
        // Create an array to hold the key
        byte[] encryptKey = "This is a test DESede key".getBytes();

        // Create a DESede key spec from the key
        DESedeKeySpec spec = new DESedeKeySpec(encryptKey);

        // Get the secret key factor for generating DESede keys
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");

        // Generate a DESede SecretKey object
        SecretKey theKey = keyFactory.generateSecret(spec);

        // Create a DESede Cipher
        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");

        // Create an initialization vector (necessary for CBC mode)

        IvParameterSpec IvParameters = new IvParameterSpec(
                new byte[] { 0x01, 0x02, 0x03, 0x04, 0x0F, 0x0E, 0x0D, 0x0C });

        // Initialize the cipher and put it into encrypt mode
        cipher.init(Cipher.ENCRYPT_MODE, theKey, IvParameters);
        return cipher.doFinal(plainText.getBytes());

    } catch (Exception exc) {
        exc.printStackTrace();
    }
    return null;
}

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   ww w  . j ava  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.hernandez.rey.crypto.TripleDES.java

/**
 * Read a TripleDES secret key from the specified file
 * //from  w  w w  .  j  a  v  a2s  .c  o  m
 * @param keyFile key file to read
 * @return secret key appropriate for encryption/decryption with 3DES
 * @throws IOException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 * @throws InvalidKeySpecException
 */
public static SecretKey readKey(final File keyFile)
        throws IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException {
    // Read the raw bytes from the keyfile
    final DataInputStream in = new DataInputStream(new FileInputStream(keyFile));
    final byte[] rawkey = new byte[(int) keyFile.length()];
    in.readFully(rawkey);
    in.close();

    // Convert the raw bytes to a secret key like this
    final DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
    final SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
    final SecretKey key = keyfactory.generateSecret(keyspec);
    return key;
}

From source file:atg.projects.store.crypto.DESedeEncryptor.java

/**
 * Initialize DESedeEncryptor.//from  www . ja v  a 2 s.co m
 *
 * @throws EncryptorException This exception indicates that a severe error
 * occurred while performing a cryptography operation.
 */
protected final void doInit() throws EncryptorException {
    try {
        addSecurityProviders();

        if (mKeyString == null) {
            mKeyString = KEY.getBytes();
        }

        mKeySpec = new DESedeKeySpec(mKeyString);
        mKey = new SecretKeySpec(mKeySpec.getKey(), ALGORITHM);
        mEncryptCypher = javax.crypto.Cipher.getInstance(ALGORITHM);
        mEncryptCypher.init(javax.crypto.Cipher.ENCRYPT_MODE, mKey);
        mDecryptCypher = javax.crypto.Cipher.getInstance(ALGORITHM);
        mDecryptCypher.init(javax.crypto.Cipher.DECRYPT_MODE, mKey);
    } catch (NoSuchAlgorithmException nsae) {
        throw new EncryptorException("Failed to initialize encryptor: ", nsae);
    } catch (NoSuchPaddingException nspe) {
        throw new EncryptorException("Failed to initialize encryptor: ", nspe);
    } catch (InvalidKeyException nske) {
        throw new EncryptorException("Failed to initialize encryptor: ", nske);
    }
}

From source file:com.anteam.demo.codec.cipher.symmetric.DESTest.java

License:asdf

public byte[] testDESedeDe(byte[] encryptedText) {
    try {//from  ww  w  . ja  va 2 s  .co m
        // Create an array to hold the key
        byte[] encryptKey = "This is a test DESede key".getBytes();

        // Create a DESede key spec from the key
        DESedeKeySpec spec = new DESedeKeySpec(encryptKey);

        // Get the secret key factor for generating DESede keys
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");

        // Generate a DESede SecretKey object
        SecretKey theKey = keyFactory.generateSecret(spec);

        // Create a DESede Cipher
        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");

        // Create an initialization vector (necessary for CBC mode)

        IvParameterSpec IvParameters = new IvParameterSpec(
                new byte[] { 0x01, 0x02, 0x03, 0x04, 0x0F, 0x0E, 0x0D, 0x0C });

        // Initialize the cipher and put it into encrypt mode
        cipher.init(Cipher.DECRYPT_MODE, theKey, IvParameters);

        return cipher.doFinal(encryptedText);
    } catch (Exception exc) {
        exc.printStackTrace();
    }
    return null;
}

From source file:jef.tools.security.EncrypterUtil.java

/**
 * ??3DESKey// w  w w.  j a  v  a2 s  .co m
 * 
 * @param password
 * @return
 */
public static final SecretKey toDESedeKey(String password) {
    byte[] bb = password.getBytes();
    Assert.isTrue(bb.length > 23, "the secretKey for 3DES must be 24 bytes at least.");
    try {
        KeySpec keySpec = new DESedeKeySpec(bb);
        SecretKey key = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);
        return key;
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e.getMessage());
    }
}