use of javax.crypto.spec.GCMParameterSpec in project AmazeFileManager by TeamAmaze.
the class CryptUtil method aesEncryptPassword.
/**
* Helper method to encrypt plain text password
*/
@RequiresApi(api = Build.VERSION_CODES.M)
private static String aesEncryptPassword(String plainTextPassword) throws GeneralSecurityException, IOException {
Cipher cipher = Cipher.getInstance(ALGO_AES);
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128, IV.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(), gcmParameterSpec);
byte[] encodedBytes = cipher.doFinal(plainTextPassword.getBytes());
return Base64.encodeToString(encodedBytes, Base64.DEFAULT);
}
use of javax.crypto.spec.GCMParameterSpec in project AmazeFileManager by TeamAmaze.
the class CryptUtil method initCipher.
/**
* Method initializes a Cipher to be used by {@link android.hardware.fingerprint.FingerprintManager}
*/
public static Cipher initCipher(Context context) throws GeneralSecurityException, IOException {
Cipher cipher = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cipher = Cipher.getInstance(ALGO_AES);
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128, IV.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(), gcmParameterSpec);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
cipher = Cipher.getInstance(ALGO_AES, "BC");
RSAKeygen keygen = new RSAKeygen(context);
cipher.init(Cipher.ENCRYPT_MODE, keygen.getSecretKey());
}
return cipher;
}
use of javax.crypto.spec.GCMParameterSpec in project AmazeFileManager by TeamAmaze.
the class CryptUtil method aesDecryptPassword.
/**
* Helper method to decrypt cipher text password
*/
@RequiresApi(api = Build.VERSION_CODES.M)
private static String aesDecryptPassword(String cipherPassword) throws GeneralSecurityException, IOException {
Cipher cipher = Cipher.getInstance(ALGO_AES);
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128, IV.getBytes());
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(), gcmParameterSpec);
byte[] decryptedBytes = cipher.doFinal(Base64.decode(cipherPassword, Base64.DEFAULT));
return new String(decryptedBytes);
}
use of javax.crypto.spec.GCMParameterSpec in project accumulo by apache.
the class DefaultCryptoModule method initCipher.
/**
* @param params
* the crypto parameters
* @param cipher
* the Java Cipher object to be init'd
* @param opMode
* encrypt or decrypt
* @throws InvalidKeyException
* if the crypto params are missing necessary values
* @throws InvalidAlgorithmParameterException
* if an invalid algorithm is chosen
*/
private void initCipher(CryptoModuleParameters params, Cipher cipher, int opMode) throws InvalidKeyException, InvalidAlgorithmParameterException {
if (params.getCipherSuiteEncryptionMode().equals(ALGORITHM_PARAMETER_SPEC_GCM)) {
cipher.init(opMode, new SecretKeySpec(params.getPlaintextKey(), params.getKeyAlgorithmName()), new GCMParameterSpec(GCM_TAG_LENGTH_IN_BYTES * 8, params.getInitializationVector()));
} else {
if (params.getInitializationVector() == null) {
cipher.init(opMode, new SecretKeySpec(params.getPlaintextKey(), params.getKeyAlgorithmName()), params.getSecureRandom());
params.setInitializationVector(cipher.getIV());
} else {
cipher.init(opMode, new SecretKeySpec(params.getPlaintextKey(), params.getKeyAlgorithmName()), new IvParameterSpec(params.getInitializationVector()));
}
}
}
use of javax.crypto.spec.GCMParameterSpec in project Signal-Android by WhisperSystems.
the class UnidentifiedAccess method deriveAccessKeyFrom.
public static byte[] deriveAccessKeyFrom(ProfileKey profileKey) {
try {
byte[] nonce = new byte[12];
byte[] input = new byte[16];
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(profileKey.serialize(), "AES"), new GCMParameterSpec(128, nonce));
byte[] ciphertext = cipher.doFinal(input);
return ByteUtil.trim(ciphertext, 16);
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | InvalidAlgorithmParameterException | BadPaddingException | IllegalBlockSizeException e) {
throw new AssertionError(e);
}
}
Aggregations