Search in sources :

Example 46 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project Signal-Android by WhisperSystems.

the class AsymmetricMasterCipher method getDigestedBytes.

private byte[] getDigestedBytes(byte[] secretBytes, int iteration) {
    try {
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secretBytes, "HmacSHA256"));
        return mac.doFinal(Conversions.intToByteArray(iteration));
    } catch (NoSuchAlgorithmException | java.security.InvalidKeyException e) {
        throw new AssertionError(e);
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) Mac(javax.crypto.Mac)

Example 47 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project Signal-Android by WhisperSystems.

the class MasterSecretUtil method generateMasterSecret.

public static MasterSecret generateMasterSecret(Context context, String passphrase) {
    try {
        byte[] encryptionSecret = generateEncryptionSecret();
        byte[] macSecret = generateMacSecret();
        byte[] masterSecret = Util.combine(encryptionSecret, macSecret);
        byte[] encryptionSalt = generateSalt();
        int iterations = generateIterationCount(passphrase, encryptionSalt);
        byte[] encryptedMasterSecret = encryptWithPassphrase(encryptionSalt, iterations, masterSecret, passphrase);
        byte[] macSalt = generateSalt();
        byte[] encryptedAndMacdMasterSecret = macWithPassphrase(macSalt, iterations, encryptedMasterSecret, passphrase);
        save(context, "encryption_salt", encryptionSalt);
        save(context, "mac_salt", macSalt);
        save(context, "passphrase_iterations", iterations);
        save(context, "master_secret", encryptedAndMacdMasterSecret);
        save(context, "passphrase_initialized", true);
        return new MasterSecret(new SecretKeySpec(encryptionSecret, "AES"), new SecretKeySpec(macSecret, "HmacSHA1"));
    } catch (GeneralSecurityException e) {
        Log.w("keyutil", e);
        return null;
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException)

Example 48 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project Openfire by igniterealtime.

the class AesEncryptor method cipher.

/**
	 * Symmetric encrypt/decrypt routine.
	 *
	 * @param attribute The value to be converted
	 * @param key The encryption key
	 * @param mode The cipher mode (encrypt or decrypt)
	 * @return The converted attribute, or null if conversion fails
	 */
private byte[] cipher(byte[] attribute, byte[] key, int mode) {
    byte[] result = null;
    try {
        // Create AES encryption key
        Key aesKey = new SecretKeySpec(key, "AES");
        // Create AES Cipher
        Cipher aesCipher = Cipher.getInstance(ALGORITHM);
        // Initialize AES Cipher and convert
        aesCipher.init(mode, aesKey, new IvParameterSpec(INIT_PARM));
        result = aesCipher.doFinal(attribute);
    } catch (Exception e) {
        log.error("AES cipher failed", e);
    }
    return result;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) Key(java.security.Key)

Example 49 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project Openfire by igniterealtime.

the class Crypto method generateAESKeystream.

public static Byte[] generateAESKeystream(Byte[] key, Integer length, Byte[] counter) {
    byte[] output = new byte[length.intValue()];
    for (int i = 0; i < output.length; i++) output[i] = 0;
    byte[] input = BitAssistant.bytesFromArray(counter);
    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
        cipher.init(1, new SecretKeySpec(BitAssistant.bytesFromArray(key), "AES"));
        for (int i = 0; i < length.intValue(); i += 16) {
            cipher.update(input, 0, 16, output, i);
            IncrementCounter(input);
        }
        for (int i = 0; i < counter.length; i++) counter[i] = Byte.valueOf(input[i]);
        return BitAssistant.bytesToArray(output);
    } catch (Exception e) {
        return new Byte[0];
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

Example 50 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project Openfire by igniterealtime.

the class Crypto method getHmacSha1.

public static Byte[] getHmacSha1(Byte[] key, Byte[] buffer) {
    try {
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(new SecretKeySpec(BitAssistant.bytesFromArray(key), "HmacSHA1"));
        return BitAssistant.bytesToArray(mac.doFinal(BitAssistant.bytesFromArray(buffer)));
    } catch (Exception e) {
        return new Byte[0];
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Mac(javax.crypto.Mac)

Aggregations

SecretKeySpec (javax.crypto.spec.SecretKeySpec)498 Cipher (javax.crypto.Cipher)194 SecretKey (javax.crypto.SecretKey)142 Mac (javax.crypto.Mac)110 IvParameterSpec (javax.crypto.spec.IvParameterSpec)106 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)94 InvalidKeyException (java.security.InvalidKeyException)67 IOException (java.io.IOException)44 Key (java.security.Key)36 SecureRandom (java.security.SecureRandom)30 Test (org.junit.Test)30 UnsupportedEncodingException (java.io.UnsupportedEncodingException)29 GeneralSecurityException (java.security.GeneralSecurityException)27 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)27 MessageDigest (java.security.MessageDigest)25 BadPaddingException (javax.crypto.BadPaddingException)25 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)25 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)18 PrivateKey (java.security.PrivateKey)18 PublicKey (java.security.PublicKey)16