Search in sources :

Example 46 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project drools by kiegroup.

the class KeyStoreHelperTest method storeKeyIntoKeyStoreFile.

private SecretKey storeKeyIntoKeyStoreFile(final String keyPhrase) throws CertificateException, NoSuchAlgorithmException, IOException, KeyStoreException, InvalidKeyException, InvalidKeySpecException {
    final KeyStore keyStore = KeyStore.getInstance("JCEKS");
    keyStore.load(null, KEYSTORE_SERVER_PASSWORD.toCharArray());
    final SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
    final SecretKey mySecretKey = secretKeyFactory.generateSecret(new DESKeySpec(keyPhrase.getBytes()));
    final KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(mySecretKey);
    keyStore.setEntry(KEY_ALIAS, skEntry, new KeyStore.PasswordProtection(KEY_PASSWORD.toCharArray()));
    try (FileOutputStream fos = new java.io.FileOutputStream(KEYSTORE_JCEKS_FILENAME, false)) {
        keyStore.store(fos, KEYSTORE_SERVER_PASSWORD.toCharArray());
    }
    return mySecretKey;
}
Also used : SecretKey(javax.crypto.SecretKey) FileOutputStream(java.io.FileOutputStream) DESKeySpec(javax.crypto.spec.DESKeySpec) KeyStore(java.security.KeyStore) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 47 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project coprhd-controller by CoprHD.

the class NTLMUtils method encryptHashWithDES.

/**
 * Encrypts the ntlm2 session hash with DES using the method described at
 * http://davenport.sourceforge.net/ntlm.html#theNtlm2SessionResponse.
 *
 * @param ntlm2SessionHash
 *            the ntlm2 session hash to encrypt
 * @param ntlmHash
 *            the 21 byte long ntlm hash
 * @return the encrypted session hash
 * @throws Exception
 *             if something goes wrong
 */
private static byte[] encryptHashWithDES(byte[] ntlm2SessionHash, byte[] ntlmHash) throws Exception {
    byte[] toReturn = new byte[24];
    for (int i = 0; i < 3; i++) {
        byte[] key = new byte[7];
        System.arraycopy(ntlmHash, i * 7, key, 0, 7);
        key = adjustOddParityForDES(key);
        DESKeySpec desKeySpec = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        desCipher.init(Cipher.ENCRYPT_MODE, secretKey);
        System.arraycopy(desCipher.doFinal(ntlm2SessionHash), 0, toReturn, i * 8, 8);
    }
    return toReturn;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 48 with DESKeySpec

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

the class StringEncrypter method encrypt.

/**
 * Encrypt a string
 *
 * @param unencryptedString
 *            String to encrypt
 * @return Encrypted string (using scheme and key specified at construction)
 * @throws EncryptionException
 */
public String encrypt(final String unencryptedString, 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 encrypt(unencryptedString, 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 49 with DESKeySpec

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

the class DES method encrypt.

/**
 * DES算法,加密
 *
 * @param data 待加密字符串
 * @param key  加密私钥,长度不能够小于8位
 * @return 加密后的字节数组,一般结合Base64编码使用
 * @throws InvalidAlgorithmParameterException
 * @throws Exception
 */
public static String encrypt(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.ENCRYPT_MODE, secretKey, paramSpec);
        byte[] bytes = cipher.doFinal(data.getBytes());
        return byte2hex(bytes);
    } 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 50 with DESKeySpec

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

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

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