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:org.craftercms.social.util.support.security.crypto.SimpleDesCipher.java

public SimpleDesCipher(String base64Key) {
    try {/*from  w  w  w .  ja va  2  s  .com*/
        cipher = Cipher.getInstance("DESede");
    } catch (NoSuchAlgorithmException e1) {
        log.error(e1.getMessage(), e1);
    } catch (NoSuchPaddingException e) {
        log.error(e.getMessage(), e);
    }

    byte[] raw = Base64.decodeBase64(base64Key);

    DESedeKeySpec keyspec;
    try {
        keyspec = new DESedeKeySpec(raw);

        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
        skey = keyfactory.generateSecret(keyspec);
    } catch (InvalidKeyException e) {
        log.error(e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        log.error(e.getMessage(), e);
    } catch (InvalidKeySpecException e) {
        log.error(e.getMessage(), e);
    }
}

From source file:org.craftercms.social.util.support.security.crypto.SimpleDesCipher.java

public void setKey(byte[] key) throws KeyException, NoSuchAlgorithmException, InvalidKeySpecException {
    DESedeKeySpec keyspec;/*  w  ww.  j av a  2 s  .  c om*/
    keyspec = new DESedeKeySpec(key);

    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
    skey = keyfactory.generateSecret(keyspec);
}

From source file:org.demo.show.providers.ThreeDes2.java

/**
 * ?//from   w ww.j av  a  2 s . co m
 * 
 * @param key
 *            
 * @return Key 
 * */
public static Key toKey(byte[] key) throws Exception {
    // Des
    DESedeKeySpec dks = new DESedeKeySpec(key);
    // 
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
    // ?
    SecretKey secretKey = keyFactory.generateSecret(dks);
    return secretKey;
}

From source file:org.kchine.rpf.db.DBLayer.java

static SecretKey getSecretKey() {
    try {/*from ww w .ja v  a  2  s.c  o m*/
        DESedeKeySpec keyspec = new DESedeKeySpec(PoolUtils.hexToBytes(_pwdKey));
        SecretKeyFactory desEdeFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey k = desEdeFactory.generateSecret(keyspec);
        return k;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:org.pentaho.mondrian.publish.PublishToServerCommand.java

public PublishToServerCommand() {
    try {/*from ww w.  j a v a 2  s.co  m*/
        byte[] keyAsBytes = "abcdefghijkPENTAHOlmnopqrstuvw5xyz".getBytes("UTF8");
        KeySpec keySpec = new DESedeKeySpec(keyAsBytes);
        keyFactory = SecretKeyFactory.getInstance("DESede");
        encryptionKey = keyFactory.generateSecret(keySpec);
        cipher = Cipher.getInstance("DESede");
    } catch (Exception e) {
        LOG.severe("failed to initialize password encryption");
        e.printStackTrace();
    }
}

From source file:org.tomcatltpa.utils.LTPAUtils.java

private byte[] getSecretKey(String ltpa3DESKey, String ltpaPassword) throws GeneralSecurityException {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(ltpaPassword.getBytes());/* www  . j  a v a2  s.  c o m*/
    byte[] hash3DES = new byte[24];
    System.arraycopy(md.digest(), 0, hash3DES, 0, 20);
    Arrays.fill(hash3DES, 20, 24, (byte) 0);
    Cipher cipher = Cipher.getInstance(DES_DECRIPTING_ALGORITHM);
    KeySpec keySpec = new DESedeKeySpec(hash3DES);
    Key secretKey2 = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);
    cipher.init(Cipher.DECRYPT_MODE, secretKey2);
    byte[] secret = cipher.doFinal(Base64.decodeBase64(ltpa3DESKey.getBytes()));
    return secret;
}

From source file:org.xdi.util.security.StringEncrypter.java

/**
 * Constructor specifying scheme and key
 * /*from w ww  .j  a v a  2  s .c o  m*/
 * @param encryptionScheme
 *            Encryption scheme to use
 * @param encryptionKey
 *            Encryption key to use
 * @throws EncryptionException
 */
public StringEncrypter(final String encryptionScheme, final String encryptionKey) throws EncryptionException {
    if (encryptionKey == null) {
        throw new IllegalArgumentException("encryption key was null");
    }
    if (encryptionKey.trim().length() < 24) {
        throw new IllegalArgumentException("encryption key was less than 24 characters");
    }

    try {
        final byte[] keyAsBytes = encryptionKey.getBytes(StringEncrypter.UNICODE_FORMAT);

        if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DESEDE_ENCRYPTION_SCHEME)) {
            keySpec = new DESedeKeySpec(keyAsBytes);
        } else if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DES_ENCRYPTION_SCHEME)) {
            keySpec = new DESKeySpec(keyAsBytes);
        } else {
            throw new IllegalArgumentException("Encryption scheme not supported: " + encryptionScheme);
        }

        keyFactory = SecretKeyFactory.getInstance(encryptionScheme);
        cipher = Cipher.getInstance(encryptionScheme);
    } catch (final InvalidKeyException e) {
        throw new EncryptionException(e);
    } catch (final UnsupportedEncodingException e) {
        throw new EncryptionException(e);
    } catch (final NoSuchAlgorithmException e) {
        throw new EncryptionException(e);
    } catch (final NoSuchPaddingException e) {
        throw new EncryptionException(e);
    }
}

From source file:org.xdi.util.security.StringEncrypter.java

/**
 * Decrypt a string encrypted with this encrypter
 * //from   ww  w  . ja  v a 2  s .  co  m
 * @param encryptedString
 *            Encrypted string
 * @return Decrypted string
 * @throws EncryptionException
 */
public String decrypt(final String encryptedString, String encryptionKey) throws EncryptionException {
    lock.lock();
    try {
        final byte[] keyAsBytes = encryptionKey.getBytes(StringEncrypter.UNICODE_FORMAT);
        String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
        KeySpec keySpec;
        if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DESEDE_ENCRYPTION_SCHEME)) {
            keySpec = new DESedeKeySpec(keyAsBytes);
        } else if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DES_ENCRYPTION_SCHEME)) {
            keySpec = new DESKeySpec(keyAsBytes);
        } else {
            throw new IllegalArgumentException("Encryption scheme not supported: " + encryptionScheme);
        }
        return decrypt(encryptedString, keySpec);
    } catch (final Exception e) {
        throw new EncryptionException(e);
    } finally {
        lock.unlock();
    }
}

From source file:org.xdi.util.security.StringEncrypter.java

/**
 * Encrypt a string//from  ww w.  j  a  va 2s  .co  m
 * 
 * @param unencryptedString
 *            String to encrypt
 * @return Encrypted string (using scheme and key specified at construction)
 * @throws EncryptionException
 */
public String encrypt(final String unencryptedString, String encryptionKey) throws EncryptionException {
    lock.lock();
    try {
        final byte[] keyAsBytes = encryptionKey.getBytes(StringEncrypter.UNICODE_FORMAT);
        String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
        KeySpec keySpec;
        if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DESEDE_ENCRYPTION_SCHEME)) {
            keySpec = new DESedeKeySpec(keyAsBytes);
        } else if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DES_ENCRYPTION_SCHEME)) {
            keySpec = new DESKeySpec(keyAsBytes);
        } else {
            throw new IllegalArgumentException("Encryption scheme not supported: " + encryptionScheme);
        }

        return encrypt(unencryptedString, keySpec);
    } catch (final Exception e) {
        throw new EncryptionException(e);
    } finally {
        lock.unlock();
    }
}

From source file:uk.ac.ox.webauth.crypto.Des3CbcSha1Kd.java

/**
 * From RFC 3961://from   w ww  . ja  v a  2s. c o m
 * DR(Key, Constant) = k-truncate(E(Key, Constant, initial-cipher-state))
 *
 * Here DR is the random-octet generation function described below, and
 * DK is the key-derivation function produced from it.  In this
 * construction, E(Key, Plaintext, CipherState) is a cipher, Constant is
 * a well-known constant determined by the specific usage of this
 * function, and k-truncate truncates its argument by taking the first k
 * bits.  Here, k is the key generation seed length needed for the
 * encryption system.
 *
 * The output of the DR function is a string of bits; the actual key is
 * produced by applying the cryptosystem's random-to-key operation on
 * this bitstring.
 *
 * If the Constant is smaller than the cipher block size of E, then it
 * must be expanded with n-fold() so it can be encrypted.  If the output
 * of E is shorter than k bits, it is fed back into the encryption as
 * many times as necessary.  The construct is as follows (where |
 * indicates concatentation):
 *
 *    K1 = E(Key, n-fold(Constant), initial-cipher-state)
 *    K2 = E(Key, K1, initial-cipher-state)
 *    K3 = E(Key, K2, initial-cipher-state)
 *    K4 = ...
 *
 *    DR(Key, Constant) = k-truncate(K1 | K2 | K3 | K4 ...)
 */
private static byte[] dr(byte[] key, byte[] constant) throws GeneralSecurityException {
    // first make a DES3 key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
    KeySpec spec = new DESedeKeySpec(key);
    SecretKey secretKey = factory.generateSecret(spec);

    // initialise the cipher to use
    Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
    cipher.init(ENCRYPT_MODE, secretKey, IV);

    // ensure the constant is not smaller than the blocksize
    if (constant.length < cipher.getBlockSize()) {
        constant = nFold(constant, cipher.getBlockSize() * 8);
    }

    // now encrypt until we have at least 21 bytes, the length of a DES3 key
    byte[] input = constant;
    byte[] kn = new byte[0];
    do {
        byte[] newKn = cipher.doFinal(input);
        byte[] oldKn = kn;
        kn = new byte[oldKn.length + newKn.length];
        System.arraycopy(oldKn, 0, kn, 0, oldKn.length);
        System.arraycopy(newKn, 0, kn, oldKn.length, newKn.length);
        input = newKn;
    } while (kn.length < 21);

    // make sure we are returning exactly 21 bytes
    if (kn.length != 21) {
        byte[] newKn = new byte[21];
        System.arraycopy(kn, 0, newKn, 0, 21);
        kn = newKn;
    }

    return kn;
}