Search in sources :

Example 26 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project android-delicious by lexs.

the class ObscuredSharedPreferences method decrypt.

protected String decrypt(String value) {
    try {
        final byte[] bytes = value != null ? Base64.decode(value, Base64.DEFAULT) : new byte[0];
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(secret));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(getSalt(), 20));
        return new String(pbeCipher.doFinal(bytes), UTF8);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 27 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project android-delicious by lexs.

the class ObscuredSharedPreferences method encrypt.

protected String encrypt(String value) {
    try {
        final byte[] bytes = value != null ? value.getBytes(UTF8) : new byte[0];
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(secret));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(getSalt(), 20));
        return new String(Base64.encode(pbeCipher.doFinal(bytes), Base64.NO_WRAP), UTF8);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 28 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project KJFrameForAndroid by kymjs.

the class CipherUtils method getDESKey.

/**
     * 返回可逆算法DES的密钥
     * 
     * @param key
     *            前8字节将被用来生成密钥。
     * @return 生成的密钥
     * @throws Exception
     */
public static Key getDESKey(byte[] key) throws Exception {
    DESKeySpec des = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    return keyFactory.generateSecret(des);
}
Also used : DESKeySpec(javax.crypto.spec.DESKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 29 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project Lazy by l123456789jy.

the class DES method encrypt.

/**
	 * 将字符串进行DES加密
	 * @param source 未加密源字符串
	 * @return 加密后字符串
	 */
public String encrypt(String source) {
    byte[] retByte = null;
    // Create SecretKey object
    DESKeySpec dks = null;
    try {
        dks = new DESKeySpec(KEY);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Create IvParameterSpec object with initialization vector
        IvParameterSpec spec = new IvParameterSpec(IV);
        // Create Cipter object
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        // Initialize Cipher object
        cipher.init(Cipher.ENCRYPT_MODE, securekey, spec);
        // Decrypting data
        retByte = cipher.doFinal(source.getBytes());
        String result = "";
        if (code == 0) {
            result = new String(retByte, "ISO-8859-1");
        } else if (code == 1) {
            result = Base64.encodeToString(retByte, false);
        } else {
            result = new String(retByte);
        }
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 30 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project Lazy by l123456789jy.

the class DES method decrypt.

/**
	 * 将DES加密的字符串解密
	 * @param encrypted 加密过的字符串
	 * @return 未加密源字符串
	 */
public String decrypt(String encrypted) {
    byte[] retByte = null;
    // Create SecretKey object
    DESKeySpec dks = null;
    try {
        dks = new DESKeySpec(KEY);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Create IvParameterSpec object with initialization vector
        IvParameterSpec spec = new IvParameterSpec(IV);
        // Create Cipter object
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        // Initialize Cipher object
        cipher.init(Cipher.DECRYPT_MODE, securekey, spec);
        if (code == 0) {
            retByte = encrypted.getBytes("ISO-8859-1");
        } else if (code == 1) {
            retByte = Base64.decode(encrypted);
        } else {
            retByte = encrypted.getBytes();
        }
        // Decrypting data
        retByte = cipher.doFinal(retByte);
        return new String(retByte, "utf-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Aggregations

SecretKeyFactory (javax.crypto.SecretKeyFactory)129 SecretKey (javax.crypto.SecretKey)84 PBEKeySpec (javax.crypto.spec.PBEKeySpec)75 Cipher (javax.crypto.Cipher)58 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)39 DESKeySpec (javax.crypto.spec.DESKeySpec)28 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)26 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)26 KeySpec (java.security.spec.KeySpec)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)23 SecureRandom (java.security.SecureRandom)18 KeyStoreException (java.security.KeyStoreException)16 IOException (java.io.IOException)15 InvalidKeyException (java.security.InvalidKeyException)14 PrivateKey (java.security.PrivateKey)12 CertificateException (java.security.cert.CertificateException)12 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)12 UnrecoverableKeyException (java.security.UnrecoverableKeyException)11 Key (java.security.Key)10 KeyFactory (java.security.KeyFactory)10