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.orig.gls.security.Encode.java

public Encode(String keyString, String ivString) {
    try {/*from w w w  .ja v  a 2  s .  c  o  m*/
        final MessageDigest md = MessageDigest.getInstance("md5");
        final byte[] digestOfPassword = md.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++];
        }
        keySpec = new DESedeKeySpec(keyBytes);
        key = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);
        iv = new IvParameterSpec(ivString.getBytes());
    } catch (UnsupportedEncodingException asd) {
        System.out.println(asd.getMessage());
    } catch (InvalidKeyException asd) {
        System.out.println(asd.getMessage());
    } catch (NoSuchAlgorithmException asd) {
        System.out.println(asd.getMessage());
    } catch (InvalidKeySpecException asd) {
        System.out.println(asd.getMessage());
    }
}

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 va2s .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:com.anteam.demo.codec.cipher.symmetric.DESedeCoder.java

/**
 * DESkey/*from  w  w  w.  j a  v a  2 s  .  c o m*/
 *
 * @param key
 */
public void setKey(byte[] key) {
    if (key != null && key.length >= BasicCoder.MIN_KEY_LENGTH) {
        this.key = key;
    }
    try {
        keySpec = new DESedeKeySpec(this.key);
    } catch (InvalidKeyException e) {
        LOG.error("DESedeKeySpec:" + this.key, e);
    }
}

From source file:gov.va.ds4p.ds4pmobileportal.ui.eHealthExchange.java

public String decryptDocumentExcludeElements(byte[] processDocBytes, byte[] kekEncryptionKeyBytes) {
    Document processedDoc = null;
    String processedDocString = "";
    DESedeKeySpec desedeEncryptKeySpec;
    try {/*  w w w.  j  a  v  a 2s.com*/

        org.apache.xml.security.Init.init();

        String processDocString = new String(processDocBytes);

        processedDoc = loadDocument(processDocString);

        desedeEncryptKeySpec = new DESedeKeySpec(kekEncryptionKeyBytes);
        SecretKeyFactory skfEncrypt = SecretKeyFactory.getInstance("DESede");
        SecretKey desedeEncryptKey = skfEncrypt.generateSecret(desedeEncryptKeySpec);

        /*************************************************
         * DECRYPT DOCUMENT
         *************************************************/
        Element encryptedDataElement = (Element) processedDoc.getElementsByTagNameNS(
                EncryptionConstants.EncryptionSpecNS, EncryptionConstants._TAG_ENCRYPTEDDATA).item(0);

        /*
         * The key to be used for decrypting xml data would be obtained from
         * the keyinfo of the EncrypteData using the kek.
         */
        XMLCipher xmlCipher = XMLCipher.getInstance();
        xmlCipher.init(XMLCipher.DECRYPT_MODE, null);
        xmlCipher.setKEK(desedeEncryptKey);

        /*
         * The following doFinal call replaces the encrypted data with
         * decrypted contents in the document.
         */
        if (encryptedDataElement != null)
            xmlCipher.doFinal(processedDoc, encryptedDataElement);

        processedDocString = converXmlDocToString(processedDoc);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return processedDocString;

}

From source file:gov.va.ds4p.ds4pmobileportal.ui.eHealthExchange.java

public String decryptDocument(byte[] processDocBytes, byte[] kekEncryptionKeyBytes, byte[] kekMaskingKeyBytes) {

    Document processedDoc = null;
    String processedDocString = "";
    DESedeKeySpec desedeEncryptKeySpec;
    DESedeKeySpec desedeMaskKeySpec;
    try {// ww w .j a va  2s  . c  o m

        org.apache.xml.security.Init.init();

        String processDocString = new String(processDocBytes);

        processedDoc = loadDocument(processDocString);

        desedeEncryptKeySpec = new DESedeKeySpec(kekEncryptionKeyBytes);
        SecretKeyFactory skfEncrypt = SecretKeyFactory.getInstance("DESede");
        SecretKey desedeEncryptKey = skfEncrypt.generateSecret(desedeEncryptKeySpec);

        desedeMaskKeySpec = new DESedeKeySpec(kekMaskingKeyBytes);
        SecretKeyFactory skfMask = SecretKeyFactory.getInstance("DESede");
        SecretKey desedeMaskKey = skfMask.generateSecret(desedeMaskKeySpec);

        /*************************************************
         * DECRYPT DOCUMENT
         *************************************************/
        Element encryptedDataElement = (Element) processedDoc.getElementsByTagNameNS(
                EncryptionConstants.EncryptionSpecNS, EncryptionConstants._TAG_ENCRYPTEDDATA).item(0);

        /*
         * The key to be used for decrypting xml data would be obtained from
         * the keyinfo of the EncrypteData using the kek.
         */
        XMLCipher xmlCipher = XMLCipher.getInstance();
        xmlCipher.init(XMLCipher.DECRYPT_MODE, null);
        xmlCipher.setKEK(desedeEncryptKey);

        /*
         * The following doFinal call replaces the encrypted data with
         * decrypted contents in the document.
         */
        if (encryptedDataElement != null)
            xmlCipher.doFinal(processedDoc, encryptedDataElement);

        /*************************************************
         * DECRYPT ELEMENTS
         *************************************************/
        NodeList encryptedDataElements = processedDoc.getElementsByTagNameNS(
                EncryptionConstants.EncryptionSpecNS, EncryptionConstants._TAG_ENCRYPTEDDATA);

        while (encryptedDataElements.getLength() > 0) {
            /*
             * The key to be used for decrypting xml data would be obtained
             * from the keyinfo of the EncrypteData using the kek.
             */
            XMLCipher xmlMaskCipher = XMLCipher.getInstance();
            xmlMaskCipher.init(XMLCipher.DECRYPT_MODE, null);
            xmlMaskCipher.setKEK(desedeMaskKey);

            xmlMaskCipher.doFinal(processedDoc, ((Element) encryptedDataElements.item(0)));

            encryptedDataElements = processedDoc.getElementsByTagNameNS(EncryptionConstants.EncryptionSpecNS,
                    EncryptionConstants._TAG_ENCRYPTEDDATA);
        }

        processedDocString = converXmlDocToString(processedDoc);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return processedDocString;
}

From source file:netinf.common.security.impl.CryptoAlgorithmImpl.java

@Override
public SecretKey getSecretKeyFromString(String contentAlgorithmName, String password)
        throws NetInfCheckedSecurityException {

    try {// ww w .j  a  v  a2  s . c  o m
        DESedeKeySpec desKeySpec = new DESedeKeySpec(password.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(contentAlgorithmName);
        return keyFactory.generateSecret(desKeySpec);
    } catch (Exception e) {
        throw new NetInfCheckedSecurityException("Unable to create SecretKey. " + e.getMessage());
    }
}

From source file:org.acegisecurity.util.EncryptionUtils.java

private static byte[] cipher(String key, byte[] passedBytes, int cipherMode) throws EncryptionException {
    try {/* w  ww .ja  va2 s . c  o  m*/
        final KeySpec keySpec = new DESedeKeySpec(stringToByteArray(key));
        final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        final Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        final SecretKey secretKey = keyFactory.generateSecret(keySpec);
        cipher.init(cipherMode, secretKey);
        return cipher.doFinal(passedBytes);
    } catch (final Exception e) {
        throw new EncryptionException(e.getMessage(), e);
    }
}

From source file:org.alfresco.encryption.AlfrescoKeyStoreImpl.java

protected Key getSecretKey(KeyInformation keyInformation)
        throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException {
    byte[] keyData = keyInformation.getKeyData();

    if (keyData == null) {
        if (keyInformation.getKeyAlgorithm().equals("DESede")) {
            // no key data provided, generate key data automatically
            keyData = generateKeyData();
        } else {//ww w  .j  a  v a 2s .  c  o m
            throw new AlfrescoRuntimeException(
                    "Unable to generate secret key: key algorithm is not DESede and no keyData provided");
        }
    }

    DESedeKeySpec keySpec = new DESedeKeySpec(keyData);
    SecretKeyFactory kf = SecretKeyFactory.getInstance(keyInformation.getKeyAlgorithm());
    SecretKey secretKey = kf.generateSecret(keySpec);
    return secretKey;
}

From source file:org.alfresco.encryption.KeyStoreTests.java

protected Key generateSecretKey(String keyAlgorithm) {
    try {/*from ww w .j  a v  a  2 s . co m*/
        DESedeKeySpec keySpec = new DESedeKeySpec(generateKeyData());
        SecretKeyFactory kf = SecretKeyFactory.getInstance(keyAlgorithm);
        SecretKey secretKey = kf.generateSecret(keySpec);
        return secretKey;
    } catch (Throwable e) {
        fail("Unexpected exception: " + e.getMessage());
        return null;
    }
}

From source file:org.alfresco.repo.lotus.ws.impl.auth.LtpaAuthenticator.java

private byte[] getSecretKey(String ltpa3DESKey, String ltpaPassword) throws Exception {
    MessageDigest md = MessageDigest.getInstance("SHA");

    md.update(ltpaPassword.getBytes());/*from  w  w w.  jav  a  2 s.  c  o m*/

    byte[] hash3DES = new byte[24];

    System.arraycopy(md.digest(), 0, hash3DES, 0, 20);

    Arrays.fill(hash3DES, 20, 24, (byte) 0);

    final Cipher cipher = Cipher.getInstance(DES_DECRIPTING_ALGORITHM);

    final KeySpec keySpec = new DESedeKeySpec(hash3DES);

    final Key secretKey = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);

    cipher.init(Cipher.DECRYPT_MODE, secretKey);

    byte[] secret = cipher.doFinal(Base64.decodeBase64(ltpa3DESKey.getBytes()));

    return secret;
}