Search in sources :

Example 41 with DESKeySpec

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

the class WPLegacyMigrationUtils method encryptPassword.

private static String encryptPassword(String clearText) {
    try {
        DESKeySpec keySpec = new DESKeySpec(DEPRECATED_DB_PASSWORD_SECRET.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(keySpec);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return Base64.encodeToString(cipher.doFinal(clearText.getBytes("UTF-8")), Base64.DEFAULT);
    } catch (Exception e) {
    }
    return clearText;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) SQLException(android.database.SQLException)

Example 42 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project robovm by robovm.

the class DESKeySpecTest method testGetKey.

/**
     * getKey() method testing. Checks that modification of returned key
     * does not affect the internal key. Also test check an equality of
     * the key with the key specified in the constructor. The object under
     * the test is created by different constructors.
     */
public void testGetKey() {
    byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8 };
    DESKeySpec ks;
    try {
        ks = new DESKeySpec(key);
    } catch (InvalidKeyException e) {
        fail("InvalidKeyException should not be thrown.");
        return;
    }
    byte[] res = ks.getKey();
    assertTrue("The returned array should be equal to the specified " + "in constructor.", Arrays.equals(key, res));
    res[0] += 1;
    assertFalse("The modification of returned key should not affect" + "the underlying key.", key[0] == res[0]);
    byte[] key1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    try {
        ks = new DESKeySpec(key1, 2);
    } catch (InvalidKeyException e) {
        fail("InvalidKeyException should not be thrown.");
        return;
    }
    res = ks.getKey();
    assertNotSame("The returned array should not be the same object " + "as specified in a constructor.", key1, res);
    byte[] exp = new byte[8];
    System.arraycopy(key1, 2, exp, 0, 8);
    assertTrue("The returned array should be equal to the specified " + "in constructor.", Arrays.equals(exp, res));
}
Also used : DESKeySpec(javax.crypto.spec.DESKeySpec) InvalidKeyException(java.security.InvalidKeyException)

Example 43 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project yyl_example by Relucent.

the class DES_Encrypt method encrypt.

/** 根据密匙加密数据* */
public static byte[] encrypt(byte[] data, byte[] keydate) throws Exception {
    DESKeySpec dks = new DESKeySpec(keydate);
    // 创建一个密匙工厂
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey key = keyFactory.generateSecret(dks);
    // Cipher对象实际完成加密操作
    Cipher cipher = Cipher.getInstance("DES");
    // 用密匙初始化Cipher对象
    cipher.init(Cipher.ENCRYPT_MODE, key, new SecureRandom());
    // 执行加密操作
    return cipher.doFinal(data);
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 44 with DESKeySpec

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

the class CheckWeakKeys method main.

public static void main(String[] args) throws Exception {
    boolean failed = false;
    for (int i = 0; i < weakKeys.length; i++) {
        DESKeySpec desSpec = new DESKeySpec(weakKeys[i]);
        if (!DESKeySpec.isWeak(weakKeys[i], 0)) {
            failed = true;
            System.out.println("Entry " + i + " should be weak");
        }
    }
    if (failed) {
        throw new Exception("Failed test!!!");
    }
    System.out.println("Passed test.");
}
Also used : DESKeySpec(javax.crypto.spec.DESKeySpec)

Example 45 with DESKeySpec

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

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