Search in sources :

Example 71 with SecretKey

use of javax.crypto.SecretKey in project android_frameworks_base by ResurrectionRemix.

the class LockSettingsService method getDecryptedPasswordForTiedProfile.

private String getDecryptedPasswordForTiedProfile(int userId) throws KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, CertificateException, IOException {
    if (DEBUG)
        Slog.v(TAG, "Get child profile decrytped key");
    byte[] storedData = mStorage.readChildProfileLock(userId);
    if (storedData == null) {
        throw new FileNotFoundException("Child profile lock file not found");
    }
    byte[] iv = Arrays.copyOfRange(storedData, 0, PROFILE_KEY_IV_SIZE);
    byte[] encryptedPassword = Arrays.copyOfRange(storedData, PROFILE_KEY_IV_SIZE, storedData.length);
    byte[] decryptionResult;
    java.security.KeyStore keyStore = java.security.KeyStore.getInstance("AndroidKeyStore");
    keyStore.load(null);
    SecretKey decryptionKey = (SecretKey) keyStore.getKey(LockPatternUtils.PROFILE_KEY_NAME_DECRYPT + userId, null);
    Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_GCM + "/" + KeyProperties.ENCRYPTION_PADDING_NONE);
    cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new GCMParameterSpec(128, iv));
    decryptionResult = cipher.doFinal(encryptedPassword);
    return new String(decryptionResult, StandardCharsets.UTF_8);
}
Also used : SecretKey(javax.crypto.SecretKey) FileNotFoundException(java.io.FileNotFoundException) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Example 72 with SecretKey

use of javax.crypto.SecretKey in project yyl_example by Relucent.

the class AES method decrypt.

//=================================Methods================================================
/**
	 * 将密文使用AES进行解密
	 * @param encryptText 密文字符串
	 * @return 解密后的字符串
	 */
public String decrypt(String encryptText) throws Exception {
    //通过SecretKeySpec形成一个key
    SecretKey key = new SecretKeySpec(keyByte, "AES");
    //获得一个私鈅加密类Cipher,ECB是加密方式,PKCS5Padding是填充方法
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    //使用私鈅解密
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] NewCipherText = hexStringTobyteArray(encryptText);
    byte[] newString = cipher.doFinal(NewCipherText);
    return new String(newString, encoding);
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

Example 73 with SecretKey

use of javax.crypto.SecretKey in project yyl_example by Relucent.

the class DES_Encrypt method getKeyDate.

/** 获得一个密匙数据* */
public static byte[] getKeyDate() throws Exception {
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    kg.init(new SecureRandom());
    SecretKey key = kg.generateKey();
    return key.getEncoded();
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator)

Example 74 with SecretKey

use of javax.crypto.SecretKey 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 75 with SecretKey

use of javax.crypto.SecretKey in project yyl_example by Relucent.

the class PBEWithMD5AndDES_Encrypt method encrypt.

/**
	 * 将传进来的明文以PBEWithMD5AndDES算法进行加密
	 * 
	 * @param text String
	 * @return String
	 */
public String encrypt(String text) throws Exception {
    if (text == null || text.length() == 0) {
        return "";
    }
    PBEKeySpec pbks = new PBEKeySpec(password.toCharArray());
    SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey k = skf.generateSecret(pbks);
    byte[] salt = new byte[8];
    Random r = new Random();
    r.nextBytes(salt);
    Cipher cp = Cipher.getInstance("PBEWithMD5AndDES");
    PBEParameterSpec ps = new PBEParameterSpec(salt, 1000);
    cp.init(Cipher.ENCRYPT_MODE, k, ps);
    byte[] ptext = text.getBytes(encoding);
    byte[] ctext = cp.doFinal(ptext);
    String result = "";
    for (int i = 0; i < salt.length; i++) {
        result += salt[i] + " ";
    }
    for (int i = 0; i < ctext.length; i++) {
        result += ctext[i] + " ";
    }
    return string2hex(result);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Aggregations

SecretKey (javax.crypto.SecretKey)491 Cipher (javax.crypto.Cipher)176 SecretKeySpec (javax.crypto.spec.SecretKeySpec)141 KeyGenerator (javax.crypto.KeyGenerator)121 SecretKeyFactory (javax.crypto.SecretKeyFactory)89 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)87 SecureRandom (java.security.SecureRandom)61 InvalidKeyException (java.security.InvalidKeyException)58 PBEKeySpec (javax.crypto.spec.PBEKeySpec)58 IvParameterSpec (javax.crypto.spec.IvParameterSpec)46 IOException (java.io.IOException)44 Test (org.junit.Test)40 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)35 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)34 KeyStore (java.security.KeyStore)32 PrivateKey (java.security.PrivateKey)30 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)30 KeyStoreException (java.security.KeyStoreException)29 BadPaddingException (javax.crypto.BadPaddingException)29 Mac (javax.crypto.Mac)29