Search in sources :

Example 11 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project oxCore by GluuFederation.

the class StringEncrypter method decrypt.

/**
 * Decrypt a string encrypted with this encrypter
 *
 * @param encryptedString
 *            Encrypted string
 * @return Decrypted string
 * @throws EncryptionException
 */
public String decrypt(final String encryptedString, String encryptionKey) throws EncryptionException {
    lock.lock();
    try {
        final byte[] keyAsBytes = encryptionKey.getBytes(StringEncrypter.UNICODE_FORMAT);
        String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
        KeySpec keySpec;
        if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DESEDE_ENCRYPTION_SCHEME)) {
            keySpec = new DESedeKeySpec(keyAsBytes);
        } else if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DES_ENCRYPTION_SCHEME)) {
            keySpec = new DESKeySpec(keyAsBytes);
        } else {
            throw new IllegalArgumentException("Encryption scheme not supported: " + encryptionScheme);
        }
        return decrypt(encryptedString, keySpec);
    } catch (final Exception e) {
        throw new EncryptionException(e);
    } finally {
        lock.unlock();
    }
}
Also used : DESKeySpec(javax.crypto.spec.DESKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) KeySpec(java.security.spec.KeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 12 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project YhLibraryForAndroid by android-coco.

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 13 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project MVPFrames by RockyQu.

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 = 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)

Example 14 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project jeesuite-libs by vakinge.

the class DES method decrypt.

/**
 * DES算法,解密
 *
 * @param data 待解密字符串
 * @param key  解密私钥,长度不能够小于8位
 * @return 解密后的字节数组
 * @throws Exception 异常
 */
public static String decrypt(String key, String data) {
    if (data == null)
        return null;
    try {
        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // key的长度不能够小于8位字节
        Key secretKey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
        AlgorithmParameterSpec paramSpec = new IvParameterSpec(IV_PARAMS_BYTES);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
        return new String(cipher.doFinal(hex2byte(data.getBytes())));
    } catch (Exception e) {
        e.printStackTrace();
        return data;
    }
}
Also used : DESKeySpec(javax.crypto.spec.DESKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Key(java.security.Key)

Example 15 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project Bytecoder by mirkosertic.

the class NTLM method calcResponse.

/* key is a 21 byte array. Split it into 3 7 byte chunks,
     * Convert each to 8 byte DES keys, encrypt the text arg with
     * each key and return the three results in a sequential []
     */
byte[] calcResponse(byte[] key, byte[] text) {
    try {
        assert key.length == 21;
        DESKeySpec dks1 = new DESKeySpec(makeDesKey(key, 0));
        DESKeySpec dks2 = new DESKeySpec(makeDesKey(key, 7));
        DESKeySpec dks3 = new DESKeySpec(makeDesKey(key, 14));
        SecretKey key1 = fac.generateSecret(dks1);
        SecretKey key2 = fac.generateSecret(dks2);
        SecretKey key3 = fac.generateSecret(dks3);
        cipher.init(Cipher.ENCRYPT_MODE, key1);
        byte[] out1 = cipher.doFinal(text, 0, 8);
        cipher.init(Cipher.ENCRYPT_MODE, key2);
        byte[] out2 = cipher.doFinal(text, 0, 8);
        cipher.init(Cipher.ENCRYPT_MODE, key3);
        byte[] out3 = cipher.doFinal(text, 0, 8);
        byte[] result = new byte[24];
        System.arraycopy(out1, 0, result, 0, 8);
        System.arraycopy(out2, 0, result, 8, 8);
        System.arraycopy(out3, 0, result, 16, 8);
        return result;
    } catch (IllegalBlockSizeException ex) {
        // None will happen
        assert false;
    } catch (BadPaddingException ex) {
        assert false;
    } catch (InvalidKeySpecException ex) {
        assert false;
    } catch (InvalidKeyException ex) {
        assert false;
    }
    return null;
}
Also used : SecretKey(javax.crypto.SecretKey) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) DESKeySpec(javax.crypto.spec.DESKeySpec) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) 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