Search in sources :

Example 56 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project protools by SeanDragon.

the class ToolDES method toKey.

/**
 * 转换密钥
 *
 * @param key
 *         二进制密钥
 *
 * @return Key 密钥
 *
 * @throws Exception
 */
private static Key toKey(byte[] key) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
    // 实例化DES密钥材料
    DESKeySpec dks = new DESKeySpec(key);
    // 实例化秘密密钥工厂
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
    // 生成秘密密钥
    SecretKey secretKey = keyFactory.generateSecret(dks);
    return secretKey;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 57 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project happy-dns-android by qiniu.

the class DES method encrypt.

/**
 * Convert data to encrypted hex string
 * @param data data to encrypt
 * @param key encrypt key
 * @return hex string
 */
public static String encrypt(String data, String key) {
    try {
        Cipher c = Cipher.getInstance("DES/ECB/PKCS5Padding");
        c.init(Cipher.ENCRYPT_MODE, SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key.getBytes())));
        return Hex.encodeHexString(c.doFinal(data.getBytes()));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher)

Example 58 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project afwall by ukanth.

the class Api method unhideCrypt.

/**
 * Decrypt the password
 *
 * @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;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) JSONException(org.json.JSONException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 59 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project afwall by ukanth.

the class Api method hideCrypt.

/**
 * Encrypt the password
 *
 * @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;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) JSONException(org.json.JSONException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 60 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project XUtil by xuexiangjys.

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)

Aggregations

DESKeySpec (javax.crypto.spec.DESKeySpec)69 SecretKeyFactory (javax.crypto.SecretKeyFactory)52 SecretKey (javax.crypto.SecretKey)48 Cipher (javax.crypto.Cipher)45 SecureRandom (java.security.SecureRandom)20 InvalidKeyException (java.security.InvalidKeyException)13 KeySpec (java.security.spec.KeySpec)12 IvParameterSpec (javax.crypto.spec.IvParameterSpec)11 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)9 BadPaddingException (javax.crypto.BadPaddingException)8 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)8 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)8 DESedeKeySpec (javax.crypto.spec.DESedeKeySpec)8 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)7 SecretKeySpec (javax.crypto.spec.SecretKeySpec)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 Key (java.security.Key)4 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)3 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)2 SQLException (android.database.SQLException)2