Search in sources :

Example 6 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project WordPress-Android by wordpress-mobile.

the class WPLegacyMigrationUtils method decryptPassword.

private static String decryptPassword(String encryptedPwd) {
    try {
        DESKeySpec keySpec = new DESKeySpec(DEPRECATED_DB_PASSWORD_SECRET.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(keySpec);
        byte[] encryptedWithoutB64 = Base64.decode(encryptedPwd, Base64.DEFAULT);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] plainTextPwdBytes = cipher.doFinal(encryptedWithoutB64);
        return new String(plainTextPwdBytes);
    } catch (Exception e) {
    }
    return encryptedPwd;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) SQLException(android.database.SQLException)

Example 7 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project hutool by looly.

the class SecureUtil method generateDESKey.

/**
 * 生成 {@link SecretKey}
 *
 * @param algorithm DES算法,包括DES、DESede等
 * @param key 密钥
 * @return {@link SecretKey}
 */
public static SecretKey generateDESKey(String algorithm, byte[] key) {
    if (StrUtil.isBlank(algorithm) || false == algorithm.startsWith("DES")) {
        throw new CryptoException("Algorithm [{}] is not a DES algorithm!");
    }
    SecretKey secretKey = null;
    if (null == key) {
        secretKey = generateKey(algorithm);
    } else {
        KeySpec keySpec;
        try {
            if (algorithm.startsWith("DESede")) {
                // DESede兼容
                keySpec = new DESedeKeySpec(key);
            } else {
                keySpec = new DESKeySpec(key);
            }
        } catch (InvalidKeyException e) {
            throw new CryptoException(e);
        }
        secretKey = generateKey(algorithm, keySpec);
    }
    return secretKey;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) InvalidKeyException(java.security.InvalidKeyException)

Example 8 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project AndroidUtilLib by SiberiaDante.

the class SDDESUtil method getRawKey.

/**
 * 对密钥进行处理
 */
private static Key getRawKey(String key) throws Exception {
    DESKeySpec dks = new DESKeySpec(key.getBytes());
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
    return keyFactory.generateSecret(dks);
}
Also used : DESKeySpec(javax.crypto.spec.DESKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 9 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project jdk8u_jdk by JetBrains.

the class DigestMD5Base method makeDesKeys.

/**
     * Create parity-adjusted keys suitable for DES / DESede encryption.
     *
     * @param input A non-null byte array containing key material for
     * DES / DESede.
     * @param desStrength A string specifying eithe a DES or a DESede key.
     * @return SecretKey An instance of either DESKeySpec or DESedeKeySpec.
     *
     * @throws NoSuchAlgorithmException if the either the DES or DESede
     * algorithms cannote be lodaed by JCE.
     * @throws InvalidKeyException if an invalid array of bytes is used
     * as a key for DES or DESede.
     * @throws InvalidKeySpecException in an invalid parameter is passed
     * to either te DESKeySpec of the DESedeKeySpec constructors.
     */
private static SecretKey makeDesKeys(byte[] input, String desStrength) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException {
    // Generate first subkey using first 7 bytes
    byte[] subkey1 = addDesParity(input, 0, 7);
    KeySpec spec = null;
    SecretKeyFactory desFactory = SecretKeyFactory.getInstance(desStrength);
    switch(desStrength) {
        case "des":
            spec = new DESKeySpec(subkey1, 0);
            if (logger.isLoggable(Level.FINEST)) {
                traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST42:DES key input: ", input);
                traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST43:DES key parity-adjusted: ", subkey1);
                traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST44:DES key material: ", ((DESKeySpec) spec).getKey());
                logger.log(Level.FINEST, "DIGEST45: is parity-adjusted? {0}", Boolean.valueOf(DESKeySpec.isParityAdjusted(subkey1, 0)));
            }
            break;
        case "desede":
            // Generate second subkey using second 7 bytes
            byte[] subkey2 = addDesParity(input, 7, 7);
            // Construct 24-byte encryption-decryption-encryption sequence
            byte[] ede = new byte[subkey1.length * 2 + subkey2.length];
            System.arraycopy(subkey1, 0, ede, 0, subkey1.length);
            System.arraycopy(subkey2, 0, ede, subkey1.length, subkey2.length);
            System.arraycopy(subkey1, 0, ede, subkey1.length + subkey2.length, subkey1.length);
            spec = new DESedeKeySpec(ede, 0);
            if (logger.isLoggable(Level.FINEST)) {
                traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST46:3DES key input: ", input);
                traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST47:3DES key ede: ", ede);
                traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST48:3DES key material: ", ((DESedeKeySpec) spec).getKey());
                logger.log(Level.FINEST, "DIGEST49: is parity-adjusted? ", Boolean.valueOf(DESedeKeySpec.isParityAdjusted(ede, 0)));
            }
            break;
        default:
            throw new IllegalArgumentException("Invalid DES strength:" + desStrength);
    }
    return desFactory.generateSecret(spec);
}
Also used : DESKeySpec(javax.crypto.spec.DESKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) KeySpec(java.security.spec.KeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 10 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project jdk8u_jdk by JetBrains.

the class NTLM method calcLMHash.

byte[] calcLMHash(byte[] pwb) {
    byte[] magic = { 0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 };
    byte[] pwb1 = new byte[14];
    int len = pwb.length;
    if (len > 14)
        len = 14;
    System.arraycopy(pwb, 0, pwb1, 0, len);
    try {
        DESKeySpec dks1 = new DESKeySpec(makeDesKey(pwb1, 0));
        DESKeySpec dks2 = new DESKeySpec(makeDesKey(pwb1, 7));
        SecretKey key1 = fac.generateSecret(dks1);
        SecretKey key2 = fac.generateSecret(dks2);
        cipher.init(Cipher.ENCRYPT_MODE, key1);
        byte[] out1 = cipher.doFinal(magic, 0, 8);
        cipher.init(Cipher.ENCRYPT_MODE, key2);
        byte[] out2 = cipher.doFinal(magic, 0, 8);
        byte[] result = new byte[21];
        System.arraycopy(out1, 0, result, 0, 8);
        System.arraycopy(out2, 0, result, 8, 8);
        return result;
    } catch (InvalidKeyException ive) {
        // Will not happen, all key material are 8 bytes
        assert false;
    } catch (InvalidKeySpecException ikse) {
        // Will not happen, we only feed DESKeySpec to DES factory
        assert false;
    } catch (IllegalBlockSizeException ibse) {
        // Will not happen, we encrypt 8 bytes
        assert false;
    } catch (BadPaddingException bpe) {
        // Will not happen, this is encryption
        assert false;
    }
    // will not happen, we returned already
    return null;
}
Also used : SecretKey(javax.crypto.SecretKey) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) DESKeySpec(javax.crypto.spec.DESKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

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