Search in sources :

Example 1 with DESKeySpec

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

the class DES_Encrypt method decode.

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

Example 2 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project UltimateAndroid by cymcsg.

the class DefaultAppLock method encryptPassword.

private String encryptPassword(String clearText) {
    try {
        DESKeySpec keySpec = new DESKeySpec(PASSWORD_ENC_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);
        String encrypedPwd = Base64.encodeToString(cipher.doFinal(clearText.getBytes("UTF-8")), Base64.DEFAULT);
        return encrypedPwd;
    } catch (Exception e) {
    }
    return clearText;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 3 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project UltimateAndroid by cymcsg.

the class DefaultAppLock method decryptPassword.

private String decryptPassword(String encryptedPwd) {
    try {
        DESKeySpec keySpec = new DESKeySpec(PASSWORD_ENC_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)

Example 4 with DESKeySpec

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

the class mySecretKeyFactory method testSecretKeyFactory10.

/**
     * Test for <code>generateSecret(KeySpec keySpec)</code> and
     * <code>getKeySpec(SecretKey key, Class keySpec)
     * methods
     * Assertion:
     * throw InvalidKeySpecException if parameter is inappropriate
     */
public void testSecretKeyFactory10() throws InvalidKeyException, InvalidKeySpecException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    byte[] bb = new byte[24];
    KeySpec ks = (defaultAlgorithm.equals(defaultAlgorithm2) ? (KeySpec) new DESKeySpec(bb) : (KeySpec) new DESedeKeySpec(bb));
    KeySpec rks = null;
    SecretKeySpec secKeySpec = new SecretKeySpec(bb, defaultAlgorithm);
    SecretKey secKey = null;
    SecretKeyFactory[] skF = createSKFac();
    assertNotNull("SecretKeyFactory object were not created", skF);
    for (int i = 0; i < skF.length; i++) {
        try {
            skF[i].generateSecret(null);
            fail("generateSecret(null): InvalidKeySpecException must be thrown");
        } catch (InvalidKeySpecException e) {
        }
        secKey = skF[i].generateSecret(ks);
        try {
            skF[i].getKeySpec(null, null);
            fail("getKeySpec(null,null): InvalidKeySpecException must be thrown");
        } catch (InvalidKeySpecException e) {
        }
        try {
            skF[i].getKeySpec(null, ks.getClass());
            fail("getKeySpec(null, Class): InvalidKeySpecException must be thrown");
        } catch (InvalidKeySpecException e) {
        }
        try {
            skF[i].getKeySpec(secKey, null);
            fail("getKeySpec(secKey, null): NullPointerException or InvalidKeySpecException must be thrown");
        } catch (InvalidKeySpecException e) {
        // Expected
        } catch (NullPointerException e) {
        // Expected
        }
        try {
            Class c;
            if (defaultAlgorithm.equals(defaultAlgorithm2)) {
                c = DESedeKeySpec.class;
            } else {
                c = DESKeySpec.class;
            }
            skF[i].getKeySpec(secKeySpec, c);
            fail("getKeySpec(secKey, Class): InvalidKeySpecException must be thrown");
        } catch (InvalidKeySpecException e) {
        }
        rks = skF[i].getKeySpec(secKeySpec, ks.getClass());
        if (defaultAlgorithm.equals(defaultAlgorithm1)) {
            assertTrue("Incorrect getKeySpec() result 1", rks instanceof DESedeKeySpec);
        } else {
            assertTrue("Incorrect getKeySpec() result 1", rks instanceof DESKeySpec);
        }
        rks = skF[i].getKeySpec(secKey, ks.getClass());
        if (defaultAlgorithm.equals(defaultAlgorithm1)) {
            assertTrue("Incorrect getKeySpec() result 2", rks instanceof DESedeKeySpec);
        } else {
            assertTrue("Incorrect getKeySpec() result 2", rks instanceof DESKeySpec);
        }
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) 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) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 5 with DESKeySpec

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

the class SecretKeyFactoryThread method test.

@Override
public void test() throws Exception {
    SecretKeyFactory skf = SecretKeyFactory.getInstance(algName);
    byte[] b = new byte[24];
    KeySpec ks = (KeySpec) ((algName == "DES") ? new DESKeySpec(b) : (algName == "DESede") ? new DESedeKeySpec(b) : new PBEKeySpec("passw".toCharArray()));
    skf.generateSecret(ks);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeySpec(java.security.spec.KeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) 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