Example usage for javax.crypto SecretKeyFactory getInstance

List of usage examples for javax.crypto SecretKeyFactory getInstance

Introduction

In this page you can find the example usage for javax.crypto SecretKeyFactory getInstance.

Prototype

public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a SecretKeyFactory object that converts secret keys of the specified algorithm.

Usage

From source file:org.tolven.config.model.CredentialManager.java

private void writeDER(char[] password, PrivateKey privateKey, File file)
        throws IOException, GeneralSecurityException {
    byte[] bytes = null;
    if (password == null) {
        bytes = privateKey.getEncoded();
    } else {/*  www.j a v  a 2s .  c om*/
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        PBEKeySpec passwordSpec = new PBEKeySpec(password);
        SecretKey secretKey = secretKeyFactory.generateSecret(passwordSpec);
        Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedPrivateKey = cipher.doFinal(privateKey.getEncoded());
        EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(cipher.getParameters(),
                encryptedPrivateKey);
        bytes = encryptedPrivateKeyInfo.getEncoded();
    }
    FileUtils.writeByteArrayToFile(file, bytes);
}

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 {//ww  w  . j  av a  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);

        /*************************************************
         * 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 {//from w  w w  .j a v a2 s . c  om

        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:csh.cryptonite.Cryptonite.java

public static String encrypt(String value, Context context) throws RuntimeException {

    try {/*  w w  w  . ja v  a  2  s  . c o m*/
        final byte[] bytes = value != null ? value.getBytes("utf-8") : new byte[0];
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(jniFullPw().toCharArray()));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.ENCRYPT_MODE, key,
                new PBEParameterSpec(Settings.Secure
                        .getString(context.getContentResolver(), Settings.Secure.ANDROID_ID).getBytes("utf-8"),
                        20));
        return new String(Base64.encode(pbeCipher.doFinal(bytes), Base64.NO_WRAP), "utf-8");

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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

static SecretKey getSecretKey() {
    try {//w w  w.  j av  a2s .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:csh.cryptonite.Cryptonite.java

public static String decrypt(String value, Context context) throws RuntimeException {
    try {//from  w w  w.  j av  a 2 s. c o m
        final byte[] bytes = value != null ? Base64.decode(value, Base64.DEFAULT) : new byte[0];
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(jniFullPw().toCharArray()));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.DECRYPT_MODE, key,
                new PBEParameterSpec(Settings.Secure
                        .getString(context.getContentResolver(), Settings.Secure.ANDROID_ID).getBytes("utf-8"),
                        20));
        return new String(pbeCipher.doFinal(bytes), "utf-8");

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:dev.ukanth.ufirewall.Api.java

/**
 * Encrypt the password/*ww w . j a v a 2 s  .  c om*/
 * @param key
 * @param data
 * @return
 */
public static String hideCrypt(String key, String data) {
    if (key == null || data == null)
        return null;
    String encodeStr = null;
    try {
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(charsetName));
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
        byte[] dataBytes = data.getBytes(charsetName);
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        encodeStr = Base64.encodeToString(cipher.doFinal(dataBytes), base64Mode);

    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage());
    }
    return encodeStr;
}

From source file:dev.ukanth.ufirewall.Api.java

/**
 * Decrypt the password//from   w  ww.  j  a v  a  2s.co m
 * @param key
 * @param data
 * @return
 */
public static String unhideCrypt(String key, String data) {
    if (key == null || data == null)
        return null;

    String decryptStr = null;
    try {
        byte[] dataBytes = Base64.decode(data, base64Mode);
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(charsetName));
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] dataBytesDecrypted = (cipher.doFinal(dataBytes));
        decryptStr = new String(dataBytesDecrypted);
    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage());
    }
    return decryptStr;
}

From source file:com.flexoodb.common.FlexUtils.java

static public String encryptdes(String passkey, String texttoencrypt) throws Exception {
    Security.addProvider(new com.sun.crypto.provider.SunJCE());
    byte key[] = passkey.trim().getBytes();
    DESKeySpec desKeySpec = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey desKey = keyFactory.generateSecret(desKeySpec);

    Cipher desCipher;//from w ww. ja  v a 2  s .  c  o  m

    // Create the cipher
    desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

    // Initialize the cipher for encryption
    desCipher.init(Cipher.ENCRYPT_MODE, desKey);

    // initialize base 64 encoding
    //org.apache.axis.encoding.Base64 base64 = new org.apache.axis.encoding.Base64();
    byte[] cleartext = texttoencrypt.getBytes();

    // Encrypt the cleartext
    byte[] ciphertext = desCipher.doFinal(cleartext);

    // encode to base64
    ciphertext = org.apache.axis.encoding.Base64.encode(ciphertext).getBytes();

    return new String(ciphertext);
}

From source file:com.flexoodb.common.FlexUtils.java

static public String decryptdes(String passkey, String texttoencrypt) throws Exception {
    Security.addProvider(new com.sun.crypto.provider.SunJCE());

    byte key[] = passkey.trim().getBytes();
    DESKeySpec desKeySpec = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey desKey = keyFactory.generateSecret(desKeySpec);

    Cipher desCipher;/*  ww  w. j a v a2 s. c  o m*/

    // Create the cipher
    desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

    // initialize base64 handler
    //org.apache.axis.encoding.Base64 base64 = new org.apache.axis.encoding.Base64();

    // decode from base64 first
    byte[] ciphertext = org.apache.axis.encoding.Base64.decode(new String(texttoencrypt.getBytes()));

    // Initialize the same cipher for decryption
    desCipher.init(Cipher.DECRYPT_MODE, desKey);

    // Decrypt the ciphertext
    byte[] cleartext1 = desCipher.doFinal(ciphertext);

    return new String(cleartext1);

}