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.alfresco.repo.lotus.ws.impl.auth.LtpaAuthenticator.java

private byte[] decrypt(byte[] token, byte[] key, String algorithm) throws Exception {
    SecretKey sKey = null;//from  w w  w  . j  a va  2s  .  c  o  m

    if (algorithm.indexOf("AES") != -1) {
        sKey = new SecretKeySpec(key, 0, 16, "AES");
    } else {
        DESedeKeySpec kSpec = new DESedeKeySpec(key);
        SecretKeyFactory kFact = SecretKeyFactory.getInstance("DESede");
        sKey = kFact.generateSecret(kSpec);
    }
    Cipher cipher = Cipher.getInstance(algorithm);

    if (algorithm.indexOf("ECB") == -1) {
        if (algorithm.indexOf("AES") != -1) {
            IvParameterSpec ivs16 = generateIvParameterSpec(key, 16);
            cipher.init(Cipher.DECRYPT_MODE, sKey, ivs16);
        } else {
            IvParameterSpec ivs8 = generateIvParameterSpec(key, 8);
            cipher.init(Cipher.DECRYPT_MODE, sKey, ivs8);
        }
    } else {
        cipher.init(Cipher.DECRYPT_MODE, sKey);
    }
    return cipher.doFinal(token);
}

From source file:org.apache.juddi.samples.DES.java

public static void main(String[] args) throws Exception {
    DES des = new DES();
    KeyGenerator kgen;//from w w  w. j a v  a  2 s.  co m
    try {
        kgen = KeyGenerator.getInstance(DESEDE_ENCRYPTION_SCHEME);
        kgen.init(168);
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        myEncryptionKey = new String(Base64.encodeBase64(raw));
        myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
        arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
        ks = new DESedeKeySpec(arrayBytes);
        skf = SecretKeyFactory.getInstance(myEncryptionScheme);
        cipher = Cipher.getInstance(myEncryptionScheme);
        key = skf.generateSecret(ks);

        System.out.println(new String(Base64.encodeBase64(raw)));
        System.out.println(des.encrypt("password"));
        System.out.println(des.decrypt(des.encrypt("password")));
    } catch (Exception ex) {
        ex.printStackTrace();
        ;
    }

}

From source file:org.apache.juddi.v3.client.cryptor.TripleDESCrytor.java

/**
 *default constructor/*from  w  w  w.  j a v a2  s  . c o  m*/
 * @throws Exception
 */
public TripleDESCrytor() throws Exception {
    String keyfromfile = CryptorFactory.loadKeyFromFile("TripleDESCrytor");
    if (keyfromfile != null)
        myEncryptionKey = keyfromfile;
    else
        myEncryptionKey = "rioTEBCe/RAHRs6tTyYxDqettnVbZA6z";
    myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
    arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
    ks = new DESedeKeySpec(arrayBytes);
    skf = SecretKeyFactory.getInstance(myEncryptionScheme);
    cipher = Cipher.getInstance(myEncryptionScheme);
    key = skf.generateSecret(ks);
}

From source file:org.apache.xml.security.samples.encryption.Decrypter.java

private static SecretKey loadKeyEncryptionKey() throws Exception {

    String fileName = "kek";
    String jceAlgorithmName = "DESede";

    File kekFile = new File(fileName);

    DESedeKeySpec keySpec = new DESedeKeySpec(JavaUtils.getBytesFromFile(fileName));
    SecretKeyFactory skf = SecretKeyFactory.getInstance(jceAlgorithmName);
    SecretKey key = skf.generateSecret(keySpec);

    System.out.println("Key encryption key loaded from " + kekFile.toURL().toString());
    return key;//from   w ww  .j  a v a 2 s . c  o m
}

From source file:org.apache.xml.security.test.encryption.BobKeyResolver.java

/**
 * Method engineResolveSecretKey/*from w  ww .  j  av  a 2s  .  c o m*/
 *
 * @param element
 * @param BaseURI
 * @param storage
 *
 * @throws KeyResolverException
 */
public javax.crypto.SecretKey engineLookupAndResolveSecretKey(Element element, String BaseURI,
        StorageResolver storage) throws KeyResolverException {

    if (engineCanResolve(element, BaseURI, storage)) {
        try {
            DESedeKeySpec keySpec = new DESedeKeySpec("abcdefghijklmnopqrstuvwx".getBytes("ASCII"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
            SecretKey key = keyFactory.generateSecret(keySpec);

            return key;
        } catch (Exception e) {
            throw new KeyResolverException("Something badly wrong in creation of bob's key");
        }
    }

    return null;
}

From source file:org.apache.xml.security.test.encryption.EncryptContentTest.java

public void setUp() throws Exception {

    org.apache.xml.security.Init.init();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from   w w w. ja v a 2  s.  co m*/
    db = dbf.newDocumentBuilder();

    byte[] bits192 = "abcdefghijklmnopqrstuvwx".getBytes();
    DESedeKeySpec keySpec = new DESedeKeySpec(bits192);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
    secretKey = keyFactory.generateSecret(keySpec);

    TransformerFactory tf = TransformerFactory.newInstance();
    tf.newTransformer();

    // Determine if we have ISO 10126 Padding - needed for Bulk AES or
    // 3DES encryption

    haveISOPadding = false;
    String algorithmId = JCEMapper
            .translateURItoJCEID(org.apache.xml.security.utils.EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES128);

    if (algorithmId != null) {
        try {
            if (Cipher.getInstance(algorithmId) != null)
                haveISOPadding = true;
        } catch (NoSuchAlgorithmException nsae) {
        } catch (NoSuchPaddingException nspe) {
        }
    }

}

From source file:org.apache.xml.security.test.encryption.XMLCipherTester.java

/**
 * Test encryption using a generated AES 192 bit key that is
 * encrypted using a 3DES key.  Then reverse by decrypting 
 * EncryptedKey by hand/*from w  w  w .  java 2  s. c  o  m*/
 */

public void testAES192ElementAES256KWCipher() throws Exception {

    Document d = document(); // source
    Document ed = null;
    Document dd = null;
    Element e = (Element) d.getElementsByTagName(element()).item(index());
    Element ee = null;

    String source = null;
    String target = null;

    if (haveISOPadding && haveKeyWraps) {

        source = toString(d);

        // Set up a Key Encryption Key
        byte[] bits192 = "abcdefghijklmnopqrstuvwx".getBytes();
        DESedeKeySpec keySpec = new DESedeKeySpec(bits192);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        Key kek = keyFactory.generateSecret(keySpec);

        // Generate a traffic key
        KeyGenerator keygen = KeyGenerator.getInstance("AES");
        keygen.init(192);
        Key key = keygen.generateKey();

        cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES_KeyWrap);
        cipher.init(XMLCipher.WRAP_MODE, kek);
        EncryptedKey encryptedKey = cipher.encryptKey(d, key);

        // encrypt
        cipher = XMLCipher.getInstance(XMLCipher.AES_192);
        cipher.init(XMLCipher.ENCRYPT_MODE, key);
        EncryptedData builder = cipher.getEncryptedData();

        KeyInfo builderKeyInfo = builder.getKeyInfo();
        if (builderKeyInfo == null) {
            builderKeyInfo = new KeyInfo(d);
            builder.setKeyInfo(builderKeyInfo);
        }

        builderKeyInfo.add(encryptedKey);

        ed = cipher.doFinal(d, e);

        //decrypt
        key = null;
        ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0);
        cipher = XMLCipher.getInstance();
        cipher.init(XMLCipher.DECRYPT_MODE, null);

        EncryptedData encryptedData = cipher.loadEncryptedData(ed, ee);

        if (encryptedData == null) {
            System.out.println("ed is null");
        } else if (encryptedData.getKeyInfo() == null) {
            System.out.println("ki is null");
        }
        EncryptedKey ek = encryptedData.getKeyInfo().itemEncryptedKey(0);

        if (ek != null) {
            XMLCipher keyCipher = XMLCipher.getInstance();
            keyCipher.init(XMLCipher.UNWRAP_MODE, kek);
            key = keyCipher.decryptKey(ek, encryptedData.getEncryptionMethod().getAlgorithm());
        }

        // Create a new cipher just to be paranoid
        XMLCipher cipher3 = XMLCipher.getInstance();
        cipher3.init(XMLCipher.DECRYPT_MODE, key);
        dd = cipher3.doFinal(ed, ee);

        target = toString(dd);

        Assert.assertEquals(source, target);
    } else {
        log.warn("Test testAES192ElementAES256KWCipher skipped as necessary algorithms not available");
    }
}

From source file:org.apache.xml.security.test.encryption.XMLCipherTester.java

public void testTrippleDesElementCipher() throws Exception {
    Document d = document(); // source
    Document ed = null; // target
    Document dd = null; // target
    Element e = (Element) d.getElementsByTagName(element()).item(index());
    Element ee = null;/* w w  w  .j a v  a 2 s .  com*/

    String source = null;
    String target = null;

    if (haveISOPadding) {

        source = toString(d);

        // prepare for encryption
        byte[] passPhrase = "24 Bytes per DESede key!".getBytes();
        DESedeKeySpec keySpec = new DESedeKeySpec(passPhrase);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey key = keyFactory.generateSecret(keySpec);

        // encrypt
        cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES);
        cipher.init(XMLCipher.ENCRYPT_MODE, key);
        ed = cipher.doFinal(d, e);

        //decrypt
        cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES);
        cipher.init(XMLCipher.DECRYPT_MODE, key);
        ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0);
        EncryptedData encryptedData = cipher.loadEncryptedData(ed, ee);
        Assert.assertEquals(encryptedData.getEncryptionMethod().getAlgorithm(), XMLCipher.TRIPLEDES);
        dd = cipher.doFinal(ed, ee);

        target = toString(dd);
        Assert.assertEquals(source, target);
    } else {
        log.warn("Test testTrippleDesElementCipher skipped as necessary algorithms not available");
    }
}

From source file:org.apache.xml.security.test.encryption.XMLCipherTester.java

public void testTrippleDesDocumentCipher() throws Exception {
    Document d = document(); // source
    Document ed = null; // target
    Document dd = null; // target
    Element e = d.getDocumentElement();
    Element ee = null;// w w  w .  j  av a  2s  .  co m

    String source = null;
    String target = null;

    if (haveISOPadding) {

        source = toString(d);

        // prepare for encryption
        byte[] passPhrase = "24 Bytes per DESede key!".getBytes();
        DESedeKeySpec keySpec = new DESedeKeySpec(passPhrase);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey key = keyFactory.generateSecret(keySpec);

        // encrypt
        cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES);
        cipher.init(XMLCipher.ENCRYPT_MODE, key);
        ed = cipher.doFinal(d, e);

        //decrypt
        cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES);
        cipher.init(XMLCipher.DECRYPT_MODE, key);
        ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0);
        dd = cipher.doFinal(ed, ee);

        target = toString(dd);

        Assert.assertEquals(source, target);
    } else {
        log.warn("Test testTrippleDesDocumentCipher skipped as necessary algorithms not available");
    }
}

From source file:org.casbah.provider.SSLeayEncoder.java

private static DESedeKeySpec calculateKeyFromPassKey(byte[] keypass, byte[] salt)
        throws IOException, GeneralSecurityException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] digest = null;
    while (baos.size() < 24) {
        md.reset();//from  w  w w.  ja  v  a  2 s.  c  om
        if (digest != null) {
            md.update(digest);
        }
        md.update(keypass);
        digest = md.digest(salt);
        baos.write(digest);
    }
    DESedeKeySpec result = new DESedeKeySpec(baos.toByteArray());
    return result;
}