protected final SecretKey generateCEK(
            final EncryptionMaterials kekMaterials,
            final Provider providerIn) {
        final String keygenAlgo = contentCryptoScheme.getKeyGeneratorAlgorithm();
        KeyGenerator generator;
        try {
            generator = providerIn == null
                ? KeyGenerator.getInstance(keygenAlgo)
                : KeyGenerator.getInstance(keygenAlgo, providerIn);
            generator.init(contentCryptoScheme.getKeyLengthInBits(),
                    cryptoScheme.getSecureRandom());
            // Set to true iff the key encryption involves the use of BC's public key
            boolean involvesBCPublicKey = false;
            KeyPair keypair = kekMaterials.getKeyPair();
            if (keypair != null) {
                String keyWrapAlgo = cryptoScheme.getKeyWrapScheme().getKeyWrapAlgorithm(keypair.getPublic());
                if (keyWrapAlgo == null) {
                    Provider provider = generator.getProvider();
                    String providerName = provider == null ? null : provider.getName();
                    involvesBCPublicKey = CryptoRuntime.BOUNCY_CASTLE_PROVIDER.equals(providerName);
                }
            }
            SecretKey secretKey = generator.generateKey();
            if (!involvesBCPublicKey || secretKey.getEncoded()[0] != 0)
                return secretKey;
            for (int retry = 0; retry < 9; retry++) {
                // Regenerate the random key due to a bug/feature in BC:
                // https://github.com/aws/aws-sdk-android/issues/15
                secretKey = generator.generateKey();
                if (secretKey.getEncoded()[0] != 0)
                    return secretKey;
            }
            // The probability of getting here is 2^80, which is impossible in practice.
            throw new SdkClientException("Failed to generate secret key");
        } catch (NoSuchAlgorithmException e) {
            throw new SdkClientException(
                    "Unable to generate envelope symmetric key:"
                            + e.getMessage(), e);
        }
    }
protected final SecretKey generateCEK ( final EncryptionMaterials kekMaterials , final Provider providerIn ) { final String keygenAlgo = contentCryptoScheme . getKeyGeneratorAlgorithm ( ) ; KeyGenerator generator ; try { generator = providerIn == null ? KeyGenerator . getInstance ( keygenAlgo ) : KeyGenerator . getInstance ( keygenAlgo , providerIn ) ; generator . init ( contentCryptoScheme . getKeyLengthInBits ( ) , cryptoScheme . getSecureRandom ( ) ) ; boolean involvesBCPublicKey = false ; KeyPair keypair = kekMaterials . getKeyPair ( ) ; if ( keypair != null ) { String keyWrapAlgo = cryptoScheme . getKeyWrapScheme ( ) . getKeyWrapAlgorithm ( keypair . getPublic ( ) ) ; if ( keyWrapAlgo == null ) { Provider provider = generator . getProvider ( ) ; String providerName = provider == null ? null : provider . getName ( ) ; involvesBCPublicKey = CryptoRuntime . BOUNCY_CASTLE_PROVIDER . equals ( providerName ) ; } } SecretKey secretKey = generator . generateKey ( ) ; if ( ! involvesBCPublicKey || secretKey . getEncoded ( ) [ 0 ] != 0 ) return secretKey ; for ( int retry = 0 ; retry < 9 ; retry ++ ) { secretKey = generator . generateKey ( ) ; if ( secretKey . getEncoded ( ) [ 0 ] != 0 ) return secretKey ; } throw new SdkClientException ( "Failed to generate secret key" ) ; } catch ( NoSuchAlgorithmException e ) { throw new SdkClientException ( "Unable to generate envelope symmetric key:" + e . getMessage ( ) , e ) ; } }
@param kekMaterials non-null encryption materials