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);
}
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);
}
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();
}
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);
}
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);
}
Aggregations