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:MainClass.java

private static String decrypt(char[] password, String text) throws Exception {
    String salt = text.substring(0, 12);
    String ciphertext = text.substring(12, text.length());
    BASE64Decoder decoder = new BASE64Decoder();
    byte[] saltArray = decoder.decodeBuffer(salt);
    byte[] ciphertextArray = decoder.decodeBuffer(ciphertext);

    PBEKeySpec keySpec = new PBEKeySpec(password);

    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");

    SecretKey key = keyFactory.generateSecret(keySpec);

    PBEParameterSpec paramSpec = new PBEParameterSpec(saltArray, 1000);

    Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
    cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

    return new String(cipher.doFinal(ciphertextArray));
}

From source file:MainClass.java

static Cipher createCipher(int mode) throws Exception {
    PBEKeySpec keySpec = new PBEKeySpec("test".toCharArray());
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(keySpec);
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update("input".getBytes());
    byte[] digest = md.digest();
    byte[] salt = new byte[8];
    for (int i = 0; i < 8; ++i)
        salt[i] = digest[i];/*from  w  ww .jav a  2s . c  om*/
    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 20);
    Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
    cipher.init(mode, key, paramSpec);
    return cipher;
}

From source file:Main.java

public static byte[] TDesDec(byte[] key, byte[] in) throws Exception {

    Key deskey = null;//from w  w  w. java2  s.  co m
    byte[] tdesKey = new byte[24];
    if (key.length % 8 != 0)
        return null;

    if (key.length == 8) {
        System.arraycopy(key, 0, tdesKey, 0, 8);
        System.arraycopy(key, 0, tdesKey, 8, 8);
        System.arraycopy(key, 0, tdesKey, 16, 8);
    }

    if (key.length == 16) {
        System.arraycopy(key, 0, tdesKey, 0, 16);
        System.arraycopy(key, 0, tdesKey, 16, 8);
    }

    DESedeKeySpec spec = new DESedeKeySpec(tdesKey);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/ECB/NoPadding");

    cipher.init(Cipher.DECRYPT_MODE, deskey);
    byte[] bOut = cipher.doFinal(in);

    return bOut;
}

From source file:Main.java

public static byte[] TDesEnc(byte[] key, byte[] in) throws Exception {

    Key deskey = null;/*from  w  w  w . ja v  a2s .  c o m*/
    byte[] tdesKey = new byte[24];
    if (key.length % 8 != 0)
        return null;

    if (key.length == 8) {
        System.arraycopy(key, 0, tdesKey, 0, 8);
        System.arraycopy(key, 0, tdesKey, 8, 8);
        System.arraycopy(key, 0, tdesKey, 16, 8);
    }

    if (key.length == 16) {
        System.arraycopy(key, 0, tdesKey, 0, 16);
        System.arraycopy(key, 0, tdesKey, 16, 8);
    }

    DESedeKeySpec spec = new DESedeKeySpec(tdesKey);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/ECB/NoPadding");

    cipher.init(Cipher.ENCRYPT_MODE, deskey);
    byte[] bOut = cipher.doFinal(in);

    return bOut;
}

From source file:Main.java

public static Key getDESKey(byte[] key) throws Exception {
    DESKeySpec des = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    return keyFactory.generateSecret(des);
}

From source file:Main.java

/**
 *
 * @throws InvalidKeySpecException/*from ww w. ja  v  a 2  s . c  o m*/
 * @throws NoSuchAlgorithmException
 */
private static Key deriveKeyPbkdf2(byte[] salt, String password)
        throws InvalidKeySpecException, NoSuchAlgorithmException {

    KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
    SecretKeyFactory factory;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1And8bit");
    } else {
        factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    }

    byte[] keyBytes = factory.generateSecret(keySpec).getEncoded();

    return new SecretKeySpec(keyBytes, "AES");
}

From source file:Main.java

private static byte[] doPBKDF2(char[] password, byte[] salt, int iterations, int bits)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, bits);
    SecretKeyFactory skf = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM);
    return skf.generateSecret(spec).getEncoded();
}

From source file:Main.java

public static String decryptData(String ciphertext, String password) throws Exception {
    int iterationCount = 100; //because polaroid
    int keyLength = 256;

    String[] fields = ciphertext.split("]");
    byte[] iv = Base64.decode(fields[0], 0);
    byte[] salt = Base64.decode(fields[1], 0);
    byte[] cipherBytes = Base64.decode(fields[2], 0);

    Log.d(TAG, "ciphertext: " + ciphertext + "\n" + "iv length is " + "\n" + iv.length);

    KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyLength);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
    SecretKey key = new SecretKeySpec(keyBytes, "AES");

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec ivParams = new IvParameterSpec(iv);
    cipher.init(Cipher.DECRYPT_MODE, key, ivParams);
    byte[] plaintext = cipher.doFinal(cipherBytes);
    String plainStr = new String(plaintext, "UTF-8");

    return plainStr;
}

From source file:Main.java

public static String encryptData(String password, String plaintextData) throws Exception {
    // Thank you Mr. Nelenkov
    String maybeThisHelps = "http://nelenkov.blogspot.com/2012/04/using-password-based-encryption-on.html";
    Log.v(TAG, maybeThisHelps);//from w  w w .  j a  va 2s. c  om
    int iterationCount = 100; //because Polaroid
    int keyLength = 256;
    int saltLength = keyLength;

    SecureRandom random = new SecureRandom();
    byte[] salt = new byte[saltLength];
    random.nextBytes(salt);
    KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyLength);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
    SecretKey key = new SecretKeySpec(keyBytes, "AES");

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    byte[] iv = new byte[cipher.getBlockSize()];
    random.nextBytes(iv);

    IvParameterSpec ivParams = new IvParameterSpec(iv);
    cipher.init(Cipher.ENCRYPT_MODE, key, ivParams);
    byte[] ciphertext = cipher.doFinal(plaintextData.getBytes("UTF-8"));

    String ivToString = new String(Base64.encode(iv, 0));
    String saltToString = new String(Base64.encode(salt, 0));
    String ciphertextToString = new String(Base64.encode(ciphertext, 0));

    Log.d(TAG, ivToString + "]" + saltToString + "]" + ciphertextToString);
    return (ivToString + "]" + saltToString + "]" + ciphertextToString).replace("\n", "");
}

From source file:com.java.demo.DesDemo.java

public static void Init() {
    try {/*w ww.  j  a v a  2 s  .  c om*/
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        keyGenerator.init(56);
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] bytesKey = secretKey.getEncoded();

        DESKeySpec dESKeySpec = new DESKeySpec(bytesKey);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
        convertSecretKey = factory.generateSecret(dESKeySpec);

    } catch (Exception e) {
    }
}