private Cipher getCipherInstance ( String algorithm ) { Cipher cipher = null ; if ( ! useBCProvider ) { try { cipher = Cipher . getInstance ( algorithm ) ; } catch ( NoSuchAlgorithmException e ) { Log . v ( Log . TAG_SYMMETRIC_KEY , "Cannot find a cipher (no algorithm); will try with Bouncy Castle provider." ) ; } catch ( NoSuchPaddingException e ) { Log . v ( Log . TAG_SYMMETRIC_KEY , "Cannot find a cipher (no padding); will try with Bouncy Castle provider." ) ; } } if ( cipher == null ) { // Register and use BouncyCastle provider if applicable: try { if ( Security . getProvider ( "BC" ) == null ) { try { Class bc = Class . forName ( "org.bouncycastle.jce.provider.BouncyCastleProvider" ) ; Security . addProvider ( ( Provider ) bc . newInstance ( ) ) ; } catch ( Exception e ) { Log . e ( Log . TAG_SYMMETRIC_KEY , "Cannot instantiate Bouncy Castle provider" , e ) ; return null ; } } cipher = Cipher . getInstance ( algorithm , "BC" ) ; useBCProvider = true ; } catch ( Exception e ) { Log . e ( Log . TAG_SYMMETRIC_KEY , "Cannot find a cipher with Bouncy Castle provider" , e ) ; } } return cipher ; }
Get a cipher instance for the algorithm . It will try to use the Cipher from the default security provider by the platform . If it couldn t find the cipher it will try to the cipher from the Bouncy Castle if the BouncyCastle library is available .