Search in sources :

Example 66 with GCMParameterSpec

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);
}
Also used : Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) RequiresApi(android.support.annotation.RequiresApi)

Example 67 with GCMParameterSpec

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;
}
Also used : Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Example 68 with GCMParameterSpec

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);
}
Also used : Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) RequiresApi(android.support.annotation.RequiresApi)

Example 69 with GCMParameterSpec

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()));
        }
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Example 70 with GCMParameterSpec

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);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

GCMParameterSpec (javax.crypto.spec.GCMParameterSpec)101 Cipher (javax.crypto.Cipher)71 SecretKeySpec (javax.crypto.spec.SecretKeySpec)46 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)35 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)32 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)31 InvalidKeyException (java.security.InvalidKeyException)30 BadPaddingException (javax.crypto.BadPaddingException)29 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)29 SecretKey (javax.crypto.SecretKey)21 GeneralSecurityException (java.security.GeneralSecurityException)12 AEADBadTagException (javax.crypto.AEADBadTagException)12 Key (java.security.Key)11 ByteBuffer (java.nio.ByteBuffer)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 ExcludedTest (com.google.security.wycheproof.WycheproofRunner.ExcludedTest)5 NoPresubmitTest (com.google.security.wycheproof.WycheproofRunner.NoPresubmitTest)5 SlowTest (com.google.security.wycheproof.WycheproofRunner.SlowTest)5 FileNotFoundException (java.io.FileNotFoundException)5